kitDsd.c 109 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
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 )
    {
274
        if ( Abc_LitIsCompl(iLit) ) 
275
            fprintf( pFile, "!" );
276
        Kit_DsdPrint2_rec( pFile, pNtk, Abc_Lit2Var(iLit) );
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        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 = " );
300
    if ( Abc_LitIsCompl(pNtk->Root) )
301
        fprintf( pFile, "!" );
302
    Kit_DsdPrint2_rec( pFile, pNtk, Abc_Lit2Var(pNtk->Root) );
303 304 305 306 307 308 309 310 311 312 313 314 315 316
//    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 )
349
        Kit_DsdPrintHex( pFile, 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
    {
354
        if ( Abc_LitIsCompl(iLit) ) 
Alan Mishchenko committed
355
            fprintf( pFile, "!" );
356
        Kit_DsdPrint_rec( pFile, pNtk, Abc_Lit2Var(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 = " );
377
    if ( Abc_LitIsCompl(pNtk->Root) )
Alan Mishchenko committed
378
        fprintf( pFile, "!" );
379
    Kit_DsdPrint_rec( pFile, pNtk, Abc_Lit2Var(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
  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 )
    {
431
        if ( Abc_LitIsCompl(iLit) ) 
432
            *pBuff++ = '!';
433
        pBuff = Kit_DsdWrite_rec( pBuff, pNtk, Abc_Lit2Var(iLit) );
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        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 )
{
454
    if ( Abc_LitIsCompl(pNtk->Root) )
455
        *pBuff++ = '!';
456
    pBuff = Kit_DsdWrite_rec( pBuff, pNtk, Abc_Lit2Var(pNtk->Root) );
457 458 459 460 461
    *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

    // 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];
591 592
        pTruthFans[0] = Kit_DsdTruthComputeNode_rec( p, pNtk, Abc_Lit2Var(iLit) );
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
593 594 595 596 597 598 599 600
            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 )
601
        pTruthFans[i] = Kit_DsdTruthComputeNode_rec( p, pNtk, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
602 603 604 605 606 607 608
    // create the truth table

    // simple gates
    if ( pObj->Type == KIT_DSD_AND )
    {
        Kit_TruthFill( pTruthRes, pNtk->nVars );
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
609
            Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
610 611 612 613 614 615 616 617 618
        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 );
619
            fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
        }
        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 )
640
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
641 642 643
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
*/
Alan Mishchenko committed
644
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
645
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
646
            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
    // compute truth table for each node
672
    pTruthRes = Kit_DsdTruthComputeNode_rec( p, pNtk, Abc_Lit2Var(pNtk->Root) );
Alan Mishchenko committed
673
    // complement the truth table if needed
674
    if ( Abc_LitIsCompl(pNtk->Root) )
Alan Mishchenko committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
        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 ) );
723 724
        pTruthFans[0] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), uSupp );
        if ( Abc_LitIsCompl(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)) )
736
                pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(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 )
746
            pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(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
            if ( pTruthFans[i] )
756
                Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(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
            if ( pTruthFans[i] )
            {
                Kit_TruthXor( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars );
768
                fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
769
            }
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
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
800
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
801 802
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
Alan Mishchenko committed
803
*/
Alan Mishchenko committed
804
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
805
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
806
            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
835
    pTruthRes = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(pNtk->Root), uSupp );
Alan Mishchenko committed
836
    // complement the truth table if needed
837
    if ( Abc_LitIsCompl(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

    // 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) )
900
                pTruthFans[i] = Kit_DsdTruthComputeNodeTwo_rec( p, pNtk, Abc_Lit2Var(iLit), uSupp, iVar, pTruthDec );
Alan Mishchenko committed
901
            else
902
                pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), 0 );
Alan Mishchenko committed
903 904 905 906 907 908 909
        }

        // create composition/decomposition functions
        if ( pObj->Type == KIT_DSD_AND )
        {
            Kit_TruthFill( pTruthRes, pNtk->nVars );
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
910
                Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
911 912 913 914 915 916 917 918
            return pTruthRes;
        }
        if ( pObj->Type == KIT_DSD_XOR )
        {
            Kit_TruthClear( pTruthRes, pNtk->nVars );
            fCompl = 0;
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            {
919
                fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
                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 )
        {
938
            pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), 0 );
Alan Mishchenko committed
939 940 941 942 943 944 945 946 947 948
            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] )
949
                    Kit_TruthAndPhase( pTruthDec, pTruthDec, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
950
                else
951
                    Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
952 953 954 955 956 957 958 959 960
            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 )
            {
961
                fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
                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
    // 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 )
999
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1000 1001
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
Alan Mishchenko committed
1002
*/
Alan Mishchenko committed
1003
//    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1004
//        assert( !Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1005
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1006
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
1007
            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
1050
    pTruthRes = Kit_DsdTruthComputeNodeTwo_rec( p, pNtk, Abc_Lit2Var(pNtk->Root), uSupp, iVar, pTruthDec );
Alan Mishchenko committed
1051
    // complement the truth table if needed
1052
    if ( Abc_LitIsCompl(pNtk->Root) )
Alan Mishchenko committed
1053
        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
  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 );
1153 1154
        Res0 = Kit_DsdCountLuts_rec( pNtk, nLutSize, Abc_Lit2Var(pObj->pFans[0]), pCounter );
        Res1 = Kit_DsdCountLuts_rec( pNtk, nLutSize, Abc_Lit2Var(pObj->pFans[1]), pCounter );
Alan Mishchenko committed
1155 1156 1157 1158 1159 1160 1161 1162
        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
    {
        *pCounter = 1000;
        return 0;
    }
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1169
        Kit_DsdCountLuts_rec( pNtk, nLutSize, Abc_Lit2Var(iLit), pCounter );
Alan Mishchenko committed
1170
    (*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
    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;
1194
    Kit_DsdCountLuts_rec( pNtk, nLutSize, Abc_Lit2Var(pNtk->Root), &Counter );
Alan Mishchenko committed
1195 1196 1197 1198 1199
    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
    Kit_DsdGetSupports( pNtk );
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
    {
        if ( pObj->Type != KIT_DSD_PRIME )
            continue;
1274
        uSupport |= Kit_DsdLitSupport( pNtk, Abc_Var2Lit(pObj->Id,0) );
Alan Mishchenko committed
1275 1276 1277 1278
    }
    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
1296 1297
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
    if ( Abc_LitIsCompl(iLit) || Abc_Lit2Var(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
1323 1324
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
    if ( Abc_Lit2Var(iLit) < p->nVars || pObj->Type != KIT_DSD_XOR )
Alan Mishchenko committed
1325 1326 1327 1328 1329
    {
        piLitsNew[(*nLitsNew)++] = iLit;
        return;
    }
    // iterate through the fanins
1330
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
1331
    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
1334 1335
    if ( Abc_LitIsCompl(iLit) )
        piLitsNew[0] = Abc_LitNot( 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
1356
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
1357
    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, Abc_LitRegular(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] );
1365
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1366 1367 1368
    }
    if ( pObj->Type == KIT_DSD_XOR )
    {
1369 1370
        int fCompl = Abc_LitIsCompl(iLit);
        Kit_DsdExpandCollectXor_rec( p, Abc_LitRegular(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++ )
        {
1374 1375
            pObjNew->pFans[i] = Kit_DsdExpandNode_rec( pNew, p, Abc_LitRegular(piLitsNew[i]) );
            fCompl ^= Abc_LitIsCompl(piLitsNew[i]);
Alan Mishchenko committed
1376
        }
1377
        return Abc_Var2Lit( 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
1392
        if ( Abc_LitIsCompl(pObjNew->pFans[i]) )
Alan Mishchenko committed
1393
        {
1394
            pObjNew->pFans[i] = Abc_LitRegular(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

    if ( pObj->nFans == 3 && 
        (pTruthNew[0] == 0xCACACACA || pTruthNew[0] == 0xC5C5C5C5 || 
         pTruthNew[0] == 0x3A3A3A3A || pTruthNew[0] == 0x35353535) )
    {
        // translate into regular MUXes
        if ( pTruthNew[0] == 0xC5C5C5C5 )
1405
            pObjNew->pFans[0] = Abc_LitNot(pObjNew->pFans[0]);
Alan Mishchenko committed
1406
        else if ( pTruthNew[0] == 0x3A3A3A3A )
1407
            pObjNew->pFans[1] = Abc_LitNot(pObjNew->pFans[1]);
Alan Mishchenko committed
1408 1409
        else if ( pTruthNew[0] == 0x35353535 )
        {
1410 1411
            pObjNew->pFans[0] = Abc_LitNot(pObjNew->pFans[0]);
            pObjNew->pFans[1] = Abc_LitNot(pObjNew->pFans[1]);
Alan Mishchenko committed
1412 1413 1414
        }
        pTruthNew[0] = 0xCACACACA;
        // resolve the complemented control input
1415
        if ( Abc_LitIsCompl(pObjNew->pFans[2]) )
Alan Mishchenko committed
1416 1417 1418 1419
        {
            unsigned char Temp = pObjNew->pFans[0];
            pObjNew->pFans[0] = pObjNew->pFans[1];
            pObjNew->pFans[1] = Temp;
1420
            pObjNew->pFans[2] = Abc_LitNot(pObjNew->pFans[2]);
Alan Mishchenko committed
1421 1422
        }
        // resolve the complemented true input
1423
        if ( Abc_LitIsCompl(pObjNew->pFans[1]) )
Alan Mishchenko committed
1424
        {
1425 1426 1427
            iLit = Abc_LitNot(iLit);
            pObjNew->pFans[0] = Abc_LitNot(pObjNew->pFans[0]);
            pObjNew->pFans[1] = Abc_LitNot(pObjNew->pFans[1]);
Alan Mishchenko committed
1428
        }
1429
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1430 1431 1432 1433
    }
    else
    {
        // if the incoming phase is complemented, absorb it into the prime node
1434
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
1435
            Kit_TruthNot( pTruthNew, pTruthNew, pObj->nFans );
1436
        return Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1437
    }
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
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_CONST1, 0 );
1462
        pNew->Root = Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(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
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_VAR, 1 );
        pObjNew->pFans[0] = Kit_DsdNtkRoot(p)->pFans[0];
1469
        pNew->Root = Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(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     []

***********************************************************************/
1488
void Kit_DsdCompSort( int pPrios[], unsigned uSupps[], unsigned short * 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

    // consider the case of simple gate
1552
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
1553
    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
            pObjNew->pFans[0] = Kit_DsdShrink_rec( pNew, p, piLitsNew[i], pPrios );
            pObjNew->pFans[1] = iLitNew;
1570
            iLitNew = Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1571
        }
1572
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(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
        {
1579
            assert( !Abc_LitIsCompl(iLitFanin) );
Alan Mishchenko committed
1580
            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
            pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_XOR, 2 );
            pObjNew->pFans[0] = Kit_DsdShrink_rec( pNew, p, piLitsNew[i], pPrios );
            pObjNew->pFans[1] = iLitNew;
1592
            iLitNew = Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1593
        }
1594
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
    }
    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
1609
        if ( Abc_LitIsCompl(pObjNew->pFans[i]) )
Alan Mishchenko committed
1610
        {
1611
            pObjNew->pFans[i] = Abc_LitRegular(pObjNew->pFans[i]);
Alan Mishchenko committed
1612 1613 1614 1615
            Kit_TruthChangePhase( pTruthNew, pObjNew->nFans, i );
        }
    }
    // if the incoming phase is complemented, absorb it into the prime node
1616
    if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
1617
        Kit_TruthNot( pTruthNew, pTruthNew, pObj->nFans );
1618
    return Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
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
}

/**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 );
1644
        pNew->Root = Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(p->Root) );
Alan Mishchenko committed
1645 1646 1647 1648 1649 1650
        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];
1651
        pNew->Root = Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(p->Root) );
Alan Mishchenko committed
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
        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
    int iFaninLit;
1740
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
    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
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1808
        RetValue |= Kit_DsdFindLargeBox_rec( pNtk, Abc_Lit2Var(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
  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 )
{
1825
    return Kit_DsdFindLargeBox_rec( pNtk, Abc_Lit2Var(pNtk->Root), Size );
Alan Mishchenko committed
1826 1827 1828 1829
}

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

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 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
  Synopsis    [Returns 1 if there is a component with more than 3 inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCountAigNodes_rec( Kit_DsdNtk_t * pNtk, int Id )
{
    Kit_DsdObj_t * pObj;
    unsigned iLit, i, RetValue;
    pObj = Kit_DsdNtkObj( pNtk, Id );
    if ( pObj == NULL )
        return 0;
    if ( pObj->Type == KIT_DSD_CONST1 || pObj->Type == KIT_DSD_VAR )
        return 0;
    if ( pObj->nFans < 2 ) // why this happens? - need to figure out
        return 0; 
    assert( pObj->nFans > 1 );
    if ( pObj->Type == KIT_DSD_AND )
        RetValue = ((int)pObj->nFans - 1);
    else if ( pObj->Type == KIT_DSD_XOR )
        RetValue = ((int)pObj->nFans - 1) * 3;
    else if ( pObj->Type == KIT_DSD_PRIME )
    {
        // assuming MUX decomposition
        assert( (int)pObj->nFans == 3 );
        RetValue = 3;
    }
    else assert( 0 );
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
        RetValue += Kit_DsdCountAigNodes_rec( pNtk, Abc_Lit2Var(iLit) );
    return RetValue;
}


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

  Synopsis    [Returns 1 if there is a component with more than 3 inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCountAigNodes2( Kit_DsdNtk_t * pNtk )
{
    return Kit_DsdCountAigNodes_rec( pNtk, Abc_Lit2Var(pNtk->Root) );
}

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

  Synopsis    [Returns 1 if there is a component with more than 3 inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Kit_DsdCountAigNodes( Kit_DsdNtk_t * pNtk )
{
    Kit_DsdObj_t * pObj;
    int i, Counter = 0;
    for ( i = 0; i < pNtk->nNodes; i++ )
    {
        pObj = pNtk->pNodes[i];
        if ( pObj->Type == KIT_DSD_AND )
            Counter += ((int)pObj->nFans - 1);
        else if ( pObj->Type == KIT_DSD_XOR )
            Counter += ((int)pObj->nFans - 1) * 3;
        else if ( pObj->Type == KIT_DSD_PRIME ) // assuming MUX decomposition
            Counter += 3;
    }
    return Counter;
}

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

Alan Mishchenko committed
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
  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++ )
    {
1928
        if ( Abc_Lit2Var(pObj0->pFans[i]) >= 4 )
Alan Mishchenko committed
1929 1930
            continue;
        for ( k = 0; k < pObj1->nFans; k++ )
1931
            if ( Abc_Lit2Var(pObj0->pFans[i]) == Abc_Lit2Var(pObj1->pFans[k]) )
Alan Mishchenko committed
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
                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
1961 1962 1963 1964 1965 1966 1967 1968 1969
  Synopsis    [Performs decomposition of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1970
void Kit_DsdDecompose_rec( Kit_DsdNtk_t * pNtk, Kit_DsdObj_t * pObj, unsigned uSupp, unsigned short * pPar, int nDecMux )
Alan Mishchenko committed
1971
{
Alan Mishchenko committed
1972
    Kit_DsdObj_t * pRes, * pRes0, * pRes1;
Alan Mishchenko committed
1973
    int nWords = Kit_TruthWordNum(pObj->nFans);
Alan Mishchenko committed
1974
    unsigned * pTruth = Kit_DsdObjTruth(pObj);
Alan Mishchenko committed
1975 1976
    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
1977
    int i, iLit0, iLit1, nFans0, nFans1, nPairs;
Alan Mishchenko committed
1978 1979 1980 1981 1982
    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
1983
    assert( uSupp == (uSupp0 = (unsigned)Kit_TruthSupport(pTruth, pObj->nFans)) );
Alan Mishchenko committed
1984 1985

    // compress the truth table
Alan Mishchenko committed
1986
    if ( uSupp != Kit_BitMask(pObj->nFans) )
Alan Mishchenko committed
1987
    {
Alan Mishchenko committed
1988 1989
        nFansNew = Kit_WordCountOnes(uSupp);
        Kit_TruthShrink( pNtk->pMem, pTruth, nFansNew, pObj->nFans, uSupp, 1 );
Alan Mishchenko committed
1990
        for ( j = k = 0; j < pObj->nFans; j++ )
Alan Mishchenko committed
1991
            if ( uSupp & (1 << j) )
Alan Mishchenko committed
1992 1993 1994
                pObj->pFans[k++] = pObj->pFans[j];
        assert( k == nFansNew );
        pObj->nFans = k;
Alan Mishchenko committed
1995
        uSupp = Kit_BitMask(pObj->nFans);
Alan Mishchenko committed
1996 1997 1998 1999 2000 2001 2002
    }

    // consider the single variable case
    if ( pObj->nFans == 1 )
    {
        pObj->Type = KIT_DSD_NONE;
        if ( pTruth[0] == 0x55555555 )
2003
            pObj->pFans[0] = Abc_LitNot(pObj->pFans[0]);
Alan Mishchenko committed
2004 2005 2006
        else
            assert( pTruth[0] == 0xAAAAAAAA );
        // update the parent pointer
2007
        *pPar = Abc_LitNotCond( pObj->pFans[0], Abc_LitIsCompl(*pPar) );
Alan Mishchenko committed
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
        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
2032
            assert( uSupp == (uSupp0 | uSupp1 | (1<<i)) );
Alan Mishchenko committed
2033 2034 2035
            if ( uSupp0 & uSupp1 )
                continue;
            // perform MUX decomposition
Alan Mishchenko committed
2036 2037
            pRes0 = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, pObj->nFans );
            pRes1 = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, pObj->nFans );
Alan Mishchenko committed
2038 2039 2040 2041 2042
            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
2043 2044
            Kit_TruthCopy( Kit_DsdObjTruth(pRes0), pCofs2[0], pObj->nFans );        
            Kit_TruthCopy( Kit_DsdObjTruth(pRes1), pCofs2[1], pObj->nFans ); 
Alan Mishchenko committed
2045
            // update the current one
Alan Mishchenko committed
2046 2047
            assert( pObj->Type == KIT_DSD_PRIME );
            pTruth[0] = 0xCACACACA;
Alan Mishchenko committed
2048
            pObj->nFans = 3;
Alan Mishchenko committed
2049
            pObj->pFans[2] = pObj->pFans[i];
Alan Mishchenko committed
2050
            pObj->pFans[0] = 2*pRes0->Id; pRes0->nRefs++;
Alan Mishchenko committed
2051
            pObj->pFans[1] = 2*pRes1->Id; pRes1->nRefs++;
Alan Mishchenko committed
2052
            // call recursively
Alan Mishchenko committed
2053 2054
            Kit_DsdDecompose_rec( pNtk, pRes0, uSupp0, pObj->pFans + 0, nDecMux );
            Kit_DsdDecompose_rec( pNtk, pRes1, uSupp1, pObj->pFans + 1, nDecMux );
Alan Mishchenko committed
2055 2056 2057 2058
            return;
        }

        // create the new node
Alan Mishchenko committed
2059
        pRes = Kit_DsdObjAlloc( pNtk, KIT_DSD_AND, 2 );
Alan Mishchenko committed
2060
        pRes->nRefs++;
Alan Mishchenko committed
2061
        pRes->nFans = 2;
Alan Mishchenko committed
2062
        pRes->pFans[0] = pObj->pFans[i];  pObj->pFans[i] = 127;  uSupp &= ~(1 << i);
Alan Mishchenko committed
2063 2064
        pRes->pFans[1] = 2*pObj->Id;
        // update the parent pointer
2065
        *pPar = Abc_LitNotCond( 2 * pRes->Id, Abc_LitIsCompl(*pPar) );
Alan Mishchenko committed
2066 2067 2068 2069 2070 2071 2072
        // consider different decompositions
        if ( fEquals[0][0] )
        {
            Kit_TruthCopy( pTruth, pCofs2[1], pObj->nFans );
        }
        else if ( fEquals[0][1] )
        {
2073
            pRes->pFans[0] = Abc_LitNot(pRes->pFans[0]);
Alan Mishchenko committed
2074 2075 2076 2077
            Kit_TruthCopy( pTruth, pCofs2[0], pObj->nFans );
        }
        else if ( fEquals[1][0] )
        {
2078 2079
            *pPar = Abc_LitNot(*pPar);
            pRes->pFans[1] = Abc_LitNot(pRes->pFans[1]);
Alan Mishchenko committed
2080 2081 2082 2083
            Kit_TruthCopy( pTruth, pCofs2[1], pObj->nFans );
        }
        else if ( fEquals[1][1] )
        {
2084 2085 2086
            *pPar = Abc_LitNot(*pPar);
            pRes->pFans[0] = Abc_LitNot(pRes->pFans[0]);  
            pRes->pFans[1] = Abc_LitNot(pRes->pFans[1]);
Alan Mishchenko committed
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
            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
2097
        assert( Kit_DsdObjTruth(pObj) == pTruth );
Alan Mishchenko committed
2098
        Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pRes->pFans + 1, nDecMux );
Alan Mishchenko committed
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
        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
2113 2114
        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
2115 2116 2117
        if ( uSupp0 == 0 || uSupp1 == 0 )
        {
            pObj->fMark = 0;
Alan Mishchenko committed
2118
            Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pPar, nDecMux );
Alan Mishchenko committed
2119 2120 2121 2122 2123 2124 2125 2126 2127
            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
2128 2129
            iLit0 = Kit_WordFindFirstBit( uSupp0 & ~uSupp1 );
            iLit1 = Kit_WordFindFirstBit( uSupp1 & ~uSupp0 );
Alan Mishchenko committed
2130
            // get four cofactors                                        
Alan Mishchenko committed
2131 2132 2133 2134
            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
2135 2136 2137 2138 2139 2140 2141 2142
            // 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
2143 2144
                pRes = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, 3 );
                Kit_DsdObjTruth(pRes)[0] = 0xCACACACA;
Alan Mishchenko committed
2145
                pRes->nRefs++;
Alan Mishchenko committed
2146
                pRes->nFans = 3;
Alan Mishchenko committed
2147 2148 2149
                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
2150
                // update the node
Alan Mishchenko committed
2151
//                if ( fEquals[0][0] && fEquals[0][1] )
Alan Mishchenko committed
2152
//                    Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[0][1], pObj->nFans, i );
Alan Mishchenko committed
2153
//                else
Alan Mishchenko committed
2154 2155
//                    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
2156
                if ( fEquals[1][0] && fEquals[1][1] )
2157
                    pRes->pFans[0] = Abc_LitNot(pRes->pFans[0]);
Alan Mishchenko committed
2158
                // decompose the remainder
Alan Mishchenko committed
2159
                Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pPar, nDecMux );
Alan Mishchenko committed
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
                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
2173 2174 2175 2176 2177 2178
            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
2179 2180 2181 2182 2183
            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
2184
            pRes = Kit_DsdObjAlloc( pNtk, KIT_DSD_AND, 2 );
Alan Mishchenko committed
2185
            pRes->nRefs++;
Alan Mishchenko committed
2186
            pRes->nFans = 2;
Alan Mishchenko committed
2187 2188
            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
2189 2190
            if ( !fPairs[0][1] && !fPairs[0][2] && !fPairs[0][3] ) // 00
            {
2191 2192
                pRes->pFans[0] = Abc_LitNot(pRes->pFans[0]);  
                pRes->pFans[1] = Abc_LitNot(pRes->pFans[1]);
Alan Mishchenko committed
2193
                Kit_TruthMuxVar( pTruth, pCofs4[1][1], pCofs4[0][0], pObj->nFans, k );
Alan Mishchenko committed
2194 2195 2196
            }
            else if ( !fPairs[1][0] && !fPairs[1][2] && !fPairs[1][3] ) // 01
            {
2197
                pRes->pFans[1] = Abc_LitNot(pRes->pFans[1]);  
Alan Mishchenko committed
2198
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[0][1], pObj->nFans, k );
Alan Mishchenko committed
2199 2200 2201
            }
            else if ( !fPairs[2][0] && !fPairs[2][1] && !fPairs[2][3] ) // 10
            {
2202
                pRes->pFans[0] = Abc_LitNot(pRes->pFans[0]);  
Alan Mishchenko committed
2203
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[1][0], pObj->nFans, k );
Alan Mishchenko committed
2204 2205 2206 2207 2208 2209 2210 2211
            }
            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
2212
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[1][1], pObj->nFans, k );
Alan Mishchenko committed
2213 2214 2215 2216 2217 2218 2219
//                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
2220
                Kit_TruthMuxVar( pTruth, pCofs4[0][0], pCofs4[0][1], pObj->nFans, k );
Alan Mishchenko committed
2221 2222
            }
            // decompose the remainder
Alan Mishchenko committed
2223
            Kit_DsdDecompose_rec( pNtk, pObj, uSupp, pPar, nDecMux );
Alan Mishchenko committed
2224 2225 2226
            return;
        }
    }
Alan Mishchenko committed
2227

Alan Mishchenko committed
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
    // 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
2245
        pObj->pFans[2] = pObj->pFans[iBestVar];
Alan Mishchenko committed
2246 2247 2248 2249 2250 2251
        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
2252

Alan Mishchenko committed
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2266
Kit_DsdNtk_t * Kit_DsdDecomposeInt( unsigned * pTruth, int nVars, int nDecMux )
Alan Mishchenko committed
2267
{
Alan Mishchenko committed
2268 2269
    Kit_DsdNtk_t * pNtk;
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
2270
    unsigned uSupp;
Alan Mishchenko committed
2271 2272
    int i, nVarsReal;
    assert( nVars <= 16 );
Alan Mishchenko committed
2273
    pNtk = Kit_DsdNtkAlloc( nVars );
2274
    pNtk->Root = Abc_Var2Lit( pNtk->nVars, 0 );
Alan Mishchenko committed
2275
    // create the first node
Alan Mishchenko committed
2276
    pObj = Kit_DsdObjAlloc( pNtk, KIT_DSD_PRIME, nVars );
Alan Mishchenko committed
2277
    assert( pNtk->pNodes[0] == pObj );
Alan Mishchenko committed
2278
    for ( i = 0; i < nVars; i++ )
2279
       pObj->pFans[i] = Abc_Var2Lit( i, 0 );
Alan Mishchenko committed
2280
    Kit_TruthCopy( Kit_DsdObjTruth(pObj), pTruth, nVars );
Alan Mishchenko committed
2281
    uSupp = Kit_TruthSupport( pTruth, nVars );
Alan Mishchenko committed
2282
    // consider special cases
Alan Mishchenko committed
2283
    nVarsReal = Kit_WordCountOnes( uSupp );
Alan Mishchenko committed
2284 2285 2286 2287
    if ( nVarsReal == 0 )
    {
        pObj->Type = KIT_DSD_CONST1;
        pObj->nFans = 0;
Alan Mishchenko committed
2288
        if ( pTruth[0] == 0 )
2289
             pNtk->Root = Abc_LitNot(pNtk->Root);
Alan Mishchenko committed
2290 2291 2292 2293 2294 2295
        return pNtk;
    }
    if ( nVarsReal == 1 )
    {
        pObj->Type = KIT_DSD_VAR;
        pObj->nFans = 1;
2296
        pObj->pFans[0] = Abc_Var2Lit( Kit_WordFindFirstBit(uSupp), (pTruth[0] & 1) );
Alan Mishchenko committed
2297 2298
        return pNtk;
    }
Alan Mishchenko committed
2299
    Kit_DsdDecompose_rec( pNtk, pNtk->pNodes[0], uSupp, &pNtk->Root, nDecMux );
Alan Mishchenko committed
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
    return pNtk;
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2314 2315 2316 2317 2318 2319 2320 2321 2322
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
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
  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
2343 2344 2345 2346 2347 2348 2349 2350 2351
  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
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
/*
    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 );
2362
        pNew->Root = Abc_Var2Lit( pObjNew->Id, (int)(pTruth[0] == 0) );
Alan Mishchenko committed
2363 2364 2365 2366 2367
        return pNew;
    }
    if ( nVars == 1 )
    {
        pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_VAR, 1 );
2368 2369
        pObjNew->pFans[0] = Abc_Var2Lit( 0, 0 );
        pNew->Root = Abc_Var2Lit( pObjNew->Id, (int)(pTruth[0] != 0xAAAAAAAA) );
Alan Mishchenko committed
2370 2371 2372
        return pNew;
    }
*/
Alan Mishchenko committed
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
    return Kit_DsdDecomposeInt( pTruth, nVars, nDecMux );
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2387
int Kit_DsdTestCofs( Kit_DsdNtk_t * pNtk, unsigned * pTruthInit )
Alan Mishchenko committed
2388
{
Alan Mishchenko committed
2389 2390
    Kit_DsdNtk_t * pNtk0, * pNtk1, * pTemp;
//    Kit_DsdObj_t * pRoot;
Alan Mishchenko committed
2391 2392
    unsigned * pCofs2[2] = { pNtk->pMem, pNtk->pMem + Kit_TruthWordNum(pNtk->nVars) };
    unsigned i, * pTruth;
Alan Mishchenko committed
2393
    int fVerbose = 1;
Alan Mishchenko committed
2394
    int RetValue = 0;
Alan Mishchenko committed
2395

Alan Mishchenko committed
2396
    pTruth = pTruthInit;
Alan Mishchenko committed
2397 2398
//    pRoot = Kit_DsdNtkRoot(pNtk);
//    pTruth = Kit_DsdObjTruth(pRoot);
Alan Mishchenko committed
2399
//    assert( pRoot->nFans == pNtk->nVars );
Alan Mishchenko committed
2400 2401 2402 2403 2404 2405 2406

    if ( fVerbose )
    {
        printf( "Function: " );
//        Extra_PrintBinary( stdout, pTruth, (1 << pNtk->nVars) ); 
        Extra_PrintHexadecimal( stdout, pTruth, pNtk->nVars ); 
        printf( "\n" );
2407
        Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2408 2409 2410 2411 2412
    }
    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
2413 2414 2415
        pNtk0 = Kit_DsdExpand( pTemp = pNtk0 );
        Kit_DsdNtkFree( pTemp );

Alan Mishchenko committed
2416 2417 2418
        if ( fVerbose )
        {
            printf( "Cof%d0: ", i );
2419
            Kit_DsdPrint( stdout, pNtk0 ), printf( "\n" );
Alan Mishchenko committed
2420 2421 2422 2423
        }

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

Alan Mishchenko committed
2427 2428 2429
        if ( fVerbose )
        {
            printf( "Cof%d1: ", i );
2430
            Kit_DsdPrint( stdout, pNtk1 ), printf( "\n" );
Alan Mishchenko committed
2431
        }
Alan Mishchenko committed
2432

Alan Mishchenko committed
2433 2434
//        if ( Kit_DsdCheckVar4Dec2( pNtk0, pNtk1 ) )
//            RetValue = 1;
Alan Mishchenko committed
2435

Alan Mishchenko committed
2436
        Kit_DsdNtkFree( pNtk0 );
Alan Mishchenko committed
2437
        Kit_DsdNtkFree( pNtk1 );
Alan Mishchenko committed
2438 2439 2440
    }
    if ( fVerbose )
        printf( "\n" );
Alan Mishchenko committed
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470

    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
2471
    p = Kit_DsdManAlloc( nVars, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2472
    pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2473
    if ( !Kit_TruthIsEqual( pTruth, pTruthC, nVars ) )
Alan Mishchenko committed
2474 2475 2476 2477 2478
        printf( "Verification failed.\n" );
    Kit_DsdManFree( p );

    Kit_DsdNtkFree( pNtk );
    return Result;
Alan Mishchenko committed
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
}

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2492
void Kit_DsdVerify( Kit_DsdNtk_t * pNtk, unsigned * pTruth, int nVars )
Alan Mishchenko committed
2493
{
Alan Mishchenko committed
2494 2495
    Kit_DsdMan_t * p;
    unsigned * pTruthC;
Alan Mishchenko committed
2496
    p = Kit_DsdManAlloc( nVars, Kit_DsdNtkObjNum(pNtk)+2 );
Alan Mishchenko committed
2497
    pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
    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
2514 2515
void Kit_DsdTest( unsigned * pTruth, int nVars )
{
Alan Mishchenko committed
2516 2517 2518
    Kit_DsdMan_t * p;
    unsigned * pTruthC;
    Kit_DsdNtk_t * pNtk, * pTemp;
Alan Mishchenko committed
2519 2520
    pNtk = Kit_DsdDecompose( pTruth, nVars );

2521
//    if ( Kit_DsdFindLargeBox(pNtk, Abc_Lit2Var(pNtk->Root)) )
Alan Mishchenko committed
2522 2523
//        Kit_DsdPrint( stdout, pNtk );

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

Alan Mishchenko committed
2526 2527
//    printf( "\n" );
//    Kit_DsdPrint( stdout, pNtk );
Alan Mishchenko committed
2528 2529 2530 2531

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

2532
    Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2533

2534
//    if ( Kit_DsdFindLargeBox(pNtk, Abc_Lit2Var(pNtk->Root)) )
Alan Mishchenko committed
2535 2536 2537
//        Kit_DsdTestCofs( pNtk, pTruth );

    // recompute the truth table
Alan Mishchenko committed
2538
    p = Kit_DsdManAlloc( nVars, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2539
    pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2540 2541 2542 2543 2544 2545 2546 2547 2548
//    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
2549

Alan Mishchenko committed
2550 2551 2552 2553

    Kit_DsdNtkFree( pNtk );
}

Alan Mishchenko committed
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
/**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" : "" );
2594
            Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

            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
2608
        p = Kit_DsdManAlloc( 4, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2609
        pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
        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
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

/**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
2654
                Var = Abc_Lit2Var( iFaninLit );
Alan Mishchenko committed
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
                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
2680 2681 2682 2683 2684 2685 2686 2687
    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
2688 2689 2690 2691 2692 2693 2694 2695 2696
    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
2697
    ppCofs[0][0] = ABC_ALLOC( unsigned, 80 * nMemSize );
Alan Mishchenko committed
2698 2699 2700 2701 2702 2703 2704 2705 2706 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 2733 2734 2735 2736 2737 2738 2739 2740 2741
    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
2742
                // free the networks
Alan Mishchenko committed
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
                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 );
2773
                Kit_DsdPrint( stdout, ppNtks[nStep+1][2*i+0] ), printf( "\n" );
Alan Mishchenko committed
2774
                printf( "Cof%d%d: ", nStep+1, 2*i+1 );
2775
                Kit_DsdPrint( stdout, ppNtks[nStep+1][2*i+1] ), printf( "\n" );
Alan Mishchenko committed
2776 2777 2778 2779
            }
        }
    }

Alan Mishchenko committed
2780
    // free the networks
Alan Mishchenko committed
2781 2782 2783 2784
    for ( i = 0; i <  5; i++ )
    for ( k = 0; k < 16; k++ )
        if ( ppNtks[i][k] )
            Kit_DsdNtkFree( ppNtks[i][k] );
Alan Mishchenko committed
2785
    ABC_FREE( ppCofs[0][0] );
Alan Mishchenko committed
2786 2787 2788 2789 2790

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

Alan Mishchenko committed
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
/**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 )
2817
        Kit_DsdPrint( stdout, ppNtks[0] ), printf( "\n" );
Alan Mishchenko committed
2818 2819 2820 2821
    Kit_DsdNtkFree( ppNtks[0] );

    // allocate storage for cofactors
    nMemSize = Kit_TruthWordNum(nVars);
Alan Mishchenko committed
2822
    ppCofs[0][0] = ABC_ALLOC( unsigned, 80 * nMemSize );
Alan Mishchenko committed
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 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
    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 );
2868
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 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
            }
            // 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 );
2917
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
            }
            // 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 );
2968
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
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
            }
            // 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 );
3021
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032
            }
            // 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
3033
    ABC_FREE( ppCofs[0][0] );
Alan Mishchenko committed
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 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277

/**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
3278 3279 3280 3281 3282
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


3283 3284
ABC_NAMESPACE_IMPL_END