kitDsd.c 107 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    [kitDsd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Computation kit.]

  Synopsis    [Performs disjoint-support decomposition based on truth tables.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - Dec 6, 2006.]

  Revision    [$Id: kitDsd.c,v 1.00 2006/12/06 00:00:00 alanmi Exp $]

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

#include "kit.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
  Synopsis    [Allocates the DSD manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
45
Kit_DsdMan_t * Kit_DsdManAlloc( int nVars, int nNodes )
Alan Mishchenko committed
46
{
Alan Mishchenko committed
47
    Kit_DsdMan_t * p;
Alan Mishchenko committed
48
    p = ABC_ALLOC( Kit_DsdMan_t, 1 );
Alan Mishchenko committed
49
    memset( p, 0, sizeof(Kit_DsdMan_t) );
Alan Mishchenko committed
50 51
    p->nVars    = nVars;
    p->nWords   = Kit_TruthWordNum( p->nVars );
Alan Mishchenko committed
52
    p->vTtElems = Vec_PtrAllocTruthTables( p->nVars );
Alan Mishchenko committed
53
    p->vTtNodes = Vec_PtrAllocSimInfo( nNodes, p->nWords );
Alan Mishchenko committed
54
    p->dd       = Cloud_Init( 16, 14 );
Alan Mishchenko committed
55 56
    p->vTtBdds  = Vec_PtrAllocSimInfo( (1<<12), p->nWords );
    p->vNodes   = Vec_IntAlloc( 512 );
Alan Mishchenko committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    return p;
}

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

  Synopsis    [Deallocates the DSD manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
71
void Kit_DsdManFree( Kit_DsdMan_t * p )
Alan Mishchenko committed
72
{
Alan Mishchenko committed
73 74 75
    Cloud_Quit( p->dd );
    Vec_IntFree( p->vNodes );
    Vec_PtrFree( p->vTtBdds );
Alan Mishchenko committed
76 77
    Vec_PtrFree( p->vTtElems );
    Vec_PtrFree( p->vTtNodes );
Alan Mishchenko committed
78
    ABC_FREE( p );
Alan Mishchenko committed
79 80 81 82
}

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

Alan Mishchenko committed
83 84 85 86 87 88 89 90 91
  Synopsis    [Allocates the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
92
Kit_DsdObj_t * Kit_DsdObjAlloc( Kit_DsdNtk_t * pNtk, Kit_Dsd_t Type, int nFans )
Alan Mishchenko committed
93
{
Alan Mishchenko committed
94 95
    Kit_DsdObj_t * pObj;
    int nSize = sizeof(Kit_DsdObj_t) + sizeof(unsigned) * (Kit_DsdObjOffset(nFans) + (Type == KIT_DSD_PRIME) * Kit_TruthWordNum(nFans));
Alan Mishchenko committed
96
    pObj = (Kit_DsdObj_t *)ABC_ALLOC( char, nSize );
Alan Mishchenko committed
97 98
    memset( pObj, 0, nSize );
    pObj->Id = pNtk->nVars + pNtk->nNodes;
Alan Mishchenko committed
99
    pObj->Type = Type;
Alan Mishchenko committed
100
    pObj->nFans = nFans;
Alan Mishchenko committed
101
    pObj->Offset = Kit_DsdObjOffset( nFans );
Alan Mishchenko committed
102
    // add the object
Alan Mishchenko committed
103 104 105
    if ( pNtk->nNodes == pNtk->nNodesAlloc )
    {
        pNtk->nNodesAlloc *= 2;
Alan Mishchenko committed
106
        pNtk->pNodes = ABC_REALLOC( Kit_DsdObj_t *, pNtk->pNodes, pNtk->nNodesAlloc ); 
Alan Mishchenko committed
107
    }
Alan Mishchenko committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    assert( pNtk->nNodes < pNtk->nNodesAlloc );
    pNtk->pNodes[pNtk->nNodes++] = pObj;
    return pObj;
}

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

  Synopsis    [Deallocates the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
124
void Kit_DsdObjFree( Kit_DsdNtk_t * p, Kit_DsdObj_t * pObj )
Alan Mishchenko committed
125
{
Alan Mishchenko committed
126
    ABC_FREE( pObj );
Alan Mishchenko committed
127 128 129 130 131 132 133 134 135 136 137 138 139
}

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

  Synopsis    [Allocates the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
140
Kit_DsdNtk_t * Kit_DsdNtkAlloc( int nVars )
Alan Mishchenko committed
141
{
Alan Mishchenko committed
142
    Kit_DsdNtk_t * pNtk;
Alan Mishchenko committed
143
    pNtk = ABC_ALLOC( Kit_DsdNtk_t, 1 );
Alan Mishchenko committed
144
    memset( pNtk, 0, sizeof(Kit_DsdNtk_t) );
Alan Mishchenko committed
145
    pNtk->pNodes = ABC_ALLOC( Kit_DsdObj_t *, nVars+1 );
Alan Mishchenko committed
146
    pNtk->nVars = nVars;
Alan Mishchenko committed
147
    pNtk->nNodesAlloc = nVars+1;
Alan Mishchenko committed
148
    pNtk->pMem = ABC_ALLOC( unsigned, 6 * Kit_TruthWordNum(nVars) );
Alan Mishchenko committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    return pNtk;
}

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

  Synopsis    [Deallocate the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
163
void Kit_DsdNtkFree( Kit_DsdNtk_t * pNtk )
Alan Mishchenko committed
164
{
Alan Mishchenko committed
165
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
166
    unsigned i;
Alan Mishchenko committed
167
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
Alan Mishchenko committed
168 169 170 171 172
        ABC_FREE( pObj );
    ABC_FREE( pNtk->pSupps );
    ABC_FREE( pNtk->pNodes );
    ABC_FREE( pNtk->pMem );
    ABC_FREE( pNtk );
Alan Mishchenko committed
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
}

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

  Synopsis    [Prints the hex unsigned into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdPrintHex( FILE * pFile, unsigned * pTruth, int nFans )
{
    int nDigits, Digit, k;
    nDigits = (1 << nFans) / 4;
    for ( k = nDigits - 1; k >= 0; k-- )
    {
        Digit = ((pTruth[k/8] >> ((k%8) * 4)) & 15);
        if ( Digit < 10 )
            fprintf( pFile, "%d", Digit );
        else
            fprintf( pFile, "%c", 'A' + Digit-10 );
    }
}

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

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
  Synopsis    [Prints the hex unsigned into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Kit_DsdWriteHex( char * pBuff, unsigned * pTruth, int nFans )
{
    int nDigits, Digit, k;
    nDigits = (1 << nFans) / 4;
    for ( k = nDigits - 1; k >= 0; k-- )
    {
        Digit = ((pTruth[k/8] >> ((k%8) * 4)) & 15);
        if ( Digit < 10 )
            *pBuff++ = '0' + Digit;
        else
            *pBuff++ = 'A' + Digit-10;
    }
    return pBuff;
}

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

Alan Mishchenko committed
228 229 230 231 232 233 234 235 236
  Synopsis    [Recursively print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
void Kit_DsdPrint2_rec( FILE * pFile, Kit_DsdNtk_t * pNtk, int Id )
{
    Kit_DsdObj_t * pObj;
    unsigned iLit, i;
    char Symbol;

    pObj = Kit_DsdNtkObj( pNtk, Id );
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
        fprintf( pFile, "%c", 'a' + Id );
        return;
    }

    if ( pObj->Type == KIT_DSD_CONST1 )
    {
        assert( pObj->nFans == 0 );
        fprintf( pFile, "Const1" );
        return;
    }

    if ( pObj->Type == KIT_DSD_VAR )
        assert( pObj->nFans == 1 );

    if ( pObj->Type == KIT_DSD_AND )
        Symbol = '*';
    else if ( pObj->Type == KIT_DSD_XOR )
        Symbol = '+';
    else 
        Symbol = ',';

    if ( pObj->Type == KIT_DSD_PRIME )
        fprintf( pFile, "[" );
    else
        fprintf( pFile, "(" );
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
    {
        if ( Kit_DsdLitIsCompl(iLit) ) 
            fprintf( pFile, "!" );
        Kit_DsdPrint2_rec( pFile, pNtk, Kit_DsdLit2Var(iLit) );
        if ( i < pObj->nFans - 1 )
            fprintf( pFile, "%c", Symbol );
    }
    if ( pObj->Type == KIT_DSD_PRIME )
        fprintf( pFile, "]" );
    else
        fprintf( pFile, ")" );
}

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

  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdPrint2( FILE * pFile, Kit_DsdNtk_t * pNtk )
{
//    fprintf( pFile, "F = " );
    if ( Kit_DsdLitIsCompl(pNtk->Root) )
        fprintf( pFile, "!" );
    Kit_DsdPrint2_rec( pFile, pNtk, Kit_DsdLit2Var(pNtk->Root) );
//    fprintf( pFile, "\n" );
}

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

  Synopsis    [Recursively print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
317
void Kit_DsdPrint_rec( FILE * pFile, Kit_DsdNtk_t * pNtk, int Id )
Alan Mishchenko committed
318
{
Alan Mishchenko committed
319
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
320
    unsigned iLit, i;
Alan Mishchenko committed
321 322
    char Symbol;

Alan Mishchenko committed
323
    pObj = Kit_DsdNtkObj( pNtk, Id );
Alan Mishchenko committed
324 325 326 327 328 329 330
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
        fprintf( pFile, "%c", 'a' + Id );
        return;
    }

Alan Mishchenko committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    if ( pObj->Type == KIT_DSD_CONST1 )
    {
        assert( pObj->nFans == 0 );
        fprintf( pFile, "Const1" );
        return;
    }

    if ( pObj->Type == KIT_DSD_VAR )
        assert( pObj->nFans == 1 );

    if ( pObj->Type == KIT_DSD_AND )
        Symbol = '*';
    else if ( pObj->Type == KIT_DSD_XOR )
        Symbol = '+';
    else 
        Symbol = ',';

Alan Mishchenko committed
348
    if ( pObj->Type == KIT_DSD_PRIME )
Alan Mishchenko committed
349
        Kit_DsdPrintHex( stdout, Kit_DsdObjTruth(pObj), pObj->nFans );
Alan Mishchenko committed
350 351

    fprintf( pFile, "(" );
Alan Mishchenko committed
352
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
Alan Mishchenko committed
353
    {
Alan Mishchenko committed
354
        if ( Kit_DsdLitIsCompl(iLit) ) 
Alan Mishchenko committed
355
            fprintf( pFile, "!" );
Alan Mishchenko committed
356
        Kit_DsdPrint_rec( pFile, pNtk, Kit_DsdLit2Var(iLit) );
Alan Mishchenko committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        if ( i < pObj->nFans - 1 )
            fprintf( pFile, "%c", Symbol );
    }
    fprintf( pFile, ")" );
}

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

  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
374
void Kit_DsdPrint( FILE * pFile, Kit_DsdNtk_t * pNtk )
Alan Mishchenko committed
375 376
{
    fprintf( pFile, "F = " );
Alan Mishchenko committed
377
    if ( Kit_DsdLitIsCompl(pNtk->Root) )
Alan Mishchenko committed
378
        fprintf( pFile, "!" );
Alan Mishchenko committed
379
    Kit_DsdPrint_rec( pFile, pNtk, Kit_DsdLit2Var(pNtk->Root) );
380
//    fprintf( pFile, "\n" );
Alan Mishchenko committed
381 382 383 384
}

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

385 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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  Synopsis    [Recursively print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Kit_DsdWrite_rec( char * pBuff, Kit_DsdNtk_t * pNtk, int Id )
{
    Kit_DsdObj_t * pObj;
    unsigned iLit, i;
    char Symbol;

    pObj = Kit_DsdNtkObj( pNtk, Id );
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
        *pBuff++ = 'a' + Id;
        return pBuff;
    }

    if ( pObj->Type == KIT_DSD_CONST1 )
    {
        assert( pObj->nFans == 0 );
        sprintf( pBuff, "%s", "Const1" );
        return pBuff + strlen("Const1");
    }

    if ( pObj->Type == KIT_DSD_VAR )
        assert( pObj->nFans == 1 );

    if ( pObj->Type == KIT_DSD_AND )
        Symbol = '*';
    else if ( pObj->Type == KIT_DSD_XOR )
        Symbol = '+';
    else 
        Symbol = ',';

    if ( pObj->Type == KIT_DSD_PRIME )
        pBuff = Kit_DsdWriteHex( pBuff, Kit_DsdObjTruth(pObj), pObj->nFans );

    *pBuff++ = '(';
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
    {
        if ( Kit_DsdLitIsCompl(iLit) ) 
            *pBuff++ = '!';
        pBuff = Kit_DsdWrite_rec( pBuff, pNtk, Kit_DsdLit2Var(iLit) );
        if ( i < pObj->nFans - 1 )
            *pBuff++ = Symbol;
    }
    *pBuff++ = ')';
    return pBuff;
}

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

  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdWrite( char * pBuff, Kit_DsdNtk_t * pNtk )
{
    if ( Kit_DsdLitIsCompl(pNtk->Root) )
        *pBuff++ = '!';
    pBuff = Kit_DsdWrite_rec( pBuff, pNtk, Kit_DsdLit2Var(pNtk->Root) );
    *pBuff = 0;
}

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

Alan Mishchenko committed
462 463 464 465 466 467 468 469 470 471 472 473 474
  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdPrintExpanded( Kit_DsdNtk_t * pNtk )
{
    Kit_DsdNtk_t * pTemp;
    pTemp = Kit_DsdExpand( pNtk );
Alan Mishchenko committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    Kit_DsdPrint( stdout, pTemp );
    Kit_DsdNtkFree( pTemp );
}

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

  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdPrintFromTruth( unsigned * pTruth, int nVars )
{
Alan Mishchenko committed
492
    Kit_DsdNtk_t * pTemp, * pTemp2;
493 494
//    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 5 );
    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 8 );
Alan Mishchenko committed
495 496 497 498 499
//    Kit_DsdPrintExpanded( pTemp );
    pTemp2 = Kit_DsdExpand( pTemp );
    Kit_DsdPrint( stdout, pTemp2 );
    Kit_DsdVerify( pTemp2, pTruth, nVars ); 
    Kit_DsdNtkFree( pTemp2 );
Alan Mishchenko committed
500 501 502 503 504
    Kit_DsdNtkFree( pTemp );
}

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

505 506 507 508 509 510 511 512 513
  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
void Kit_DsdPrintFromTruth2( FILE * pFile, unsigned * pTruth, int nVars )
{
    Kit_DsdNtk_t * pTemp, * pTemp2;
    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 0 );
    pTemp2 = Kit_DsdExpand( pTemp );
    Kit_DsdPrint2( pFile, pTemp2 );
    Kit_DsdVerify( pTemp2, pTruth, nVars ); 
    Kit_DsdNtkFree( pTemp2 );
    Kit_DsdNtkFree( pTemp );
}

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

  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
void Kit_DsdWriteFromTruth( char * pBuffer, unsigned * pTruth, int nVars )
{
    Kit_DsdNtk_t * pTemp, * pTemp2;
//    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 5 );
    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 8 );
//    Kit_DsdPrintExpanded( pTemp );
    pTemp2 = Kit_DsdExpand( pTemp );
    Kit_DsdWrite( pBuffer, pTemp2 );
    Kit_DsdVerify( pTemp2, pTruth, nVars ); 
    Kit_DsdNtkFree( pTemp2 );
    Kit_DsdNtkFree( pTemp );
}

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

Alan Mishchenko committed
551 552 553 554 555 556 557 558 559
  Synopsis    [Derives the truth table of the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
560
unsigned * Kit_DsdTruthComputeNode_rec( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, int Id )
Alan Mishchenko committed
561
{
Alan Mishchenko committed
562
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
563 564 565 566 567 568
    unsigned * pTruthRes, * pTruthFans[16], * pTruthTemp;
    unsigned i, iLit, fCompl;
//    unsigned m, nMints, * pTruthPrime, * pTruthMint; 

    // get the node with this ID
    pObj = Kit_DsdNtkObj( pNtk, Id );
569
    pTruthRes = (unsigned *)Vec_PtrEntry( p->vTtNodes, Id );
Alan Mishchenko committed
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

    // special case: literal of an internal node
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
        return pTruthRes;
    }

    // constant node
    if ( pObj->Type == KIT_DSD_CONST1 )
    {
        assert( pObj->nFans == 0 );
        Kit_TruthFill( pTruthRes, pNtk->nVars );
        return pTruthRes;
    }

    // elementary variable node
    if ( pObj->Type == KIT_DSD_VAR )
    {
        assert( pObj->nFans == 1 );
        iLit = pObj->pFans[0];
        pTruthFans[0] = Kit_DsdTruthComputeNode_rec( p, pNtk, Kit_DsdLit2Var(iLit) );
        if ( Kit_DsdLitIsCompl(iLit) )
            Kit_TruthNot( pTruthRes, pTruthFans[0], pNtk->nVars );
        else
            Kit_TruthCopy( pTruthRes, pTruthFans[0], pNtk->nVars );
        return pTruthRes;
    }

    // collect the truth tables of the fanins
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        pTruthFans[i] = Kit_DsdTruthComputeNode_rec( p, pNtk, Kit_DsdLit2Var(iLit) );
    // create the truth table

    // simple gates
    if ( pObj->Type == KIT_DSD_AND )
    {
        Kit_TruthFill( pTruthRes, pNtk->nVars );
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Kit_DsdLitIsCompl(iLit) );
        return pTruthRes;
    }
    if ( pObj->Type == KIT_DSD_XOR )
    {
        Kit_TruthClear( pTruthRes, pNtk->nVars );
        fCompl = 0;
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        {
            Kit_TruthXor( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars );
            fCompl ^= Kit_DsdLitIsCompl(iLit);
        }
        if ( fCompl )
            Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
        return pTruthRes;
    }
    assert( pObj->Type == KIT_DSD_PRIME );
/*
    // get the truth table of the prime node
    pTruthPrime = Kit_DsdObjTruth( pObj );
    // get storage for the temporary minterm
    pTruthMint = Vec_PtrEntry(p->vTtNodes, pNtk->nVars + pNtk->nNodes);
    // go through the minterms
    nMints = (1 << pObj->nFans);
    Kit_TruthClear( pTruthRes, pNtk->nVars );
    for ( m = 0; m < nMints; m++ )
    {
        if ( !Kit_TruthHasBit(pTruthPrime, m) )
            continue;
        Kit_TruthFill( pTruthMint, pNtk->nVars );
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Kit_DsdLitIsCompl(iLit) );
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
*/
Alan Mishchenko committed
644 645 646
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        if ( Kit_DsdLitIsCompl(iLit) )
            Kit_TruthNot( pTruthFans[i], pTruthFans[i], pNtk->nVars );
Alan Mishchenko committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    pTruthTemp = Kit_TruthCompose( p->dd, Kit_DsdObjTruth(pObj), pObj->nFans, pTruthFans, pNtk->nVars, p->vTtBdds, p->vNodes );
    Kit_TruthCopy( pTruthRes, pTruthTemp, pNtk->nVars );
    return pTruthRes;
}

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

  Synopsis    [Derives the truth table of the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Kit_DsdTruthCompute( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk )
{
    unsigned * pTruthRes;
    int i;
    // assign elementary truth ables
    assert( pNtk->nVars <= p->nVars );
    for ( i = 0; i < (int)pNtk->nVars; i++ )
670
        Kit_TruthCopy( (unsigned *)Vec_PtrEntry(p->vTtNodes, i), (unsigned *)Vec_PtrEntry(p->vTtElems, i), p->nVars );
Alan Mishchenko committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
    // compute truth table for each node
    pTruthRes = Kit_DsdTruthComputeNode_rec( p, pNtk, Kit_DsdLit2Var(pNtk->Root) );
    // complement the truth table if needed
    if ( Kit_DsdLitIsCompl(pNtk->Root) )
        Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
    return pTruthRes;
}

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

  Synopsis    [Derives the truth table of the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Kit_DsdTruthComputeNodeOne_rec( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, int Id, unsigned uSupp )
{
    Kit_DsdObj_t * pObj;
    unsigned * pTruthRes, * pTruthFans[16], * pTruthTemp;
    unsigned i, iLit, fCompl, nPartial = 0;
//    unsigned m, nMints, * pTruthPrime, * pTruthMint; 
Alan Mishchenko committed
696 697

    // get the node with this ID
Alan Mishchenko committed
698
    pObj = Kit_DsdNtkObj( pNtk, Id );
699
    pTruthRes = (unsigned *)Vec_PtrEntry( p->vTtNodes, Id );
Alan Mishchenko committed
700 701 702 703 704

    // special case: literal of an internal node
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
Alan Mishchenko committed
705
        assert( !uSupp || uSupp != (uSupp & ~(1<<Id)) );
Alan Mishchenko committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
        return pTruthRes;
    }

    // constant node
    if ( pObj->Type == KIT_DSD_CONST1 )
    {
        assert( pObj->nFans == 0 );
        Kit_TruthFill( pTruthRes, pNtk->nVars );
        return pTruthRes;
    }

    // elementary variable node
    if ( pObj->Type == KIT_DSD_VAR )
    {
        assert( pObj->nFans == 1 );
        iLit = pObj->pFans[0];
Alan Mishchenko committed
722
        assert( Kit_DsdLitIsLeaf( pNtk, iLit ) );
Alan Mishchenko committed
723
        pTruthFans[0] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Kit_DsdLit2Var(iLit), uSupp );
Alan Mishchenko committed
724
        if ( Kit_DsdLitIsCompl(iLit) )
Alan Mishchenko committed
725 726 727 728 729 730 731
            Kit_TruthNot( pTruthRes, pTruthFans[0], pNtk->nVars );
        else
            Kit_TruthCopy( pTruthRes, pTruthFans[0], pNtk->nVars );
        return pTruthRes;
    }

    // collect the truth tables of the fanins
Alan Mishchenko committed
732 733 734 735
    if ( uSupp )
    {
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            if ( uSupp != (uSupp & ~Kit_DsdLitSupport(pNtk, iLit)) )
Alan Mishchenko committed
736
                pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Kit_DsdLit2Var(iLit), uSupp );
Alan Mishchenko committed
737 738 739
            else
            {
                pTruthFans[i] = NULL;
Alan Mishchenko committed
740
                nPartial = 1;
Alan Mishchenko committed
741 742 743 744 745
            }
    }
    else
    {
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
Alan Mishchenko committed
746
            pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Kit_DsdLit2Var(iLit), uSupp );
Alan Mishchenko committed
747
    }
Alan Mishchenko committed
748 749 750 751 752 753
    // create the truth table

    // simple gates
    if ( pObj->Type == KIT_DSD_AND )
    {
        Kit_TruthFill( pTruthRes, pNtk->nVars );
Alan Mishchenko committed
754
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
Alan Mishchenko committed
755 756
            if ( pTruthFans[i] )
                Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Kit_DsdLitIsCompl(iLit) );
Alan Mishchenko committed
757 758 759 760 761 762
        return pTruthRes;
    }
    if ( pObj->Type == KIT_DSD_XOR )
    {
        Kit_TruthClear( pTruthRes, pNtk->nVars );
        fCompl = 0;
Alan Mishchenko committed
763
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
Alan Mishchenko committed
764
        {
Alan Mishchenko committed
765 766 767 768 769
            if ( pTruthFans[i] )
            {
                Kit_TruthXor( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars );
                fCompl ^= Kit_DsdLitIsCompl(iLit);
            }
Alan Mishchenko committed
770 771 772 773 774 775 776
        }
        if ( fCompl )
            Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
        return pTruthRes;
    }
    assert( pObj->Type == KIT_DSD_PRIME );

Alan Mishchenko committed
777
    if ( uSupp && nPartial )
Alan Mishchenko committed
778 779 780 781 782 783 784 785
    {
        // find the only non-empty component
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            if ( pTruthFans[i] )
                break;
        assert( i < pObj->nFans );
        return pTruthFans[i];
    }
Alan Mishchenko committed
786
/*
Alan Mishchenko committed
787
    // get the truth table of the prime node
Alan Mishchenko committed
788
    pTruthPrime = Kit_DsdObjTruth( pObj );
Alan Mishchenko committed
789 790 791 792 793 794 795 796 797 798
    // get storage for the temporary minterm
    pTruthMint = Vec_PtrEntry(p->vTtNodes, pNtk->nVars + pNtk->nNodes);
    // go through the minterms
    nMints = (1 << pObj->nFans);
    Kit_TruthClear( pTruthRes, pNtk->nVars );
    for ( m = 0; m < nMints; m++ )
    {
        if ( !Kit_TruthHasBit(pTruthPrime, m) )
            continue;
        Kit_TruthFill( pTruthMint, pNtk->nVars );
Alan Mishchenko committed
799 800
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Kit_DsdLitIsCompl(iLit) );
Alan Mishchenko committed
801 802
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
Alan Mishchenko committed
803
*/
Alan Mishchenko committed
804 805 806
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        if ( Kit_DsdLitIsCompl(iLit) )
            Kit_TruthNot( pTruthFans[i], pTruthFans[i], pNtk->nVars );
Alan Mishchenko committed
807 808
    pTruthTemp = Kit_TruthCompose( p->dd, Kit_DsdObjTruth(pObj), pObj->nFans, pTruthFans, pNtk->nVars, p->vTtBdds, p->vNodes );
    Kit_TruthCopy( pTruthRes, pTruthTemp, pNtk->nVars );
Alan Mishchenko committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822
    return pTruthRes;
}

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

  Synopsis    [Derives the truth table of the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
823
unsigned * Kit_DsdTruthComputeOne( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, unsigned uSupp )
Alan Mishchenko committed
824 825 826
{
    unsigned * pTruthRes;
    int i;
Alan Mishchenko committed
827 828 829 830
    // if support is specified, request that supports are available
    if ( uSupp )
        Kit_DsdGetSupports( pNtk );
    // assign elementary truth tables
Alan Mishchenko committed
831 832
    assert( pNtk->nVars <= p->nVars );
    for ( i = 0; i < (int)pNtk->nVars; i++ )
833
        Kit_TruthCopy( (unsigned *)Vec_PtrEntry(p->vTtNodes, i), (unsigned *)Vec_PtrEntry(p->vTtElems, i), p->nVars );
Alan Mishchenko committed
834
    // compute truth table for each node
Alan Mishchenko committed
835
    pTruthRes = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Kit_DsdLit2Var(pNtk->Root), uSupp );
Alan Mishchenko committed
836
    // complement the truth table if needed
Alan Mishchenko committed
837
    if ( Kit_DsdLitIsCompl(pNtk->Root) )
Alan Mishchenko committed
838 839 840 841
        Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
    return pTruthRes;
}

Alan Mishchenko committed
842 843
/**Function*************************************************************

Alan Mishchenko committed
844 845 846 847 848 849 850 851 852 853 854 855 856
  Synopsis    [Derives the truth table of the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Kit_DsdTruthComputeNodeTwo_rec( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, int Id, unsigned uSupp, int iVar, unsigned * pTruthDec )
{
    Kit_DsdObj_t * pObj;
    int pfBoundSet[16];
Alan Mishchenko committed
857 858 859
    unsigned * pTruthRes, * pTruthFans[16], * pTruthTemp;
    unsigned i, iLit, fCompl, nPartial, uSuppFan, uSuppCur;
//    unsigned m, nMints, * pTruthPrime, * pTruthMint; 
Alan Mishchenko committed
860 861 862 863
    assert( uSupp > 0 );

    // get the node with this ID
    pObj = Kit_DsdNtkObj( pNtk, Id );
864
    pTruthRes = (unsigned *)Vec_PtrEntry( p->vTtNodes, Id );
Alan Mishchenko committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
        return pTruthRes;
    }
    assert( pObj->Type != KIT_DSD_CONST1 );
    assert( pObj->Type != KIT_DSD_VAR );

    // count the number of intersecting fanins 
    // collect the total support of the intersecting fanins
    nPartial = 0;
    uSuppFan = 0;
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
    {
        uSuppCur = Kit_DsdLitSupport(pNtk, iLit);
        if ( uSupp & uSuppCur )
        {
            nPartial++;
            uSuppFan |= uSuppCur;
        }
    }

    // if there is no intersection, or full intersection, use simple procedure
    if ( nPartial == 0 || nPartial == pObj->nFans )
Alan Mishchenko committed
889
        return Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Id, 0 );
Alan Mishchenko committed
890 891 892 893 894 895 896 897 898 899 900 901

    // if support of the component includes some other variables
    // we need to continue constructing it as usual by the two-function procedure
    if ( uSuppFan != (uSuppFan & uSupp) )
    {
        assert( nPartial == 1 );
//        return Kit_DsdTruthComputeNodeTwo_rec( p, pNtk, Id, uSupp, iVar, pTruthDec );
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        {
            if ( uSupp & Kit_DsdLitSupport(pNtk, iLit) )
                pTruthFans[i] = Kit_DsdTruthComputeNodeTwo_rec( p, pNtk, Kit_DsdLit2Var(iLit), uSupp, iVar, pTruthDec );
            else
Alan Mishchenko committed
902
                pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Kit_DsdLit2Var(iLit), 0 );
Alan Mishchenko committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
        }

        // create composition/decomposition functions
        if ( pObj->Type == KIT_DSD_AND )
        {
            Kit_TruthFill( pTruthRes, pNtk->nVars );
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
                Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Kit_DsdLitIsCompl(iLit) );
            return pTruthRes;
        }
        if ( pObj->Type == KIT_DSD_XOR )
        {
            Kit_TruthClear( pTruthRes, pNtk->nVars );
            fCompl = 0;
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            {
                fCompl ^= Kit_DsdLitIsCompl(iLit);
                Kit_TruthXor( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars );
            }
            if ( fCompl )
                Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
            return pTruthRes;
        }
        assert( pObj->Type == KIT_DSD_PRIME );
    }
    else
    {
        assert( uSuppFan == (uSuppFan & uSupp) );
        assert( nPartial < pObj->nFans );
        // the support of the insecting component(s) is contained in the bound-set
        // and yet there are components that are not contained in the bound set

        // solve the fanins and collect info, which components belong to the bound set
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        {
Alan Mishchenko committed
938
            pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Kit_DsdLit2Var(iLit), 0 );
Alan Mishchenko committed
939 940 941 942 943 944 945 946 947 948 949 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 978 979 980 981 982 983 984
            pfBoundSet[i] = (int)((uSupp & Kit_DsdLitSupport(pNtk, iLit)) > 0);
        }

        // create composition/decomposition functions
        if ( pObj->Type == KIT_DSD_AND )
        {
            Kit_TruthIthVar( pTruthRes, pNtk->nVars, iVar );
            Kit_TruthFill( pTruthDec, pNtk->nVars );
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
                if ( pfBoundSet[i] )
                    Kit_TruthAndPhase( pTruthDec, pTruthDec, pTruthFans[i], pNtk->nVars, 0, Kit_DsdLitIsCompl(iLit) );
                else
                    Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Kit_DsdLitIsCompl(iLit) );
            return pTruthRes;
        }
        if ( pObj->Type == KIT_DSD_XOR )
        {
            Kit_TruthIthVar( pTruthRes, pNtk->nVars, iVar );
            Kit_TruthClear( pTruthDec, pNtk->nVars );
            fCompl = 0;
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            {
                fCompl ^= Kit_DsdLitIsCompl(iLit);
                if ( pfBoundSet[i] )
                    Kit_TruthXor( pTruthDec, pTruthDec, pTruthFans[i], pNtk->nVars );
                else
                    Kit_TruthXor( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars );
            }
            if ( fCompl )
                Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
            return pTruthRes;
        }
        assert( pObj->Type == KIT_DSD_PRIME );
        assert( nPartial == 1 );

        // find the only non-empty component
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            if ( pfBoundSet[i] )
                break;
        assert( i < pObj->nFans );

        // save this component as the decomposed function
        Kit_TruthCopy( pTruthDec, pTruthFans[i], pNtk->nVars );
        // set the corresponding component to be the new variable
        Kit_TruthIthVar( pTruthFans[i], pNtk->nVars, iVar );
    }
Alan Mishchenko committed
985
/*
Alan Mishchenko committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    // get the truth table of the prime node
    pTruthPrime = Kit_DsdObjTruth( pObj );
    // get storage for the temporary minterm
    pTruthMint = Vec_PtrEntry(p->vTtNodes, pNtk->nVars + pNtk->nNodes);
    // go through the minterms
    nMints = (1 << pObj->nFans);
    Kit_TruthClear( pTruthRes, pNtk->nVars );
    for ( m = 0; m < nMints; m++ )
    {
        if ( !Kit_TruthHasBit(pTruthPrime, m) )
            continue;
        Kit_TruthFill( pTruthMint, pNtk->nVars );
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Kit_DsdLitIsCompl(iLit) );
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
Alan Mishchenko committed
1002
*/
Alan Mishchenko committed
1003 1004
//    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
//        assert( !Kit_DsdLitIsCompl(iLit) );
Alan Mishchenko committed
1005
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
Alan Mishchenko committed
1006 1007
        if ( Kit_DsdLitIsCompl(iLit) )
            Kit_TruthNot( pTruthFans[i], pTruthFans[i], pNtk->nVars );
Alan Mishchenko committed
1008 1009
    pTruthTemp = Kit_TruthCompose( p->dd, Kit_DsdObjTruth(pObj), pObj->nFans, pTruthFans, pNtk->nVars, p->vTtBdds, p->vNodes );
    Kit_TruthCopy( pTruthRes, pTruthTemp, pNtk->nVars );
Alan Mishchenko committed
1010 1011 1012 1013 1014
    return pTruthRes;
}

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

Alan Mishchenko committed
1015 1016 1017 1018 1019 1020 1021 1022 1023
  Synopsis    [Derives the truth table of the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1024
unsigned * Kit_DsdTruthComputeTwo( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, unsigned uSupp, int iVar, unsigned * pTruthDec )
Alan Mishchenko committed
1025
{
Alan Mishchenko committed
1026
    unsigned * pTruthRes, uSuppAll;
Alan Mishchenko committed
1027
    int i;
Alan Mishchenko committed
1028
    assert( uSupp > 0 );
Alan Mishchenko committed
1029
    assert( pNtk->nVars <= p->nVars );
Alan Mishchenko committed
1030 1031 1032 1033 1034 1035
    // compute support of all nodes
    uSuppAll = Kit_DsdGetSupports( pNtk );
    // consider special case - there is no overlap
    if ( (uSupp & uSuppAll) == 0 )
    {
        Kit_TruthClear( pTruthDec, pNtk->nVars );
Alan Mishchenko committed
1036
        return Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
1037 1038 1039 1040
    }
    // consider special case - support is fully contained
    if ( (uSupp & uSuppAll) == uSuppAll )
    {
Alan Mishchenko committed
1041
        pTruthRes = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
1042 1043 1044 1045 1046
        Kit_TruthCopy( pTruthDec, pTruthRes, pNtk->nVars );
        Kit_TruthIthVar( pTruthRes, pNtk->nVars, iVar );
        return pTruthRes;
    }
    // assign elementary truth tables
Alan Mishchenko committed
1047
    for ( i = 0; i < (int)pNtk->nVars; i++ )
1048
        Kit_TruthCopy( (unsigned *)Vec_PtrEntry(p->vTtNodes, i), (unsigned *)Vec_PtrEntry(p->vTtElems, i), p->nVars );
Alan Mishchenko committed
1049
    // compute truth table for each node
Alan Mishchenko committed
1050
    pTruthRes = Kit_DsdTruthComputeNodeTwo_rec( p, pNtk, Kit_DsdLit2Var(pNtk->Root), uSupp, iVar, pTruthDec );
Alan Mishchenko committed
1051 1052 1053
    // complement the truth table if needed
    if ( Kit_DsdLitIsCompl(pNtk->Root) )
        Kit_TruthNot( pTruthRes, pTruthRes, pNtk->nVars );
Alan Mishchenko committed
1054
    return pTruthRes;
Alan Mishchenko committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
}

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

  Synopsis    [Derives the truth table of the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1068 1069 1070 1071
void Kit_DsdTruth( Kit_DsdNtk_t * pNtk, unsigned * pTruthRes )
{
    Kit_DsdMan_t * p;
    unsigned * pTruth;
Alan Mishchenko committed
1072
    p = Kit_DsdManAlloc( pNtk->nVars, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
1073
    pTruth = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
1074 1075 1076
    Kit_TruthCopy( pTruthRes, pTruth, pNtk->nVars );
    Kit_DsdManFree( p );
}
Alan Mishchenko committed
1077 1078 1079

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

Alan Mishchenko committed
1080 1081 1082 1083 1084 1085 1086 1087 1088
  Synopsis    [Derives the truth table of the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1089
void Kit_DsdTruthPartialTwo( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, unsigned uSupp, int iVar, unsigned * pTruthCo, unsigned * pTruthDec )
Alan Mishchenko committed
1090
{
Alan Mishchenko committed
1091 1092 1093
    unsigned * pTruth = Kit_DsdTruthComputeTwo( p, pNtk, uSupp, iVar, pTruthDec );
    if ( pTruthCo )
        Kit_TruthCopy( pTruthCo, pTruth, pNtk->nVars );
Alan Mishchenko committed
1094 1095 1096 1097
}

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

Alan Mishchenko committed
1098 1099 1100 1101 1102 1103 1104 1105 1106
  Synopsis    [Derives the truth table of the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1107
void Kit_DsdTruthPartial( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, unsigned * pTruthRes, unsigned uSupp )
Alan Mishchenko committed
1108
{
Alan Mishchenko committed
1109
    unsigned * pTruth = Kit_DsdTruthComputeOne( p, pNtk, uSupp );
Alan Mishchenko committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    Kit_TruthCopy( pTruthRes, pTruth, pNtk->nVars );
/*
    // verification
    {
        // compute the same function using different procedure
        unsigned * pTruthTemp = Vec_PtrEntry(p->vTtNodes, pNtk->nVars + pNtk->nNodes + 1);
        pNtk->pSupps = NULL;
        Kit_DsdTruthComputeTwo( p, pNtk, uSupp, -1, pTruthTemp );
//        if ( !Kit_TruthIsEqual( pTruthTemp, pTruthRes, pNtk->nVars ) )
        if ( !Kit_TruthIsEqualWithPhase( pTruthTemp, pTruthRes, pNtk->nVars ) )
        {
            printf( "Verification FAILED!\n" );
            Kit_DsdPrint( stdout, pNtk );
            Kit_DsdPrintFromTruth( pTruthRes, pNtk->nVars );
            Kit_DsdPrintFromTruth( pTruthTemp, pNtk->nVars );
        }
//        else
//            printf( "Verification successful.\n" );
    }
*/
Alan Mishchenko committed
1130 1131 1132 1133
}

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

Alan Mishchenko committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
  Synopsis    [Counts the number of blocks of the given number of inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCountLuts_rec( Kit_DsdNtk_t * pNtk, int nLutSize, int Id, int * pCounter )
{
    Kit_DsdObj_t * pObj;
    unsigned iLit, i, Res0, Res1;
    pObj = Kit_DsdNtkObj( pNtk, Id );
    if ( pObj == NULL )
        return 0;
    if ( pObj->Type == KIT_DSD_AND || pObj->Type == KIT_DSD_XOR )
    {
        assert( pObj->nFans == 2 );
        Res0 = Kit_DsdCountLuts_rec( pNtk, nLutSize, Kit_DsdLit2Var(pObj->pFans[0]), pCounter );
        Res1 = Kit_DsdCountLuts_rec( pNtk, nLutSize, Kit_DsdLit2Var(pObj->pFans[1]), pCounter );
        if ( Res0 == 0 && Res1 > 0 )
            return Res1 - 1;
        if ( Res0 > 0 && Res1 == 0 )
            return Res0 - 1;
        (*pCounter)++;
        return nLutSize - 2;
    }
    assert( pObj->Type == KIT_DSD_PRIME );
Alan Mishchenko committed
1163
    if ( (int)pObj->nFans > nLutSize ) //+ 1 )
Alan Mishchenko committed
1164 1165 1166 1167 1168 1169 1170
    {
        *pCounter = 1000;
        return 0;
    }
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        Kit_DsdCountLuts_rec( pNtk, nLutSize, Kit_DsdLit2Var(iLit), pCounter );
    (*pCounter)++;
Alan Mishchenko committed
1171 1172
//    if ( (int)pObj->nFans == nLutSize + 1 )
//        (*pCounter)++;
Alan Mishchenko committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    return nLutSize - pObj->nFans;
}

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

  Synopsis    [Counts the number of blocks of the given number of inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCountLuts( Kit_DsdNtk_t * pNtk, int nLutSize )
{
    int Counter = 0;
    if ( Kit_DsdNtkRoot(pNtk)->Type == KIT_DSD_CONST1 )
        return 0;
    if ( Kit_DsdNtkRoot(pNtk)->Type == KIT_DSD_VAR )
        return 0;
    Kit_DsdCountLuts_rec( pNtk, nLutSize, Kit_DsdLit2Var(pNtk->Root), &Counter );
    if ( Counter >= 1000 )
        return -1;
    return Counter;
}

Alan Mishchenko committed
1200 1201
/**Function*************************************************************

Alan Mishchenko committed
1202
  Synopsis    [Returns the size of the largest non-DSD block.]
Alan Mishchenko committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdNonDsdSizeMax( Kit_DsdNtk_t * pNtk )
{
    Kit_DsdObj_t * pObj;
    unsigned i, nSizeMax = 0;
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
    {
        if ( pObj->Type != KIT_DSD_PRIME )
            continue;
        if ( nSizeMax < pObj->nFans )
            nSizeMax = pObj->nFans;
    }
    return nSizeMax;
}

Alan Mishchenko committed
1225 1226
/**Function*************************************************************

Alan Mishchenko committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  Synopsis    [Returns the largest non-DSD block.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Kit_DsdObj_t * Kit_DsdNonDsdPrimeMax( Kit_DsdNtk_t * pNtk )
{
    Kit_DsdObj_t * pObj, * pObjMax = NULL;
    unsigned i, nSizeMax = 0;
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
    {
        if ( pObj->Type != KIT_DSD_PRIME )
            continue;
        if ( nSizeMax < pObj->nFans )
        {
            nSizeMax = pObj->nFans;
            pObjMax = pObj;
        }
    }
    return pObjMax;
}

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

Alan Mishchenko committed
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
  Synopsis    [Finds the union of supports of the non-DSD blocks.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Kit_DsdNonDsdSupports( Kit_DsdNtk_t * pNtk )
{
    Kit_DsdObj_t * pObj;
    unsigned i, uSupport = 0;
Alan Mishchenko committed
1268
//    ABC_FREE( pNtk->pSupps );
Alan Mishchenko committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    Kit_DsdGetSupports( pNtk );
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
    {
        if ( pObj->Type != KIT_DSD_PRIME )
            continue;
        uSupport |= Kit_DsdLitSupport( pNtk, Kit_DsdVar2Lit(pObj->Id,0) );
    }
    return uSupport;
}

Alan Mishchenko committed
1279

Alan Mishchenko committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
/**Function*************************************************************

  Synopsis    [Expands the node.]

  Description [Returns the new literal.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1291
void Kit_DsdExpandCollectAnd_rec( Kit_DsdNtk_t * p, unsigned iLit, unsigned * piLitsNew, int * nLitsNew )
Alan Mishchenko committed
1292
{
Alan Mishchenko committed
1293
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
1294 1295
    unsigned i, iLitFanin;
    // check the end of the supergate
Alan Mishchenko committed
1296 1297
    pObj = Kit_DsdNtkObj( p, Kit_DsdLit2Var(iLit) );
    if ( Kit_DsdLitIsCompl(iLit) || Kit_DsdLit2Var(iLit) < p->nVars || pObj->Type != KIT_DSD_AND )
Alan Mishchenko committed
1298 1299 1300 1301 1302
    {
        piLitsNew[(*nLitsNew)++] = iLit;
        return;
    }
    // iterate through the fanins
Alan Mishchenko committed
1303
    Kit_DsdObjForEachFanin( p, pObj, iLitFanin, i )
Alan Mishchenko committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
        Kit_DsdExpandCollectAnd_rec( p, iLitFanin, piLitsNew, nLitsNew );
}

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

  Synopsis    [Expands the node.]

  Description [Returns the new literal.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1318
void Kit_DsdExpandCollectXor_rec( Kit_DsdNtk_t * p, unsigned iLit, unsigned * piLitsNew, int * nLitsNew )
Alan Mishchenko committed
1319
{
Alan Mishchenko committed
1320
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
1321 1322
    unsigned i, iLitFanin;
    // check the end of the supergate
Alan Mishchenko committed
1323 1324
    pObj = Kit_DsdNtkObj( p, Kit_DsdLit2Var(iLit) );
    if ( Kit_DsdLit2Var(iLit) < p->nVars || pObj->Type != KIT_DSD_XOR )
Alan Mishchenko committed
1325 1326 1327 1328 1329
    {
        piLitsNew[(*nLitsNew)++] = iLit;
        return;
    }
    // iterate through the fanins
Alan Mishchenko committed
1330 1331
    pObj = Kit_DsdNtkObj( p, Kit_DsdLit2Var(iLit) );
    Kit_DsdObjForEachFanin( p, pObj, iLitFanin, i )
Alan Mishchenko committed
1332 1333
        Kit_DsdExpandCollectXor_rec( p, iLitFanin, piLitsNew, nLitsNew );
    // if the literal was complemented, pass the complemented attribute somewhere
Alan Mishchenko committed
1334 1335
    if ( Kit_DsdLitIsCompl(iLit) )
        piLitsNew[0] = Kit_DsdLitNot( piLitsNew[0] );
Alan Mishchenko committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
}

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

  Synopsis    [Expands the node.]

  Description [Returns the new literal.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1349
int Kit_DsdExpandNode_rec( Kit_DsdNtk_t * pNew, Kit_DsdNtk_t * p, int iLit )
Alan Mishchenko committed
1350 1351
{
    unsigned * pTruth, * pTruthNew;
Alan Mishchenko committed
1352
    unsigned i, iLitFanin, piLitsNew[16], nLitsNew = 0;
Alan Mishchenko committed
1353
    Kit_DsdObj_t * pObj, * pObjNew;
Alan Mishchenko committed
1354 1355

    // consider the case of simple gate
Alan Mishchenko committed
1356 1357
    pObj = Kit_DsdNtkObj( p, Kit_DsdLit2Var(iLit) );
    if ( pObj == NULL )
Alan Mishchenko committed
1358
        return iLit;
Alan Mishchenko committed
1359 1360
    if ( pObj->Type == KIT_DSD_AND )
    {
1361
        Kit_DsdExpandCollectAnd_rec( p, Kit_DsdLitRegular(iLit), piLitsNew, (int *)&nLitsNew );
Alan Mishchenko committed
1362
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_AND, nLitsNew );
Alan Mishchenko committed
1363 1364
        for ( i = 0; i < pObjNew->nFans; i++ )
            pObjNew->pFans[i] = Kit_DsdExpandNode_rec( pNew, p, piLitsNew[i] );
Alan Mishchenko committed
1365
        return Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(iLit) );
Alan Mishchenko committed
1366 1367 1368
    }
    if ( pObj->Type == KIT_DSD_XOR )
    {
Alan Mishchenko committed
1369
        int fCompl = Kit_DsdLitIsCompl(iLit);
1370
        Kit_DsdExpandCollectXor_rec( p, Kit_DsdLitRegular(iLit), piLitsNew, (int *)&nLitsNew );
Alan Mishchenko committed
1371
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_XOR, nLitsNew );
Alan Mishchenko committed
1372 1373
        for ( i = 0; i < pObjNew->nFans; i++ )
        {
Alan Mishchenko committed
1374 1375
            pObjNew->pFans[i] = Kit_DsdExpandNode_rec( pNew, p, Kit_DsdLitRegular(piLitsNew[i]) );
            fCompl ^= Kit_DsdLitIsCompl(piLitsNew[i]);
Alan Mishchenko committed
1376
        }
Alan Mishchenko committed
1377
        return Kit_DsdVar2Lit( pObjNew->Id, fCompl );
Alan Mishchenko committed
1378 1379 1380 1381
    }
    assert( pObj->Type == KIT_DSD_PRIME );

    // create new PRIME node
Alan Mishchenko committed
1382
    pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_PRIME, pObj->nFans );
Alan Mishchenko committed
1383
    // copy the truth table
Alan Mishchenko committed
1384 1385
    pTruth = Kit_DsdObjTruth( pObj );
    pTruthNew = Kit_DsdObjTruth( pObjNew );
Alan Mishchenko committed
1386 1387
    Kit_TruthCopy( pTruthNew, pTruth, pObj->nFans );
    // create fanins
Alan Mishchenko committed
1388
    Kit_DsdObjForEachFanin( pNtk, pObj, iLitFanin, i )
Alan Mishchenko committed
1389 1390 1391
    {
        pObjNew->pFans[i] = Kit_DsdExpandNode_rec( pNew, p, iLitFanin );
        // complement the corresponding inputs of the truth table
Alan Mishchenko committed
1392
        if ( Kit_DsdLitIsCompl(pObjNew->pFans[i]) )
Alan Mishchenko committed
1393
        {
Alan Mishchenko committed
1394
            pObjNew->pFans[i] = Kit_DsdLitRegular(pObjNew->pFans[i]);
Alan Mishchenko committed
1395 1396 1397
            Kit_TruthChangePhase( pTruthNew, pObjNew->nFans, i );
        }
    }
Alan Mishchenko committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

    if ( pObj->nFans == 3 && 
        (pTruthNew[0] == 0xCACACACA || pTruthNew[0] == 0xC5C5C5C5 || 
         pTruthNew[0] == 0x3A3A3A3A || pTruthNew[0] == 0x35353535) )
    {
        // translate into regular MUXes
        if ( pTruthNew[0] == 0xC5C5C5C5 )
            pObjNew->pFans[0] = Kit_DsdLitNot(pObjNew->pFans[0]);
        else if ( pTruthNew[0] == 0x3A3A3A3A )
            pObjNew->pFans[1] = Kit_DsdLitNot(pObjNew->pFans[1]);
        else if ( pTruthNew[0] == 0x35353535 )
        {
            pObjNew->pFans[0] = Kit_DsdLitNot(pObjNew->pFans[0]);
            pObjNew->pFans[1] = Kit_DsdLitNot(pObjNew->pFans[1]);
        }
        pTruthNew[0] = 0xCACACACA;
        // resolve the complemented control input
        if ( Kit_DsdLitIsCompl(pObjNew->pFans[2]) )
        {
            unsigned char Temp = pObjNew->pFans[0];
            pObjNew->pFans[0] = pObjNew->pFans[1];
            pObjNew->pFans[1] = Temp;
            pObjNew->pFans[2] = Kit_DsdLitNot(pObjNew->pFans[2]);
        }
        // resolve the complemented true input
        if ( Kit_DsdLitIsCompl(pObjNew->pFans[1]) )
        {
            iLit = Kit_DsdLitNot(iLit);
            pObjNew->pFans[0] = Kit_DsdLitNot(pObjNew->pFans[0]);
            pObjNew->pFans[1] = Kit_DsdLitNot(pObjNew->pFans[1]);
        }
        return Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(iLit) );
    }
    else
    {
        // if the incoming phase is complemented, absorb it into the prime node
        if ( Kit_DsdLitIsCompl(iLit) )
            Kit_TruthNot( pTruthNew, pTruthNew, pObj->nFans );
        return Kit_DsdVar2Lit( pObjNew->Id, 0 );
    }
Alan Mishchenko committed
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
}

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

  Synopsis    [Expands the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1451
Kit_DsdNtk_t * Kit_DsdExpand( Kit_DsdNtk_t * p )
Alan Mishchenko committed
1452
{
Alan Mishchenko committed
1453 1454
    Kit_DsdNtk_t * pNew;
    Kit_DsdObj_t * pObjNew;
Alan Mishchenko committed
1455 1456 1457 1458
    assert( p->nVars <= 16 );
    // create a new network
    pNew = Kit_DsdNtkAlloc( p->nVars );
    // consider simple special cases
Alan Mishchenko committed
1459
    if ( Kit_DsdNtkRoot(p)->Type == KIT_DSD_CONST1 )
Alan Mishchenko committed
1460
    {
Alan Mishchenko committed
1461 1462
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_CONST1, 0 );
        pNew->Root = Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(p->Root) );
Alan Mishchenko committed
1463 1464
        return pNew;
    }
Alan Mishchenko committed
1465
    if ( Kit_DsdNtkRoot(p)->Type == KIT_DSD_VAR )
Alan Mishchenko committed
1466
    {
Alan Mishchenko committed
1467 1468 1469
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_VAR, 1 );
        pObjNew->pFans[0] = Kit_DsdNtkRoot(p)->pFans[0];
        pNew->Root = Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(p->Root) );
Alan Mishchenko committed
1470 1471 1472 1473 1474 1475 1476 1477 1478
        return pNew;
    }
    // convert the root node
    pNew->Root = Kit_DsdExpandNode_rec( pNew, p, p->Root );
    return pNew;
}

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

Alan Mishchenko committed
1479 1480 1481 1482 1483 1484 1485 1486 1487
  Synopsis    [Sorts the literals by their support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1488
void Kit_DsdCompSort( int pPrios[], unsigned uSupps[], unsigned char * piLits, int nVars, unsigned piLitsRes[] )
Alan Mishchenko committed
1489
{
Alan Mishchenko committed
1490 1491
    int nSuppSizes[16], Priority[16], pOrder[16];
    int i, k, iVarBest, SuppMax, PrioMax;
Alan Mishchenko committed
1492 1493 1494
    // compute support sizes and priorities of the components
    for ( i = 0; i < nVars; i++ )
    {
Alan Mishchenko committed
1495
        assert( uSupps[i] );
Alan Mishchenko committed
1496
        pOrder[i] = i;
Alan Mishchenko committed
1497
        Priority[i] = KIT_INFINITY;
Alan Mishchenko committed
1498 1499 1500 1501
        for ( k = 0; k < 16; k++ )
            if ( uSupps[i] & (1 << k) )
                Priority[i] = KIT_MIN( Priority[i], pPrios[k] );
        assert( Priority[i] != 16 );
Alan Mishchenko committed
1502
        nSuppSizes[i] = Kit_WordCountOnes(uSupps[i]);
Alan Mishchenko committed
1503
    }
Alan Mishchenko committed
1504 1505 1506
    // sort the components by pririty
    Extra_BubbleSort( pOrder, Priority, nVars, 0 );
    // find the component by with largest size and lowest priority
Alan Mishchenko committed
1507
    iVarBest = -1;
Alan Mishchenko committed
1508 1509
    SuppMax  = 0;
    PrioMax  = 0;
Alan Mishchenko committed
1510 1511
    for ( i = 0; i < nVars; i++ )
    {
Alan Mishchenko committed
1512
        if ( SuppMax < nSuppSizes[i] || (SuppMax == nSuppSizes[i] && PrioMax < Priority[i]) )
Alan Mishchenko committed
1513
        {
Alan Mishchenko committed
1514 1515
            SuppMax  = nSuppSizes[i];
            PrioMax  = Priority[i];
Alan Mishchenko committed
1516 1517 1518
            iVarBest = i;
        }
    }
Alan Mishchenko committed
1519
    assert( iVarBest != -1 );
Alan Mishchenko committed
1520 1521
    // copy the resulting literals
    k = 0;
Alan Mishchenko committed
1522
    piLitsRes[k++] = piLits[iVarBest];
Alan Mishchenko committed
1523 1524 1525 1526
    for ( i = 0; i < nVars; i++ )
    {
        if ( pOrder[i] == iVarBest )
            continue;
Alan Mishchenko committed
1527
        piLitsRes[k++] = piLits[pOrder[i]];
Alan Mishchenko committed
1528
    }
Alan Mishchenko committed
1529
    assert( k == nVars );
Alan Mishchenko committed
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
}

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

  Synopsis    [Shrinks multi-input nodes.]

  Description [Takes the array of variable priorities pPrios.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdShrink_rec( Kit_DsdNtk_t * pNew, Kit_DsdNtk_t * p, int iLit, int pPrios[] )
{
Alan Mishchenko committed
1545 1546
    Kit_DsdObj_t * pObj;
    Kit_DsdObj_t * pObjNew = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
1547 1548
    unsigned * pTruth, * pTruthNew;
    unsigned i, piLitsNew[16], uSupps[16];
Alan Mishchenko committed
1549
    int iLitFanin, iLitNew;
Alan Mishchenko committed
1550 1551 1552 1553

    // consider the case of simple gate
    pObj = Kit_DsdNtkObj( p, Kit_DsdLit2Var(iLit) );
    if ( pObj == NULL )
Alan Mishchenko committed
1554
        return iLit;
Alan Mishchenko committed
1555 1556
    if ( pObj->Type == KIT_DSD_AND )
    {
Alan Mishchenko committed
1557 1558 1559 1560 1561 1562 1563 1564 1565
        // get the supports
        Kit_DsdObjForEachFanin( p, pObj, iLitFanin, i )
            uSupps[i] = Kit_DsdLitSupport( p, iLitFanin );
        // put the largest component last
        // sort other components in the decreasing order of priority of their vars
        Kit_DsdCompSort( pPrios, uSupps, pObj->pFans, pObj->nFans, piLitsNew );
        // construct the two-input node network
        iLitNew = Kit_DsdShrink_rec( pNew, p, piLitsNew[0], pPrios );
        for ( i = 1; i < pObj->nFans; i++ )
Alan Mishchenko committed
1566 1567
        {
            pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_AND, 2 );
Alan Mishchenko committed
1568 1569 1570
            pObjNew->pFans[0] = Kit_DsdShrink_rec( pNew, p, piLitsNew[i], pPrios );
            pObjNew->pFans[1] = iLitNew;
            iLitNew = Kit_DsdVar2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1571
        }
Alan Mishchenko committed
1572
        return Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(iLit) );
Alan Mishchenko committed
1573 1574 1575
    }
    if ( pObj->Type == KIT_DSD_XOR )
    {
Alan Mishchenko committed
1576 1577
        // get the supports
        Kit_DsdObjForEachFanin( p, pObj, iLitFanin, i )
Alan Mishchenko committed
1578
        {
Alan Mishchenko committed
1579 1580
            assert( !Kit_DsdLitIsCompl(iLitFanin) );
            uSupps[i] = Kit_DsdLitSupport( p, iLitFanin );
Alan Mishchenko committed
1581
        }
Alan Mishchenko committed
1582 1583 1584 1585 1586 1587
        // put the largest component last
        // sort other components in the decreasing order of priority of their vars
        Kit_DsdCompSort( pPrios, uSupps, pObj->pFans, pObj->nFans, piLitsNew );
        // construct the two-input node network
        iLitNew = Kit_DsdShrink_rec( pNew, p, piLitsNew[0], pPrios );
        for ( i = 1; i < pObj->nFans; i++ )
Alan Mishchenko committed
1588
        {
Alan Mishchenko committed
1589 1590 1591 1592
            pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_XOR, 2 );
            pObjNew->pFans[0] = Kit_DsdShrink_rec( pNew, p, piLitsNew[i], pPrios );
            pObjNew->pFans[1] = iLitNew;
            iLitNew = Kit_DsdVar2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1593
        }
Alan Mishchenko committed
1594
        return Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(iLit) );
Alan Mishchenko committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
    }
    assert( pObj->Type == KIT_DSD_PRIME );

    // create new PRIME node
    pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_PRIME, pObj->nFans );
    // copy the truth table
    pTruth = Kit_DsdObjTruth( pObj );
    pTruthNew = Kit_DsdObjTruth( pObjNew );
    Kit_TruthCopy( pTruthNew, pTruth, pObj->nFans );
    // create fanins
    Kit_DsdObjForEachFanin( pNtk, pObj, iLitFanin, i )
    {
        pObjNew->pFans[i] = Kit_DsdShrink_rec( pNew, p, iLitFanin, pPrios );
        // complement the corresponding inputs of the truth table
        if ( Kit_DsdLitIsCompl(pObjNew->pFans[i]) )
        {
            pObjNew->pFans[i] = Kit_DsdLitRegular(pObjNew->pFans[i]);
            Kit_TruthChangePhase( pTruthNew, pObjNew->nFans, i );
        }
    }
    // if the incoming phase is complemented, absorb it into the prime node
Alan Mishchenko committed
1616
    if ( Kit_DsdLitIsCompl(iLit) )
Alan Mishchenko committed
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
        Kit_TruthNot( pTruthNew, pTruthNew, pObj->nFans );
    return Kit_DsdVar2Lit( pObjNew->Id, 0 );
}

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

  Synopsis    [Shrinks the network.]

  Description [Transforms the network to have two-input nodes so that the
  higher-ordered nodes were decomposed out first.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Kit_DsdNtk_t * Kit_DsdShrink( Kit_DsdNtk_t * p, int pPrios[] )
{
    Kit_DsdNtk_t * pNew;
    Kit_DsdObj_t * pObjNew;
    assert( p->nVars <= 16 );
    // create a new network
    pNew = Kit_DsdNtkAlloc( p->nVars );
    // consider simple special cases
    if ( Kit_DsdNtkRoot(p)->Type == KIT_DSD_CONST1 )
    {
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_CONST1, 0 );
        pNew->Root = Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(p->Root) );
        return pNew;
    }
    if ( Kit_DsdNtkRoot(p)->Type == KIT_DSD_VAR )
    {
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_VAR, 1 );
        pObjNew->pFans[0] = Kit_DsdNtkRoot(p)->pFans[0];
        pNew->Root = Kit_DsdVar2Lit( pObjNew->Id, Kit_DsdLitIsCompl(p->Root) );
        return pNew;
    }
    // convert the root node
    pNew->Root = Kit_DsdShrink_rec( pNew, p, p->Root, pPrios );
    return pNew;
}

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

  Synopsis    [Rotates the network.]

  Description [Transforms prime nodes to have the fanin with the
  highest frequency of supports go first.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdRotate( Kit_DsdNtk_t * p, int pFreqs[] )
{
    Kit_DsdObj_t * pObj;
    unsigned * pIn, * pOut, * pTemp, k;
    int i, v, Temp, uSuppFanin, iFaninLit, WeightMax, FaninMax, nSwaps;
    int Weights[16];
    // go through the prime nodes
    Kit_DsdNtkForEachObj( p, pObj, i )
    {
        if ( pObj->Type != KIT_DSD_PRIME )
            continue;
        // count the fanin frequencies
        Kit_DsdObjForEachFanin( p, pObj, iFaninLit, k )
        {
            uSuppFanin = Kit_DsdLitSupport( p, iFaninLit );
            Weights[k] = 0;
            for ( v = 0; v < 16; v++ )
                if ( uSuppFanin & (1 << v) )
Alan Mishchenko committed
1689
                    Weights[k] += pFreqs[v] - 1;
Alan Mishchenko committed
1690 1691
        }
        // find the most frequent fanin
Alan Mishchenko committed
1692 1693
        WeightMax = 0; 
        FaninMax = -1;
Alan Mishchenko committed
1694 1695 1696 1697 1698 1699
        for ( k = 0; k < pObj->nFans; k++ )
            if ( WeightMax < Weights[k] )
            {
                WeightMax = Weights[k];
                FaninMax = k;
            }
Alan Mishchenko committed
1700 1701 1702
        // no need to reorder if there are no frequent fanins
        if ( FaninMax == -1 )
            continue;
Alan Mishchenko committed
1703 1704 1705
        // move the fanins number k to the first place
        nSwaps = 0;
        pIn = Kit_DsdObjTruth(pObj);
Alan Mishchenko committed
1706 1707 1708
        pOut = p->pMem;
//        for ( v = FaninMax; v < ((int)pObj->nFans)-1; v++ )
        for ( v = FaninMax-1; v >= 0; v-- )
Alan Mishchenko committed
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
        {
            // swap the fanins
            Temp = pObj->pFans[v];
            pObj->pFans[v] = pObj->pFans[v+1];
            pObj->pFans[v+1] = Temp;
            // swap the truth table variables
            Kit_TruthSwapAdjacentVars( pOut, pIn, pObj->nFans, v );
            pTemp = pIn; pIn = pOut; pOut = pTemp;
            nSwaps++;
        }
Alan Mishchenko committed
1719 1720
        if ( nSwaps & 1 )
            Kit_TruthCopy( pOut, pIn, pObj->nFans );
Alan Mishchenko committed
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    }    
}

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

  Synopsis    [Compute the support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1735
unsigned Kit_DsdGetSupports_rec( Kit_DsdNtk_t * p, int iLit )
Alan Mishchenko committed
1736 1737 1738
{
    Kit_DsdObj_t * pObj;
    unsigned uSupport, k;
Alan Mishchenko committed
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
    int iFaninLit;
    pObj = Kit_DsdNtkObj( p, Kit_DsdLit2Var(iLit) );
    if ( pObj == NULL )
        return Kit_DsdLitSupport( p, iLit );
    uSupport = 0;
    Kit_DsdObjForEachFanin( p, pObj, iFaninLit, k )
        uSupport |= Kit_DsdGetSupports_rec( p, iFaninLit );
    p->pSupps[pObj->Id - p->nVars] = uSupport;
    assert( uSupport <= 0xFFFF );
    return uSupport;
}

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

  Synopsis    [Compute the support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1762
unsigned Kit_DsdGetSupports( Kit_DsdNtk_t * p )
Alan Mishchenko committed
1763 1764
{
    Kit_DsdObj_t * pRoot;
Alan Mishchenko committed
1765
    unsigned uSupport;
Alan Mishchenko committed
1766
    assert( p->pSupps == NULL );
Alan Mishchenko committed
1767
    p->pSupps = ABC_ALLOC( unsigned, p->nNodes );
Alan Mishchenko committed
1768 1769 1770
    // consider simple special cases
    pRoot = Kit_DsdNtkRoot(p);
    if ( pRoot->Type == KIT_DSD_CONST1 )
Alan Mishchenko committed
1771
    {
Alan Mishchenko committed
1772
        assert( p->nNodes == 1 );
Alan Mishchenko committed
1773
        uSupport = p->pSupps[0] = 0;
Alan Mishchenko committed
1774 1775 1776 1777
    }
    if ( pRoot->Type == KIT_DSD_VAR )
    {
        assert( p->nNodes == 1 );
Alan Mishchenko committed
1778
        uSupport = p->pSupps[0] = Kit_DsdLitSupport( p, pRoot->pFans[0] );
Alan Mishchenko committed
1779 1780
    }
    else
Alan Mishchenko committed
1781 1782 1783
        uSupport = Kit_DsdGetSupports_rec( p, p->Root );
    assert( uSupport <= 0xFFFF );
    return uSupport;
Alan Mishchenko committed
1784 1785 1786 1787
}

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

Alan Mishchenko committed
1788 1789 1790 1791 1792 1793 1794 1795 1796
  Synopsis    [Returns 1 if there is a component with more than 3 inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1797
int Kit_DsdFindLargeBox_rec( Kit_DsdNtk_t * pNtk, int Id, int Size )
Alan Mishchenko committed
1798
{
Alan Mishchenko committed
1799
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
1800
    unsigned iLit, i, RetValue;
Alan Mishchenko committed
1801 1802 1803 1804
    pObj = Kit_DsdNtkObj( pNtk, Id );
    if ( pObj == NULL )
        return 0;
    if ( pObj->Type == KIT_DSD_PRIME && (int)pObj->nFans > Size )
Alan Mishchenko committed
1805 1806
        return 1;
    RetValue = 0;
Alan Mishchenko committed
1807 1808
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        RetValue |= Kit_DsdFindLargeBox_rec( pNtk, Kit_DsdLit2Var(iLit), Size );
Alan Mishchenko committed
1809 1810 1811 1812 1813
    return RetValue;
}

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

Alan Mishchenko committed
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
  Synopsis    [Returns 1 if there is a component with more than 3 inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdFindLargeBox( Kit_DsdNtk_t * pNtk, int Size )
{
    return Kit_DsdFindLargeBox_rec( pNtk, Kit_DsdLit2Var(pNtk->Root), Size );
}

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

  Synopsis    [Returns 1 if the non-DSD 4-var func is implementable with two 3-LUTs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdRootNodeHasCommonVars( Kit_DsdObj_t * pObj0, Kit_DsdObj_t * pObj1 )
{
    unsigned i, k;
    for ( i = 0; i < pObj0->nFans; i++ )
    {
        if ( Kit_DsdLit2Var(pObj0->pFans[i]) >= 4 )
            continue;
        for ( k = 0; k < pObj1->nFans; k++ )
            if ( Kit_DsdLit2Var(pObj0->pFans[i]) == Kit_DsdLit2Var(pObj1->pFans[k]) )
                return 1;
    }
    return 0;
}

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

  Synopsis    [Returns 1 if the non-DSD 4-var func is implementable with two 3-LUTs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCheckVar4Dec2( Kit_DsdNtk_t * pNtk0, Kit_DsdNtk_t * pNtk1 )
{
    assert( pNtk0->nVars == 4 );
    assert( pNtk1->nVars == 4 );
    if ( Kit_DsdFindLargeBox(pNtk0, 2) )
        return 0;
    if ( Kit_DsdFindLargeBox(pNtk1, 2) )
        return 0;
    return Kit_DsdRootNodeHasCommonVars( Kit_DsdNtkRoot(pNtk0), Kit_DsdNtkRoot(pNtk1) );
}

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

Alan Mishchenko committed
1877 1878 1879 1880 1881 1882 1883 1884 1885
  Synopsis    [Performs decomposition of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1886
void Kit_DsdDecompose_rec( Kit_DsdNtk_t * pNtk, Kit_DsdObj_t * pObj, unsigned uSupp, unsigned char * pPar, int nDecMux )
Alan Mishchenko committed
1887
{
Alan Mishchenko committed
1888
    Kit_DsdObj_t * pRes, * pRes0, * pRes1;
Alan Mishchenko committed
1889
    int nWords = Kit_TruthWordNum(pObj->nFans);
Alan Mishchenko committed
1890
    unsigned * pTruth = Kit_DsdObjTruth(pObj);
Alan Mishchenko committed
1891 1892
    unsigned * pCofs2[2] = { pNtk->pMem, pNtk->pMem + nWords };
    unsigned * pCofs4[2][2] = { {pNtk->pMem + 2 * nWords, pNtk->pMem + 3 * nWords}, {pNtk->pMem + 4 * nWords, pNtk->pMem + 5 * nWords} };
Alan Mishchenko committed
1893
    int i, iLit0, iLit1, nFans0, nFans1, nPairs;
Alan Mishchenko committed
1894 1895 1896 1897 1898
    int fEquals[2][2], fOppos, fPairs[4][4];
    unsigned j, k, nFansNew, uSupp0, uSupp1;

    assert( pObj->nFans > 0 );
    assert( pObj->Type == KIT_DSD_PRIME );
Alan Mishchenko committed
1899
    assert( uSupp == (uSupp0 = (unsigned)Kit_TruthSupport(pTruth, pObj->nFans)) );
Alan Mishchenko committed
1900 1901

    // compress the truth table
Alan Mishchenko committed
1902
    if ( uSupp != Kit_BitMask(pObj->nFans) )
Alan Mishchenko committed
1903
    {
Alan Mishchenko committed
1904 1905
        nFansNew = Kit_WordCountOnes(uSupp);
        Kit_TruthShrink( pNtk->pMem, pTruth, nFansNew, pObj->nFans, uSupp, 1 );
Alan Mishchenko committed
1906
        for ( j = k = 0; j < pObj->nFans; j++ )
Alan Mishchenko committed
1907
            if ( uSupp & (1 << j) )
Alan Mishchenko committed
1908 1909 1910
                pObj->pFans[k++] = pObj->pFans[j];
        assert( k == nFansNew );
        pObj->nFans = k;
Alan Mishchenko committed
1911
        uSupp = Kit_BitMask(pObj->nFans);
Alan Mishchenko committed
1912 1913 1914 1915 1916 1917 1918
    }

    // consider the single variable case
    if ( pObj->nFans == 1 )
    {
        pObj->Type = KIT_DSD_NONE;
        if ( pTruth[0] == 0x55555555 )
Alan Mishchenko committed
1919
            pObj->pFans[0] = Kit_DsdLitNot(pObj->pFans[0]);
Alan Mishchenko committed
1920 1921 1922
        else
            assert( pTruth[0] == 0xAAAAAAAA );
        // update the parent pointer
Alan Mishchenko committed
1923
        *pPar = Kit_DsdLitNotCond( pObj->pFans[0], Kit_DsdLitIsCompl(*pPar) );
Alan Mishchenko committed
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
        return;
    }

    // decompose the output
    if ( !pObj->fMark )
    for ( i = pObj->nFans - 1; i >= 0; i-- )
    {
        // get the two-variable cofactors
        Kit_TruthCofactor0New( pCofs2[0], pTruth, pObj->nFans, i );
        Kit_TruthCofactor1New( pCofs2[1], pTruth, pObj->nFans, i );
//        assert( !Kit_TruthVarInSupport( pCofs2[0], pObj->nFans, i) );
//        assert( !Kit_TruthVarInSupport( pCofs2[1], pObj->nFans, i) );
        // get the constant cofs
        fEquals[0][0] = Kit_TruthIsConst0( pCofs2[0], pObj->nFans );
        fEquals[0][1] = Kit_TruthIsConst0( pCofs2[1], pObj->nFans );
        fEquals[1][0] = Kit_TruthIsConst1( pCofs2[0], pObj->nFans );
        fEquals[1][1] = Kit_TruthIsConst1( pCofs2[1], pObj->nFans );
        fOppos        = Kit_TruthIsOpposite( pCofs2[0], pCofs2[1], pObj->nFans );
        assert( !Kit_TruthIsEqual(pCofs2[0], pCofs2[1], pObj->nFans) );
        if ( fEquals[0][0] + fEquals[0][1] + fEquals[1][0] + fEquals[1][1] + fOppos == 0 )
        {
            // check the MUX decomposition
            uSupp0 = Kit_TruthSupport( pCofs2[0], pObj->nFans );
            uSupp1 = Kit_TruthSupport( pCofs2[1], pObj->nFans );
Alan Mishchenko committed
1948
            assert( uSupp == (uSupp0 | uSupp1 | (1<<i)) );
Alan Mishchenko committed
1949 1950 1951
            if ( uSupp0 & uSupp1 )
                continue;
            // perform MUX decomposition
Alan Mishchenko committed
1952 1953
            pRes0 = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, pObj->nFans );
            pRes1 = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, pObj->nFans );
Alan Mishchenko committed
1954 1955 1956 1957 1958
            for ( k = 0; k < pObj->nFans; k++ )
            {
                pRes0->pFans[k] = (uSupp0 & (1 << k))? pObj->pFans[k] : 127;
                pRes1->pFans[k] = (uSupp1 & (1 << k))? pObj->pFans[k] : 127;
            }
Alan Mishchenko committed
1959 1960
            Kit_TruthCopy( Kit_DsdObjTruth(pRes0), pCofs2[0], pObj->nFans );        
            Kit_TruthCopy( Kit_DsdObjTruth(pRes1), pCofs2[1], pObj->nFans ); 
Alan Mishchenko committed
1961
            // update the current one
Alan Mishchenko committed
1962 1963
            assert( pObj->Type == KIT_DSD_PRIME );
            pTruth[0] = 0xCACACACA;
Alan Mishchenko committed
1964
            pObj->nFans = 3;
Alan Mishchenko committed
1965
            pObj->pFans[2] = pObj->pFans[i];
Alan Mishchenko committed
1966
            pObj->pFans[0] = 2*pRes0->Id; pRes0->nRefs++;
Alan Mishchenko committed
1967
            pObj->pFans[1] = 2*pRes1->Id; pRes1->nRefs++;
Alan Mishchenko committed
1968
            // call recursively
Alan Mishchenko committed
1969 1970
            Kit_DsdDecompose_rec( pNtk, pRes0, uSupp0, pObj->pFans + 0, nDecMux );
            Kit_DsdDecompose_rec( pNtk, pRes1, uSupp1, pObj->pFans + 1, nDecMux );
Alan Mishchenko committed
1971 1972 1973 1974
            return;
        }

        // create the new node
Alan Mishchenko committed
1975
        pRes = Kit_DsdObjAlloc( pNtk, KIT_DSD_AND, 2 );
Alan Mishchenko committed
1976
        pRes->nRefs++;
Alan Mishchenko committed
1977
        pRes->nFans = 2;
Alan Mishchenko committed
1978
        pRes->pFans[0] = pObj->pFans[i];  pObj->pFans[i] = 127;  uSupp &= ~(1 << i);
Alan Mishchenko committed
1979 1980
        pRes->pFans[1] = 2*pObj->Id;
        // update the parent pointer
Alan Mishchenko committed
1981
        *pPar = Kit_DsdLitNotCond( 2 * pRes->Id, Kit_DsdLitIsCompl(*pPar) );
Alan Mishchenko committed
1982 1983 1984 1985 1986 1987 1988
        // consider different decompositions
        if ( fEquals[0][0] )
        {
            Kit_TruthCopy( pTruth, pCofs2[1], pObj->nFans );
        }
        else if ( fEquals[0][1] )
        {
Alan Mishchenko committed
1989
            pRes->pFans[0] = Kit_DsdLitNot(pRes->pFans[0]);
Alan Mishchenko committed
1990 1991 1992 1993
            Kit_TruthCopy( pTruth, pCofs2[0], pObj->nFans );
        }
        else if ( fEquals[1][0] )
        {
Alan Mishchenko committed
1994 1995
            *pPar = Kit_DsdLitNot(*pPar);
            pRes->pFans[1] = Kit_DsdLitNot(pRes->pFans[1]);
Alan Mishchenko committed
1996 1997 1998 1999
            Kit_TruthCopy( pTruth, pCofs2[1], pObj->nFans );
        }
        else if ( fEquals[1][1] )
        {
Alan Mishchenko committed
2000 2001 2002
            *pPar = Kit_DsdLitNot(*pPar);
            pRes->pFans[0] = Kit_DsdLitNot(pRes->pFans[0]);  
            pRes->pFans[1] = Kit_DsdLitNot(pRes->pFans[1]);
Alan Mishchenko committed
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
            Kit_TruthCopy( pTruth, pCofs2[0], pObj->nFans );
        }
        else if ( fOppos )
        {
            pRes->Type = KIT_DSD_XOR;
            Kit_TruthCopy( pTruth, pCofs2[0], pObj->nFans );
        }
        else
            assert( 0 );
        // decompose the remainder
Alan Mishchenko committed
2013
        assert( Kit_DsdObjTruth(pObj) == pTruth );
Alan Mishchenko committed
2014
        Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pRes->pFans + 1, nDecMux );
Alan Mishchenko committed
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
        return;
    }
    pObj->fMark = 1;

    // decompose the input
    for ( i = pObj->nFans - 1; i >= 0; i-- )
    {
        assert( Kit_TruthVarInSupport( pTruth, pObj->nFans, i ) );
        // get the single variale cofactors
        Kit_TruthCofactor0New( pCofs2[0], pTruth, pObj->nFans, i );
        Kit_TruthCofactor1New( pCofs2[1], pTruth, pObj->nFans, i );
        // check the existence of MUX decomposition
        uSupp0 = Kit_TruthSupport( pCofs2[0], pObj->nFans );
        uSupp1 = Kit_TruthSupport( pCofs2[1], pObj->nFans );
Alan Mishchenko committed
2029 2030
        assert( uSupp == (uSupp0 | uSupp1 | (1<<i)) );
        // if one of the cofs is a constant, it is time to check the output again
Alan Mishchenko committed
2031 2032 2033
        if ( uSupp0 == 0 || uSupp1 == 0 )
        {
            pObj->fMark = 0;
Alan Mishchenko committed
2034
            Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pPar, nDecMux );
Alan Mishchenko committed
2035 2036 2037 2038 2039 2040 2041 2042 2043
            return;
        }
        assert( uSupp0 && uSupp1 );
        // get the number of unique variables
        nFans0 = Kit_WordCountOnes( uSupp0 & ~uSupp1 );
        nFans1 = Kit_WordCountOnes( uSupp1 & ~uSupp0 );
        if ( nFans0 == 1 && nFans1 == 1 )
        {
            // get the cofactors w.r.t. the unique variables
Alan Mishchenko committed
2044 2045
            iLit0 = Kit_WordFindFirstBit( uSupp0 & ~uSupp1 );
            iLit1 = Kit_WordFindFirstBit( uSupp1 & ~uSupp0 );
Alan Mishchenko committed
2046
            // get four cofactors                                        
Alan Mishchenko committed
2047 2048 2049 2050
            Kit_TruthCofactor0New( pCofs4[0][0], pCofs2[0], pObj->nFans, iLit0 );
            Kit_TruthCofactor1New( pCofs4[0][1], pCofs2[0], pObj->nFans, iLit0 );
            Kit_TruthCofactor0New( pCofs4[1][0], pCofs2[1], pObj->nFans, iLit1 );
            Kit_TruthCofactor1New( pCofs4[1][1], pCofs2[1], pObj->nFans, iLit1 );
Alan Mishchenko committed
2051 2052 2053 2054 2055 2056 2057 2058
            // check existence conditions
            fEquals[0][0] = Kit_TruthIsEqual( pCofs4[0][0], pCofs4[1][0], pObj->nFans );
            fEquals[0][1] = Kit_TruthIsEqual( pCofs4[0][1], pCofs4[1][1], pObj->nFans );
            fEquals[1][0] = Kit_TruthIsEqual( pCofs4[0][0], pCofs4[1][1], pObj->nFans );
            fEquals[1][1] = Kit_TruthIsEqual( pCofs4[0][1], pCofs4[1][0], pObj->nFans );
            if ( (fEquals[0][0] && fEquals[0][1]) || (fEquals[1][0] && fEquals[1][1]) )
            {
                // construct the MUX
Alan Mishchenko committed
2059 2060
                pRes = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, 3 );
                Kit_DsdObjTruth(pRes)[0] = 0xCACACACA;
Alan Mishchenko committed
2061
                pRes->nRefs++;
Alan Mishchenko committed
2062
                pRes->nFans = 3;
Alan Mishchenko committed
2063 2064 2065
                pRes->pFans[0] = pObj->pFans[iLit0]; pObj->pFans[iLit0] = 127;  uSupp &= ~(1 << iLit0);
                pRes->pFans[1] = pObj->pFans[iLit1]; pObj->pFans[iLit1] = 127;  uSupp &= ~(1 << iLit1);
                pRes->pFans[2] = pObj->pFans[i];     pObj->pFans[i] = 2 * pRes->Id; // remains in support
Alan Mishchenko committed
2066
                // update the node
Alan Mishchenko committed
2067
//                if ( fEquals[0][0] && fEquals[0][1] )
Alan Mishchenko committed
2068
//                    Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[0][1], pObj->nFans, i );
Alan Mishchenko committed
2069
//                else
Alan Mishchenko committed
2070 2071
//                    Kit_TruthMuxVar( pTruth, pCofs4[0][1], pCofs4[0][0], pObj->nFans, i );
                Kit_TruthMuxVar( pTruth, pCofs4[1][0], pCofs4[1][1], pObj->nFans, i );
Alan Mishchenko committed
2072 2073
                if ( fEquals[1][0] && fEquals[1][1] )
                    pRes->pFans[0] = Kit_DsdLitNot(pRes->pFans[0]);
Alan Mishchenko committed
2074
                // decompose the remainder
Alan Mishchenko committed
2075
                Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pPar, nDecMux );
Alan Mishchenko committed
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
                return;
            }
        }

        // try other inputs
        for ( k = i+1; k < pObj->nFans; k++ )
        {
            // get four cofactors                                                ik
            Kit_TruthCofactor0New( pCofs4[0][0], pCofs2[0], pObj->nFans, k ); // 00 
            Kit_TruthCofactor1New( pCofs4[0][1], pCofs2[0], pObj->nFans, k ); // 01 
            Kit_TruthCofactor0New( pCofs4[1][0], pCofs2[1], pObj->nFans, k ); // 10 
            Kit_TruthCofactor1New( pCofs4[1][1], pCofs2[1], pObj->nFans, k ); // 11 
            // compare equal pairs
Alan Mishchenko committed
2089 2090 2091 2092 2093 2094
            fPairs[0][1] = fPairs[1][0] = Kit_TruthIsEqual( pCofs4[0][0], pCofs4[0][1], pObj->nFans );
            fPairs[0][2] = fPairs[2][0] = Kit_TruthIsEqual( pCofs4[0][0], pCofs4[1][0], pObj->nFans );
            fPairs[0][3] = fPairs[3][0] = Kit_TruthIsEqual( pCofs4[0][0], pCofs4[1][1], pObj->nFans );
            fPairs[1][2] = fPairs[2][1] = Kit_TruthIsEqual( pCofs4[0][1], pCofs4[1][0], pObj->nFans );
            fPairs[1][3] = fPairs[3][1] = Kit_TruthIsEqual( pCofs4[0][1], pCofs4[1][1], pObj->nFans );
            fPairs[2][3] = fPairs[3][2] = Kit_TruthIsEqual( pCofs4[1][0], pCofs4[1][1], pObj->nFans );
Alan Mishchenko committed
2095 2096 2097 2098 2099
            nPairs = fPairs[0][1] + fPairs[0][2] + fPairs[0][3] + fPairs[1][2] + fPairs[1][3] + fPairs[2][3];
            if ( nPairs != 3 && nPairs != 2 )
                continue;

            // decomposition exists
Alan Mishchenko committed
2100
            pRes = Kit_DsdObjAlloc( pNtk, KIT_DSD_AND, 2 );
Alan Mishchenko committed
2101
            pRes->nRefs++;
Alan Mishchenko committed
2102
            pRes->nFans = 2;
Alan Mishchenko committed
2103 2104
            pRes->pFans[0] = pObj->pFans[k]; pObj->pFans[k] = 2 * pRes->Id;  // remains in support
            pRes->pFans[1] = pObj->pFans[i]; pObj->pFans[i] = 127;       uSupp &= ~(1 << i);
Alan Mishchenko committed
2105 2106
            if ( !fPairs[0][1] && !fPairs[0][2] && !fPairs[0][3] ) // 00
            {
Alan Mishchenko committed
2107 2108
                pRes->pFans[0] = Kit_DsdLitNot(pRes->pFans[0]);  
                pRes->pFans[1] = Kit_DsdLitNot(pRes->pFans[1]);
Alan Mishchenko committed
2109
                Kit_TruthMuxVar( pTruth, pCofs4[1][1], pCofs4[0][0], pObj->nFans, k );
Alan Mishchenko committed
2110 2111 2112
            }
            else if ( !fPairs[1][0] && !fPairs[1][2] && !fPairs[1][3] ) // 01
            {
Alan Mishchenko committed
2113
                pRes->pFans[1] = Kit_DsdLitNot(pRes->pFans[1]);  
Alan Mishchenko committed
2114
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[0][1], pObj->nFans, k );
Alan Mishchenko committed
2115 2116 2117
            }
            else if ( !fPairs[2][0] && !fPairs[2][1] && !fPairs[2][3] ) // 10
            {
Alan Mishchenko committed
2118
                pRes->pFans[0] = Kit_DsdLitNot(pRes->pFans[0]);  
Alan Mishchenko committed
2119
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[1][0], pObj->nFans, k );
Alan Mishchenko committed
2120 2121 2122 2123 2124 2125 2126 2127
            }
            else if ( !fPairs[3][0] && !fPairs[3][1] && !fPairs[3][2] ) // 11
            {
//                unsigned uSupp0 = Kit_TruthSupport(pCofs4[0][0], pObj->nFans);
//                unsigned uSupp1 = Kit_TruthSupport(pCofs4[1][1], pObj->nFans);
//                unsigned uSupp;
//                Extra_PrintBinary( stdout, &uSupp0, pObj->nFans ); printf( "\n" );
//                Extra_PrintBinary( stdout, &uSupp1, pObj->nFans ); printf( "\n" );
Alan Mishchenko committed
2128
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[1][1], pObj->nFans, k );
Alan Mishchenko committed
2129 2130 2131 2132 2133 2134 2135
//                uSupp = Kit_TruthSupport(pTruth, pObj->nFans);
//                Extra_PrintBinary( stdout, &uSupp, pObj->nFans ); printf( "\n" ); printf( "\n" );
            }
            else
            {
                assert( fPairs[0][3] && fPairs[1][2] );
                pRes->Type = KIT_DSD_XOR;;
Alan Mishchenko committed
2136
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[0][1], pObj->nFans, k );
Alan Mishchenko committed
2137 2138
            }
            // decompose the remainder
Alan Mishchenko committed
2139
            Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pPar, nDecMux );
Alan Mishchenko committed
2140 2141 2142
            return;
        }
    }
Alan Mishchenko committed
2143

Alan Mishchenko committed
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
    // if all decomposition methods failed and we are still above the limit, perform MUX-decomposition
    if ( nDecMux > 0 && (int)pObj->nFans > nDecMux )
    {
        int iBestVar = Kit_TruthBestCofVar( pTruth, pObj->nFans, pCofs2[0], pCofs2[1] );
        uSupp0 = Kit_TruthSupport( pCofs2[0], pObj->nFans );
        uSupp1 = Kit_TruthSupport( pCofs2[1], pObj->nFans );
        // perform MUX decomposition
        pRes0 = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, pObj->nFans );
        pRes1 = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, pObj->nFans );
        for ( k = 0; k < pObj->nFans; k++ )
            pRes0->pFans[k] = pRes1->pFans[k] = pObj->pFans[k];
        Kit_TruthCopy( Kit_DsdObjTruth(pRes0), pCofs2[0], pObj->nFans );        
        Kit_TruthCopy( Kit_DsdObjTruth(pRes1), pCofs2[1], pObj->nFans ); 
        // update the current one
        assert( pObj->Type == KIT_DSD_PRIME );
        pTruth[0] = 0xCACACACA;
        pObj->nFans = 3;
Alan Mishchenko committed
2161
        pObj->pFans[2] = pObj->pFans[iBestVar];
Alan Mishchenko committed
2162 2163 2164 2165 2166 2167
        pObj->pFans[0] = 2*pRes0->Id; pRes0->nRefs++;
        pObj->pFans[1] = 2*pRes1->Id; pRes1->nRefs++;
        // call recursively
        Kit_DsdDecompose_rec( pNtk, pRes0, uSupp0, pObj->pFans + 0, nDecMux );
        Kit_DsdDecompose_rec( pNtk, pRes1, uSupp1, pObj->pFans + 1, nDecMux );
    }
Alan Mishchenko committed
2168

Alan Mishchenko committed
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2182
Kit_DsdNtk_t * Kit_DsdDecomposeInt( unsigned * pTruth, int nVars, int nDecMux )
Alan Mishchenko committed
2183
{
Alan Mishchenko committed
2184 2185
    Kit_DsdNtk_t * pNtk;
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
2186
    unsigned uSupp;
Alan Mishchenko committed
2187 2188
    int i, nVarsReal;
    assert( nVars <= 16 );
Alan Mishchenko committed
2189
    pNtk = Kit_DsdNtkAlloc( nVars );
Alan Mishchenko committed
2190
    pNtk->Root = Kit_DsdVar2Lit( pNtk->nVars, 0 );
Alan Mishchenko committed
2191
    // create the first node
Alan Mishchenko committed
2192
    pObj = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, nVars );
Alan Mishchenko committed
2193
    assert( pNtk->pNodes[0] == pObj );
Alan Mishchenko committed
2194
    for ( i = 0; i < nVars; i++ )
Alan Mishchenko committed
2195 2196
       pObj->pFans[i] = Kit_DsdVar2Lit( i, 0 );
    Kit_TruthCopy( Kit_DsdObjTruth(pObj), pTruth, nVars );
Alan Mishchenko committed
2197
    uSupp = Kit_TruthSupport( pTruth, nVars );
Alan Mishchenko committed
2198
    // consider special cases
Alan Mishchenko committed
2199
    nVarsReal = Kit_WordCountOnes( uSupp );
Alan Mishchenko committed
2200 2201 2202 2203
    if ( nVarsReal == 0 )
    {
        pObj->Type = KIT_DSD_CONST1;
        pObj->nFans = 0;
Alan Mishchenko committed
2204
        if ( pTruth[0] == 0 )
Alan Mishchenko committed
2205
             pNtk->Root = Kit_DsdLitNot(pNtk->Root);
Alan Mishchenko committed
2206 2207 2208 2209 2210 2211
        return pNtk;
    }
    if ( nVarsReal == 1 )
    {
        pObj->Type = KIT_DSD_VAR;
        pObj->nFans = 1;
Alan Mishchenko committed
2212
        pObj->pFans[0] = Kit_DsdVar2Lit( Kit_WordFindFirstBit(uSupp), (pTruth[0] & 1) );
Alan Mishchenko committed
2213 2214
        return pNtk;
    }
Alan Mishchenko committed
2215
    Kit_DsdDecompose_rec( pNtk, pNtk->pNodes[0], uSupp, &pNtk->Root, nDecMux );
Alan Mishchenko committed
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
    return pNtk;
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2230 2231 2232 2233 2234 2235 2236 2237 2238
Kit_DsdNtk_t * Kit_DsdDecompose( unsigned * pTruth, int nVars )
{
    return Kit_DsdDecomposeInt( pTruth, nVars, 0 );
}

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

  Synopsis    [Performs decomposition of the truth table.]

Alan Mishchenko committed
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Kit_DsdNtk_t * Kit_DsdDecomposeExpand( unsigned * pTruth, int nVars )
{
    Kit_DsdNtk_t * pNtk, * pTemp;
    pNtk = Kit_DsdDecomposeInt( pTruth, nVars, 0 );
    pNtk = Kit_DsdExpand( pTemp = pNtk );      
    Kit_DsdNtkFree( pTemp );
    return pNtk;
}

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

  Synopsis    [Performs decomposition of the truth table.]

Alan Mishchenko committed
2259 2260 2261 2262 2263 2264 2265 2266 2267
  Description [Uses MUXes to break-down large prime nodes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Kit_DsdNtk_t *  Kit_DsdDecomposeMux( unsigned * pTruth, int nVars, int nDecMux )
{
Alan Mishchenko committed
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
/*
    Kit_DsdNtk_t * pNew;
    Kit_DsdObj_t * pObjNew;
    assert( nVars <= 16 );
    // create a new network
    pNew = Kit_DsdNtkAlloc( nVars );
    // consider simple special cases
    if ( nVars == 0 )
    {
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_CONST1, 0 );
        pNew->Root = Kit_DsdVar2Lit( pObjNew->Id, (int)(pTruth[0] == 0) );
        return pNew;
    }
    if ( nVars == 1 )
    {
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_VAR, 1 );
        pObjNew->pFans[0] = Kit_DsdVar2Lit( 0, 0 );
        pNew->Root = Kit_DsdVar2Lit( pObjNew->Id, (int)(pTruth[0] != 0xAAAAAAAA) );
        return pNew;
    }
*/
Alan Mishchenko committed
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
    return Kit_DsdDecomposeInt( pTruth, nVars, nDecMux );
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2303
int Kit_DsdTestCofs( Kit_DsdNtk_t * pNtk, unsigned * pTruthInit )
Alan Mishchenko committed
2304
{
Alan Mishchenko committed
2305 2306
    Kit_DsdNtk_t * pNtk0, * pNtk1, * pTemp;
//    Kit_DsdObj_t * pRoot;
Alan Mishchenko committed
2307 2308
    unsigned * pCofs2[2] = { pNtk->pMem, pNtk->pMem + Kit_TruthWordNum(pNtk->nVars) };
    unsigned i, * pTruth;
Alan Mishchenko committed
2309
    int fVerbose = 1;
Alan Mishchenko committed
2310
    int RetValue = 0;
Alan Mishchenko committed
2311

Alan Mishchenko committed
2312
    pTruth = pTruthInit;
Alan Mishchenko committed
2313 2314
//    pRoot = Kit_DsdNtkRoot(pNtk);
//    pTruth = Kit_DsdObjTruth(pRoot);
Alan Mishchenko committed
2315
//    assert( pRoot->nFans == pNtk->nVars );
Alan Mishchenko committed
2316 2317 2318 2319 2320 2321 2322

    if ( fVerbose )
    {
        printf( "Function: " );
//        Extra_PrintBinary( stdout, pTruth, (1 << pNtk->nVars) ); 
        Extra_PrintHexadecimal( stdout, pTruth, pNtk->nVars ); 
        printf( "\n" );
2323
        Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2324 2325 2326 2327 2328
    }
    for ( i = 0; i < pNtk->nVars; i++ )
    {
        Kit_TruthCofactor0New( pCofs2[0], pTruth, pNtk->nVars, i );
        pNtk0 = Kit_DsdDecompose( pCofs2[0], pNtk->nVars );
Alan Mishchenko committed
2329 2330 2331
        pNtk0 = Kit_DsdExpand( pTemp = pNtk0 );
        Kit_DsdNtkFree( pTemp );

Alan Mishchenko committed
2332 2333 2334
        if ( fVerbose )
        {
            printf( "Cof%d0: ", i );
2335
            Kit_DsdPrint( stdout, pNtk0 ), printf( "\n" );
Alan Mishchenko committed
2336 2337 2338 2339
        }

        Kit_TruthCofactor1New( pCofs2[1], pTruth, pNtk->nVars, i );
        pNtk1 = Kit_DsdDecompose( pCofs2[1], pNtk->nVars );
Alan Mishchenko committed
2340 2341 2342
        pNtk1 = Kit_DsdExpand( pTemp = pNtk1 );
        Kit_DsdNtkFree( pTemp );

Alan Mishchenko committed
2343 2344 2345
        if ( fVerbose )
        {
            printf( "Cof%d1: ", i );
2346
            Kit_DsdPrint( stdout, pNtk1 ), printf( "\n" );
Alan Mishchenko committed
2347
        }
Alan Mishchenko committed
2348

Alan Mishchenko committed
2349 2350
//        if ( Kit_DsdCheckVar4Dec2( pNtk0, pNtk1 ) )
//            RetValue = 1;
Alan Mishchenko committed
2351

Alan Mishchenko committed
2352
        Kit_DsdNtkFree( pNtk0 );
Alan Mishchenko committed
2353
        Kit_DsdNtkFree( pNtk1 );
Alan Mishchenko committed
2354 2355 2356
    }
    if ( fVerbose )
        printf( "\n" );
Alan Mishchenko committed
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386

    return RetValue;
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdEval( unsigned * pTruth, int nVars, int nLutSize )
{
    Kit_DsdMan_t * p;
    Kit_DsdNtk_t * pNtk;
    unsigned * pTruthC;
    int Result;

    // decompose the function
    pNtk = Kit_DsdDecompose( pTruth, nVars );
    Result = Kit_DsdCountLuts( pNtk, nLutSize );
//    printf( "\n" );
//    Kit_DsdPrint( stdout, pNtk );
//    printf( "Eval = %d.\n", Result );

    // recompute the truth table
Alan Mishchenko committed
2387
    p = Kit_DsdManAlloc( nVars, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2388
    pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2389
    if ( !Kit_TruthIsEqual( pTruth, pTruthC, nVars ) )
Alan Mishchenko committed
2390 2391 2392 2393 2394
        printf( "Verification failed.\n" );
    Kit_DsdManFree( p );

    Kit_DsdNtkFree( pNtk );
    return Result;
Alan Mishchenko committed
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2408
void Kit_DsdVerify( Kit_DsdNtk_t * pNtk, unsigned * pTruth, int nVars )
Alan Mishchenko committed
2409
{
Alan Mishchenko committed
2410 2411
    Kit_DsdMan_t * p;
    unsigned * pTruthC;
Alan Mishchenko committed
2412
    p = Kit_DsdManAlloc( nVars, Kit_DsdNtkObjNum(pNtk)+2 );
Alan Mishchenko committed
2413
    pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
    if ( !Extra_TruthIsEqual( pTruth, pTruthC, nVars ) )
        printf( "Verification failed.\n" );
    Kit_DsdManFree( p );
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2430 2431
void Kit_DsdTest( unsigned * pTruth, int nVars )
{
Alan Mishchenko committed
2432 2433 2434
    Kit_DsdMan_t * p;
    unsigned * pTruthC;
    Kit_DsdNtk_t * pNtk, * pTemp;
Alan Mishchenko committed
2435 2436
    pNtk = Kit_DsdDecompose( pTruth, nVars );

Alan Mishchenko committed
2437
//    if ( Kit_DsdFindLargeBox(pNtk, Kit_DsdLit2Var(pNtk->Root)) )
Alan Mishchenko committed
2438 2439
//        Kit_DsdPrint( stdout, pNtk );

Alan Mishchenko committed
2440 2441
//    if ( Kit_DsdNtkRoot(pNtk)->nFans == (unsigned)nVars && nVars == 6 )

Alan Mishchenko committed
2442 2443
//    printf( "\n" );
//    Kit_DsdPrint( stdout, pNtk );
Alan Mishchenko committed
2444 2445 2446 2447

    pNtk = Kit_DsdExpand( pTemp = pNtk );
    Kit_DsdNtkFree( pTemp );

2448
    Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2449 2450 2451 2452 2453

//    if ( Kit_DsdFindLargeBox(pNtk, Kit_DsdLit2Var(pNtk->Root)) )
//        Kit_DsdTestCofs( pNtk, pTruth );

    // recompute the truth table
Alan Mishchenko committed
2454
    p = Kit_DsdManAlloc( nVars, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2455
    pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2456 2457 2458 2459 2460 2461 2462 2463 2464
//    Extra_PrintBinary( stdout, pTruth, 1 << nVars ); printf( "\n" );
//    Extra_PrintBinary( stdout, pTruthC, 1 << nVars ); printf( "\n" );
    if ( Extra_TruthIsEqual( pTruth, pTruthC, nVars ) )
    {
//        printf( "Verification is okay.\n" );
    }
    else
        printf( "Verification failed.\n" );
    Kit_DsdManFree( p );
Alan Mishchenko committed
2465

Alan Mishchenko committed
2466 2467 2468 2469

    Kit_DsdNtkFree( pNtk );
}

Alan Mishchenko committed
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
/**Function*************************************************************

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdPrecompute4Vars()
{
    Kit_DsdMan_t * p;
    Kit_DsdNtk_t * pNtk, * pTemp;
    FILE * pFile;
    unsigned uTruth;
    unsigned * pTruthC;
    char Buffer[256];
    int i, RetValue;
    int Counter1 = 0, Counter2 = 0;

    pFile = fopen( "5npn/npn4.txt", "r" );
    for ( i = 0; fgets( Buffer, 100, pFile ); i++ )
    {
        Buffer[6] = 0;
        Extra_ReadHexadecimal( &uTruth, Buffer+2, 4 );
        uTruth = ((uTruth & 0xffff) << 16) | (uTruth & 0xffff);
        pNtk = Kit_DsdDecompose( &uTruth, 4 );

        pNtk = Kit_DsdExpand( pTemp = pNtk );
        Kit_DsdNtkFree( pTemp );


        if ( Kit_DsdFindLargeBox(pNtk, 3) )
        {
//            RetValue = 0;
            RetValue = Kit_DsdTestCofs( pNtk, &uTruth );
            printf( "\n" );
            printf( "%3d : Non-DSD function  %s  %s\n", i, Buffer + 2, RetValue? "implementable" : "" );
2510
            Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523

            Counter1++;
            Counter2 += RetValue;
        }

/*
        printf( "%3d : Function  %s   ", i, Buffer + 2 );
        if ( !Kit_DsdFindLargeBox(pNtk, 3) )
            Kit_DsdPrint( stdout, pNtk );
        else
            printf( "\n" );
*/

Alan Mishchenko committed
2524
        p = Kit_DsdManAlloc( 4, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2525
        pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
        if ( !Extra_TruthIsEqual( &uTruth, pTruthC, 4 ) )
            printf( "Verification failed.\n" );
        Kit_DsdManFree( p );

        Kit_DsdNtkFree( pNtk );
    }
    fclose( pFile );
    printf( "non-DSD = %d   implementable = %d\n", Counter1, Counter2 );
}

Alan Mishchenko committed
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595

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

  Synopsis    [Returns the set of cofactoring variables.]

  Description [If there is no DSD components returns 0.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCofactoringGetVars( Kit_DsdNtk_t ** ppNtk, int nSize, int * pVars )
{
    Kit_DsdObj_t * pObj;
    unsigned m;
    int i, k, v, Var, nVars, iFaninLit;
    // go through all the networks
    nVars = 0;
    for ( i = 0; i < nSize; i++ )
    {
        // go through the prime objects of each networks
        Kit_DsdNtkForEachObj( ppNtk[i], pObj, k )     
        {
            if ( pObj->Type != KIT_DSD_PRIME )
                continue;
            if ( pObj->nFans == 3 )
                continue;
            // collect direct fanin variables
            Kit_DsdObjForEachFanin( ppNtk[i], pObj, iFaninLit, m )
            {
                if ( !Kit_DsdLitIsLeaf(ppNtk[i], iFaninLit) )
                    continue;
                // add it to the array
                Var = Kit_DsdLit2Var( iFaninLit );
                for ( v = 0; v < nVars; v++ )
                    if ( pVars[v] == Var )
                        break;
                if ( v == nVars )
                    pVars[nVars++] = Var;
            }
        }
    }
    return nVars;
}

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

  Synopsis    [Canonical decomposition into completely DSD-structure.]

  Description [Returns the number of cofactoring steps. Also returns
  the cofactoring variables in pVars.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCofactoring( unsigned * pTruth, int nVars, int * pCofVars, int nLimit, int fVerbose )
{
Alan Mishchenko committed
2596 2597 2598 2599 2600 2601 2602 2603
    Kit_DsdNtk_t * ppNtks[5][16] = {
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
    };
    Kit_DsdNtk_t * pTemp;
Alan Mishchenko committed
2604 2605 2606 2607 2608 2609 2610 2611 2612
    unsigned * ppCofs[5][16];
    int pTryVars[16], nTryVars;
    int nPrimeSizeMin, nPrimeSizeMax, nPrimeSizeCur;
    int nSuppSizeMin, nSuppSizeMax, iVarBest;
    int i, k, v, nStep, nSize, nMemSize;
    assert( nLimit < 5 );

    // allocate storage for cofactors
    nMemSize = Kit_TruthWordNum(nVars);
Alan Mishchenko committed
2613
    ppCofs[0][0] = ABC_ALLOC( unsigned, 80 * nMemSize );
Alan Mishchenko committed
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
    nSize = 0;
    for ( i = 0; i <  5; i++ )
    for ( k = 0; k < 16; k++ )
        ppCofs[i][k] = ppCofs[0][0] + nMemSize * nSize++;
    assert( nSize == 80 );

    // copy the function
    Kit_TruthCopy( ppCofs[0][0], pTruth, nVars );
    ppNtks[0][0] = Kit_DsdDecompose( ppCofs[0][0], nVars );

    if ( fVerbose )
        printf( "\nProcessing prime function with %d support variables:\n", nVars );

    // perform recursive cofactoring
    for ( nStep = 0; nStep < nLimit; nStep++ )
    {
        nSize = (1 << nStep);
        // find the variables to use in the cofactoring step
        nTryVars = Kit_DsdCofactoringGetVars( ppNtks[nStep], nSize, pTryVars );
        if ( nTryVars == 0 )
            break;
        // cofactor w.r.t. the above variables
        iVarBest = -1;
        nPrimeSizeMin = 10000;
        nSuppSizeMin  = 10000;
        for ( v = 0; v < nTryVars; v++ )
        {
            nPrimeSizeMax = 0;
            nSuppSizeMax = 0;
            for ( i = 0; i < nSize; i++ )
            {
                // cofactor and decompose cofactors          
                Kit_TruthCofactor0New( ppCofs[nStep+1][2*i+0], ppCofs[nStep][i], nVars, pTryVars[v] );
                Kit_TruthCofactor1New( ppCofs[nStep+1][2*i+1], ppCofs[nStep][i], nVars, pTryVars[v] );
                ppNtks[nStep+1][2*i+0] = Kit_DsdDecompose( ppCofs[nStep+1][2*i+0], nVars );
                ppNtks[nStep+1][2*i+1] = Kit_DsdDecompose( ppCofs[nStep+1][2*i+1], nVars );
                // compute the largest non-decomp block
                nPrimeSizeCur  = Kit_DsdNonDsdSizeMax(ppNtks[nStep+1][2*i+0]);
                nPrimeSizeMax  = KIT_MAX( nPrimeSizeMax, nPrimeSizeCur );
                nPrimeSizeCur  = Kit_DsdNonDsdSizeMax(ppNtks[nStep+1][2*i+1]);
                nPrimeSizeMax  = KIT_MAX( nPrimeSizeMax, nPrimeSizeCur );
                // compute the sum total of supports
                nSuppSizeMax += Kit_TruthSupportSize( ppCofs[nStep+1][2*i+0], nVars );
                nSuppSizeMax += Kit_TruthSupportSize( ppCofs[nStep+1][2*i+1], nVars );
Alan Mishchenko committed
2658
                // free the networks
Alan Mishchenko committed
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
                Kit_DsdNtkFree( ppNtks[nStep+1][2*i+0] );
                Kit_DsdNtkFree( ppNtks[nStep+1][2*i+1] );
            }
            // find the min max support size of the prime component
            if ( nPrimeSizeMin > nPrimeSizeMax || (nPrimeSizeMin == nPrimeSizeMax && nSuppSizeMin > nSuppSizeMax) )
            {
                nPrimeSizeMin = nPrimeSizeMax;
                nSuppSizeMin  = nSuppSizeMax;
                iVarBest      = pTryVars[v];
            }
        }
        assert( iVarBest != -1 );
        // save the variable
        if ( pCofVars )
            pCofVars[nStep] = iVarBest;
        // cofactor w.r.t. the best
        for ( i = 0; i < nSize; i++ )
        {
            Kit_TruthCofactor0New( ppCofs[nStep+1][2*i+0], ppCofs[nStep][i], nVars, iVarBest );
            Kit_TruthCofactor1New( ppCofs[nStep+1][2*i+1], ppCofs[nStep][i], nVars, iVarBest );
            ppNtks[nStep+1][2*i+0] = Kit_DsdDecompose( ppCofs[nStep+1][2*i+0], nVars );
            ppNtks[nStep+1][2*i+1] = Kit_DsdDecompose( ppCofs[nStep+1][2*i+1], nVars );
            if ( fVerbose )
            {
                ppNtks[nStep+1][2*i+0] = Kit_DsdExpand( pTemp = ppNtks[nStep+1][2*i+0] );
                Kit_DsdNtkFree( pTemp );
                ppNtks[nStep+1][2*i+1] = Kit_DsdExpand( pTemp = ppNtks[nStep+1][2*i+1] );
                Kit_DsdNtkFree( pTemp );

                printf( "Cof%d%d: ", nStep+1, 2*i+0 );
2689
                Kit_DsdPrint( stdout, ppNtks[nStep+1][2*i+0] ), printf( "\n" );
Alan Mishchenko committed
2690
                printf( "Cof%d%d: ", nStep+1, 2*i+1 );
2691
                Kit_DsdPrint( stdout, ppNtks[nStep+1][2*i+1] ), printf( "\n" );
Alan Mishchenko committed
2692 2693 2694 2695
            }
        }
    }

Alan Mishchenko committed
2696
    // free the networks
Alan Mishchenko committed
2697 2698 2699 2700
    for ( i = 0; i <  5; i++ )
    for ( k = 0; k < 16; k++ )
        if ( ppNtks[i][k] )
            Kit_DsdNtkFree( ppNtks[i][k] );
Alan Mishchenko committed
2701
    ABC_FREE( ppCofs[0][0] );
Alan Mishchenko committed
2702 2703 2704 2705 2706

    assert( nStep <= nLimit );
    return nStep;
}

Alan Mishchenko committed
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
/**Function*************************************************************

  Synopsis    [Canonical decomposition into completely DSD-structure.]

  Description [Returns the number of cofactoring steps. Also returns
  the cofactoring variables in pVars.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Kit_DsdPrintCofactors( unsigned * pTruth, int nVars, int nCofLevel, int fVerbose )
{
    Kit_DsdNtk_t * ppNtks[32] = {0}, * pTemp;
    unsigned * ppCofs[5][16];
    int piCofVar[5];
    int nPrimeSizeMax, nPrimeSizeCur, nSuppSizeMax;
    int i, k, v1, v2, v3, v4, s, nSteps, nSize, nMemSize;
    assert( nCofLevel < 5 );

    // print the function
    ppNtks[0] = Kit_DsdDecompose( pTruth, nVars );
    ppNtks[0] = Kit_DsdExpand( pTemp = ppNtks[0] );
    Kit_DsdNtkFree( pTemp );
    if ( fVerbose )
2733
        Kit_DsdPrint( stdout, ppNtks[0] ), printf( "\n" );
Alan Mishchenko committed
2734 2735 2736 2737
    Kit_DsdNtkFree( ppNtks[0] );

    // allocate storage for cofactors
    nMemSize = Kit_TruthWordNum(nVars);
Alan Mishchenko committed
2738
    ppCofs[0][0] = ABC_ALLOC( unsigned, 80 * nMemSize );
Alan Mishchenko committed
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
    nSize = 0;
    for ( i = 0; i <  5; i++ )
    for ( k = 0; k < 16; k++ )
        ppCofs[i][k] = ppCofs[0][0] + nMemSize * nSize++;
    assert( nSize == 80 );

    // copy the function
    Kit_TruthCopy( ppCofs[0][0], pTruth, nVars );

    if ( nCofLevel == 1 )
    for ( v1 = 0; v1 < nVars; v1++ )
    {
        nSteps = 0;
        piCofVar[nSteps++] = v1;

        printf( "    Variables { " );
        for ( i = 0; i < nSteps; i++ )
            printf( "%c ", 'a' + piCofVar[i] );
        printf( "}\n" );

        // single cofactors
        for ( s = 1; s <= nSteps; s++ )
        {
            for ( k = 0; k < s; k++ )
            {
                nSize = (1 << k);
                for ( i = 0; i < nSize; i++ )
                {
                    Kit_TruthCofactor0New( ppCofs[k+1][2*i+0], ppCofs[k][i], nVars, piCofVar[k] );
                    Kit_TruthCofactor1New( ppCofs[k+1][2*i+1], ppCofs[k][i], nVars, piCofVar[k] );
                }
            }
        }
        // compute DSD networks
        nSize = (1 << nSteps);
        nPrimeSizeMax = 0;
        nSuppSizeMax = 0;
        for ( i = 0; i < nSize; i++ )
        {
            ppNtks[i] = Kit_DsdDecompose( ppCofs[nSteps][i], nVars );
            ppNtks[i] = Kit_DsdExpand( pTemp = ppNtks[i] );
            Kit_DsdNtkFree( pTemp );
            if ( fVerbose )
            {
                printf( "Cof%d%d: ", nSteps, i );
2784
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
            }
            // compute the largest non-decomp block
            nPrimeSizeCur  = Kit_DsdNonDsdSizeMax(ppNtks[i]);
            nPrimeSizeMax  = KIT_MAX( nPrimeSizeMax, nPrimeSizeCur );
            Kit_DsdNtkFree( ppNtks[i] );
            nSuppSizeMax += Kit_TruthSupportSize( ppCofs[nSteps][i], nVars );
        }
        printf( "Max = %2d. Supps = %2d.\n", nPrimeSizeMax, nSuppSizeMax );
    }

    if ( nCofLevel == 2 )
    for ( v1 = 0; v1 < nVars; v1++ )
    for ( v2 = v1+1; v2 < nVars; v2++ )
    {
        nSteps = 0;
        piCofVar[nSteps++] = v1;
        piCofVar[nSteps++] = v2;

        printf( "    Variables { " );
        for ( i = 0; i < nSteps; i++ )
            printf( "%c ", 'a' + piCofVar[i] );
        printf( "}\n" );

        // single cofactors
        for ( s = 1; s <= nSteps; s++ )
        {
            for ( k = 0; k < s; k++ )
            {
                nSize = (1 << k);
                for ( i = 0; i < nSize; i++ )
                {
                    Kit_TruthCofactor0New( ppCofs[k+1][2*i+0], ppCofs[k][i], nVars, piCofVar[k] );
                    Kit_TruthCofactor1New( ppCofs[k+1][2*i+1], ppCofs[k][i], nVars, piCofVar[k] );
                }
            }
        }
        // compute DSD networks
        nSize = (1 << nSteps);
        nPrimeSizeMax = 0;
        nSuppSizeMax = 0;
        for ( i = 0; i < nSize; i++ )
        {
            ppNtks[i] = Kit_DsdDecompose( ppCofs[nSteps][i], nVars );
            ppNtks[i] = Kit_DsdExpand( pTemp = ppNtks[i] );
            Kit_DsdNtkFree( pTemp );
            if ( fVerbose )
            {
                printf( "Cof%d%d: ", nSteps, i );
2833
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
            }
            // compute the largest non-decomp block
            nPrimeSizeCur  = Kit_DsdNonDsdSizeMax(ppNtks[i]);
            nPrimeSizeMax  = KIT_MAX( nPrimeSizeMax, nPrimeSizeCur );
            Kit_DsdNtkFree( ppNtks[i] );
            nSuppSizeMax += Kit_TruthSupportSize( ppCofs[nSteps][i], nVars );
        }
        printf( "Max = %2d. Supps = %2d.\n", nPrimeSizeMax, nSuppSizeMax );
    }

    if ( nCofLevel == 3 )
    for ( v1 = 0; v1 < nVars; v1++ )
    for ( v2 = v1+1; v2 < nVars; v2++ )
    for ( v3 = v2+1; v3 < nVars; v3++ )
    {
        nSteps = 0;
        piCofVar[nSteps++] = v1;
        piCofVar[nSteps++] = v2;
        piCofVar[nSteps++] = v3;

        printf( "    Variables { " );
        for ( i = 0; i < nSteps; i++ )
            printf( "%c ", 'a' + piCofVar[i] );
        printf( "}\n" );

        // single cofactors
        for ( s = 1; s <= nSteps; s++ )
        {
            for ( k = 0; k < s; k++ )
            {
                nSize = (1 << k);
                for ( i = 0; i < nSize; i++ )
                {
                    Kit_TruthCofactor0New( ppCofs[k+1][2*i+0], ppCofs[k][i], nVars, piCofVar[k] );
                    Kit_TruthCofactor1New( ppCofs[k+1][2*i+1], ppCofs[k][i], nVars, piCofVar[k] );
                }
            }
        }
        // compute DSD networks
        nSize = (1 << nSteps);
        nPrimeSizeMax = 0;
        nSuppSizeMax = 0;
        for ( i = 0; i < nSize; i++ )
        {
            ppNtks[i] = Kit_DsdDecompose( ppCofs[nSteps][i], nVars );
            ppNtks[i] = Kit_DsdExpand( pTemp = ppNtks[i] );
            Kit_DsdNtkFree( pTemp );
            if ( fVerbose )
            {
                printf( "Cof%d%d: ", nSteps, i );
2884
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
            }
            // compute the largest non-decomp block
            nPrimeSizeCur  = Kit_DsdNonDsdSizeMax(ppNtks[i]);
            nPrimeSizeMax  = KIT_MAX( nPrimeSizeMax, nPrimeSizeCur );
            Kit_DsdNtkFree( ppNtks[i] );
            nSuppSizeMax += Kit_TruthSupportSize( ppCofs[nSteps][i], nVars );
        }
        printf( "Max = %2d. Supps = %2d.\n", nPrimeSizeMax, nSuppSizeMax );
    }

    if ( nCofLevel == 4 )
    for ( v1 = 0; v1 < nVars; v1++ )
    for ( v2 = v1+1; v2 < nVars; v2++ )
    for ( v3 = v2+1; v3 < nVars; v3++ )
    for ( v4 = v3+1; v4 < nVars; v4++ )
    {
        nSteps = 0;
        piCofVar[nSteps++] = v1;
        piCofVar[nSteps++] = v2;
        piCofVar[nSteps++] = v3;
        piCofVar[nSteps++] = v4;

        printf( "    Variables { " );
        for ( i = 0; i < nSteps; i++ )
            printf( "%c ", 'a' + piCofVar[i] );
        printf( "}\n" );

        // single cofactors
        for ( s = 1; s <= nSteps; s++ )
        {
            for ( k = 0; k < s; k++ )
            {
                nSize = (1 << k);
                for ( i = 0; i < nSize; i++ )
                {
                    Kit_TruthCofactor0New( ppCofs[k+1][2*i+0], ppCofs[k][i], nVars, piCofVar[k] );
                    Kit_TruthCofactor1New( ppCofs[k+1][2*i+1], ppCofs[k][i], nVars, piCofVar[k] );
                }
            }
        }
        // compute DSD networks
        nSize = (1 << nSteps);
        nPrimeSizeMax = 0;
        nSuppSizeMax = 0;
        for ( i = 0; i < nSize; i++ )
        {
            ppNtks[i] = Kit_DsdDecompose( ppCofs[nSteps][i], nVars );
            ppNtks[i] = Kit_DsdExpand( pTemp = ppNtks[i] );
            Kit_DsdNtkFree( pTemp );
            if ( fVerbose )
            {
                printf( "Cof%d%d: ", nSteps, i );
2937
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
            }
            // compute the largest non-decomp block
            nPrimeSizeCur  = Kit_DsdNonDsdSizeMax(ppNtks[i]);
            nPrimeSizeMax  = KIT_MAX( nPrimeSizeMax, nPrimeSizeCur );
            Kit_DsdNtkFree( ppNtks[i] );
            nSuppSizeMax += Kit_TruthSupportSize( ppCofs[nSteps][i], nVars );
        }
        printf( "Max = %2d. Supps = %2d.\n", nPrimeSizeMax, nSuppSizeMax );
    }


Alan Mishchenko committed
2949
    ABC_FREE( ppCofs[0][0] );
Alan Mishchenko committed
2950 2951
}

2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char ** Kit_DsdNpn4ClassNames()
{
    static const char * pNames[222] = {
        "F = 0",                     /*   0 */ 
        "F = (!d*(!c*(!b*!a)))",     /*   1 */
        "F = (!d*(!c*!b))",          /*   2 */
        "F = (!d*(!c*(b+a)))",       /*   3 */
        "F = (!d*(!c*!(b*a)))",      /*   4 */
        "F = (!d*!c)",               /*   5 */
        "F = (!d*16(a,b,c))",        /*   6 */
        "F = (!d*17(a,b,c))",        /*   7 */
        "F = (!d*18(a,b,c))",        /*   8 */
        "F = (!d*19(a,b,c))",        /*   9 */
        "F = (!d*CA(!b,!c,a))",      /*  10 */
        "F = (!d*(c+!(!b*!a)))",     /*  11 */
        "F = (!d*!(c*!(!b*!a)))",    /*  12 */
        "F = (!d*(c+b))",            /*  13 */
        "F = (!d*3D(a,b,c))",        /*  14 */
        "F = (!d*!(c*b))",           /*  15 */
        "F = (!d*(c+(b+!a)))",       /*  16 */
        "F = (!d*6B(a,b,c))",        /*  17 */
        "F = (!d*!(c*!(b+a)))",      /*  18 */
        "F = (!d*7E(a,b,c))",        /*  19 */
        "F = (!d*!(c*(b*a)))",       /*  20 */
        "F = (!d)",                  /*  21 */
        "F = 0116(a,b,c,d)",         /*  22 */
        "F = 0117(a,b,c,d)",         /*  23 */
        "F = 0118(a,b,c,d)",         /*  24 */
        "F = 0119(a,b,c,d)",         /*  25 */
        "F = 011A(a,b,c,d)",         /*  26 */
        "F = 011B(a,b,c,d)",         /*  27 */
        "F = 29((!b*!a),c,d)",       /*  28 */
        "F = 2B((!b*!a),c,d)",       /*  29 */
        "F = 012C(a,b,c,d)",         /*  30 */
        "F = 012D(a,b,c,d)",         /*  31 */
        "F = 012F(a,b,c,d)",         /*  32 */
        "F = 013C(a,b,c,d)",         /*  33 */
        "F = 013D(a,b,c,d)",         /*  34 */
        "F = 013E(a,b,c,d)",         /*  35 */
        "F = 013F(a,b,c,d)",         /*  36 */
        "F = 0168(a,b,c,d)",         /*  37 */
        "F = 0169(a,b,c,d)",         /*  38 */
        "F = 016A(a,b,c,d)",         /*  39 */
        "F = 016B(a,b,c,d)",         /*  40 */
        "F = 016E(a,b,c,d)",         /*  41 */
        "F = 016F(a,b,c,d)",         /*  42 */
        "F = 017E(a,b,c,d)",         /*  43 */
        "F = 017F(a,b,c,d)",         /*  44 */
        "F = 0180(a,b,c,d)",         /*  45 */
        "F = 0181(a,b,c,d)",         /*  46 */
        "F = 0182(a,b,c,d)",         /*  47 */
        "F = 0183(a,b,c,d)",         /*  48 */
        "F = 0186(a,b,c,d)",         /*  49 */
        "F = 0187(a,b,c,d)",         /*  50 */
        "F = 0189(a,b,c,d)",         /*  51 */
        "F = 018B(a,b,c,d)",         /*  52 */
        "F = 018F(a,b,c,d)",         /*  53 */
        "F = 0196(a,b,c,d)",         /*  54 */
        "F = 0197(a,b,c,d)",         /*  55 */
        "F = 0198(a,b,c,d)",         /*  56 */
        "F = 0199(a,b,c,d)",         /*  57 */
        "F = 019A(a,b,c,d)",         /*  58 */
        "F = 019B(a,b,c,d)",         /*  59 */
        "F = 019E(a,b,c,d)",         /*  60 */
        "F = 019F(a,b,c,d)",         /*  61 */
        "F = 42(a,(!c*!b),d)",       /*  62 */
        "F = 46(a,(!c*!b),d)",       /*  63 */
        "F = 4A(a,(!c*!b),d)",       /*  64 */
        "F = CA((!c*!b),!d,a)",      /*  65 */
        "F = 01AC(a,b,c,d)",         /*  66 */
        "F = 01AD(a,b,c,d)",         /*  67 */
        "F = 01AE(a,b,c,d)",         /*  68 */
        "F = 01AF(a,b,c,d)",         /*  69 */
        "F = 01BC(a,b,c,d)",         /*  70 */
        "F = 01BD(a,b,c,d)",         /*  71 */
        "F = 01BE(a,b,c,d)",         /*  72 */
        "F = 01BF(a,b,c,d)",         /*  73 */
        "F = 01E8(a,b,c,d)",         /*  74 */
        "F = 01E9(a,b,c,d)",         /*  75 */
        "F = 01EA(a,b,c,d)",         /*  76 */
        "F = 01EB(a,b,c,d)",         /*  77 */
        "F = 25((!b*!a),c,d)",       /*  78 */
        "F = !CA(d,c,(!b*!a))",      /*  79 */
        "F = (d+!(!c*(!b*!a)))",     /*  80 */
        "F = 16(b,c,d)",             /*  81 */
        "F = 033D(a,b,c,d)",         /*  82 */
        "F = 17(b,c,d)",             /*  83 */
        "F = ((!d*!a)+(!c*!b))",     /*  84 */
        "F = !(!(!c*!b)*!(!d*!a))",  /*  85 */
        "F = 0358(a,b,c,d)",         /*  86 */
        "F = 0359(a,b,c,d)",         /*  87 */
        "F = 035A(a,b,c,d)",         /*  88 */
        "F = 035B(a,b,c,d)",         /*  89 */
        "F = 035E(a,b,c,d)",         /*  90 */
        "F = 035F(a,b,c,d)",         /*  91 */
        "F = 0368(a,b,c,d)",         /*  92 */
        "F = 0369(a,b,c,d)",         /*  93 */
        "F = 036A(a,b,c,d)",         /*  94 */
        "F = 036B(a,b,c,d)",         /*  95 */
        "F = 036C(a,b,c,d)",         /*  96 */
        "F = 036D(a,b,c,d)",         /*  97 */
        "F = 036E(a,b,c,d)",         /*  98 */
        "F = 036F(a,b,c,d)",         /*  99 */
        "F = 037C(a,b,c,d)",         /* 100 */
        "F = 037D(a,b,c,d)",         /* 101 */
        "F = 037E(a,b,c,d)",         /* 102 */
        "F = 18(b,c,d)",             /* 103 */
        "F = 03C1(a,b,c,d)",         /* 104 */
        "F = 19(b,c,d)",             /* 105 */
        "F = 03C5(a,b,c,d)",         /* 106 */
        "F = 03C6(a,b,c,d)",         /* 107 */
        "F = 03C7(a,b,c,d)",         /* 108 */
        "F = CA(!c,!d,b)",           /* 109 */
        "F = 03D4(a,b,c,d)",         /* 110 */
        "F = 03D5(a,b,c,d)",         /* 111 */
        "F = 03D6(a,b,c,d)",         /* 112 */
        "F = 03D7(a,b,c,d)",         /* 113 */
        "F = 03D8(a,b,c,d)",         /* 114 */
        "F = 03D9(a,b,c,d)",         /* 115 */
        "F = 03DB(a,b,c,d)",         /* 116 */
        "F = 03DC(a,b,c,d)",         /* 117 */
        "F = 03DD(a,b,c,d)",         /* 118 */
        "F = 03DE(a,b,c,d)",         /* 119 */
        "F = (d+!(!c*!b))",          /* 120 */
        "F = ((d+c)*(b+a))",         /* 121 */
        "F = 0661(a,b,c,d)",         /* 122 */
        "F = 0662(a,b,c,d)",         /* 123 */
        "F = 0663(a,b,c,d)",         /* 124 */
        "F = (!(d*c)*(b+a))",        /* 125 */
        "F = 0667(a,b,c,d)",         /* 126 */
        "F = 29((b+a),c,d)",         /* 127 */
        "F = 066B(a,b,c,d)",         /* 128 */
        "F = 2B((b+a),c,d)",         /* 129 */
        "F = 0672(a,b,c,d)",         /* 130 */
        "F = 0673(a,b,c,d)",         /* 131 */
        "F = 0676(a,b,c,d)",         /* 132 */
        "F = 0678(a,b,c,d)",         /* 133 */
        "F = 0679(a,b,c,d)",         /* 134 */
        "F = 067A(a,b,c,d)",         /* 135 */
        "F = 067B(a,b,c,d)",         /* 136 */
        "F = 067E(a,b,c,d)",         /* 137 */
        "F = 24((b+a),c,d)",         /* 138 */
        "F = 0691(a,b,c,d)",         /* 139 */
        "F = 0693(a,b,c,d)",         /* 140 */
        "F = 26((b+a),c,d)",         /* 141 */
        "F = 0697(a,b,c,d)",         /* 142 */
        "F = !CA(d,c,(b+a))",        /* 143 */
        "F = 06B0(a,b,c,d)",         /* 144 */
        "F = 06B1(a,b,c,d)",         /* 145 */
        "F = 06B2(a,b,c,d)",         /* 146 */
        "F = 06B3(a,b,c,d)",         /* 147 */
        "F = 06B4(a,b,c,d)",         /* 148 */
        "F = 06B5(a,b,c,d)",         /* 149 */
        "F = 06B6(a,b,c,d)",         /* 150 */
        "F = 06B7(a,b,c,d)",         /* 151 */
        "F = 06B9(a,b,c,d)",         /* 152 */
        "F = 06BD(a,b,c,d)",         /* 153 */
        "F = 2C((b+a),c,d)",         /* 154 */
        "F = 06F1(a,b,c,d)",         /* 155 */
        "F = 06F2(a,b,c,d)",         /* 156 */
        "F = CA((b+a),!d,c)",        /* 157 */
        "F = (d+!(!c*!(b+!a)))",     /* 158 */
        "F = 0776(a,b,c,d)",         /* 159 */
        "F = 16((b*a),c,d)",         /* 160 */
        "F = 0779(a,b,c,d)",         /* 161 */
        "F = 077A(a,b,c,d)",         /* 162 */
        "F = 077E(a,b,c,d)",         /* 163 */
        "F = 07B0(a,b,c,d)",         /* 164 */
        "F = 07B1(a,b,c,d)",         /* 165 */
        "F = 07B4(a,b,c,d)",         /* 166 */
        "F = 07B5(a,b,c,d)",         /* 167 */
        "F = 07B6(a,b,c,d)",         /* 168 */
        "F = 07BC(a,b,c,d)",         /* 169 */
        "F = 07E0(a,b,c,d)",         /* 170 */
        "F = 07E1(a,b,c,d)",         /* 171 */
        "F = 07E2(a,b,c,d)",         /* 172 */
        "F = 07E3(a,b,c,d)",         /* 173 */
        "F = 07E6(a,b,c,d)",         /* 174 */
        "F = 07E9(a,b,c,d)",         /* 175 */
        "F = 1C((b*a),c,d)",         /* 176 */
        "F = 07F1(a,b,c,d)",         /* 177 */
        "F = 07F2(a,b,c,d)",         /* 178 */
        "F = (d+!(!c*!(b*a)))",      /* 179 */
        "F = (d+c)",                 /* 180 */
        "F = 1668(a,b,c,d)",         /* 181 */
        "F = 1669(a,b,c,d)",         /* 182 */
        "F = 166A(a,b,c,d)",         /* 183 */
        "F = 166B(a,b,c,d)",         /* 184 */
        "F = 166E(a,b,c,d)",         /* 185 */
        "F = 167E(a,b,c,d)",         /* 186 */
        "F = 1681(a,b,c,d)",         /* 187 */
        "F = 1683(a,b,c,d)",         /* 188 */
        "F = 1686(a,b,c,d)",         /* 189 */
        "F = 1687(a,b,c,d)",         /* 190 */
        "F = 1689(a,b,c,d)",         /* 191 */
        "F = 168B(a,b,c,d)",         /* 192 */
        "F = 168E(a,b,c,d)",         /* 193 */
        "F = 1696(a,b,c,d)",         /* 194 */
        "F = 1697(a,b,c,d)",         /* 195 */
        "F = 1698(a,b,c,d)",         /* 196 */
        "F = 1699(a,b,c,d)",         /* 197 */
        "F = 169A(a,b,c,d)",         /* 198 */
        "F = 169B(a,b,c,d)",         /* 199 */
        "F = 169E(a,b,c,d)",         /* 200 */
        "F = 16A9(a,b,c,d)",         /* 201 */
        "F = 16AC(a,b,c,d)",         /* 202 */
        "F = 16AD(a,b,c,d)",         /* 203 */
        "F = 16BC(a,b,c,d)",         /* 204 */
        "F = (d+E9(a,b,c))",         /* 205 */
        "F = 177E(a,b,c,d)",         /* 206 */
        "F = 178E(a,b,c,d)",         /* 207 */
        "F = 1796(a,b,c,d)",         /* 208 */
        "F = 1798(a,b,c,d)",         /* 209 */
        "F = 179A(a,b,c,d)",         /* 210 */
        "F = 17AC(a,b,c,d)",         /* 211 */
        "F = (d+E8(a,b,c))",         /* 212 */
        "F = (d+E7(a,b,c))",         /* 213 */
        "F = 19E1(a,b,c,d)",         /* 214 */
        "F = 19E3(a,b,c,d)",         /* 215 */
        "F = (d+E6(a,b,c))",         /* 216 */
        "F = 1BD8(a,b,c,d)",         /* 217 */
        "F = (d+CA(b,c,a))",         /* 218 */
        "F = (d+(c+(!b*!a)))",       /* 219 */
        "F = (d+(c+!b))",            /* 220 */
        "F = (d+(c+(b+a)))"          /* 221 */
    };
    return (char **)pNames;
}


Alan Mishchenko committed
3194 3195 3196 3197 3198
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


3199 3200
ABC_NAMESPACE_IMPL_END