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
/**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"
22
#include "misc/extra/extra.h"
Alan Mishchenko committed
23

24 25 26
ABC_NAMESPACE_IMPL_START


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

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

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

Alan Mishchenko committed
37 38 39 40 41 42 43 44 45
  Synopsis    [Allocates the DSD manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Deallocates the DSD manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Deallocates the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Allocates the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Deallocate the DSD network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
164
void Kit_DsdNtkFree( Kit_DsdNtk_t * pNtk )
Alan Mishchenko committed
165
{
Alan Mishchenko committed
166
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
167
    unsigned i;
Alan Mishchenko committed
168
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
Alan Mishchenko committed
169 170 171 172 173
        ABC_FREE( pObj );
    ABC_FREE( pNtk->pSupps );
    ABC_FREE( pNtk->pNodes );
    ABC_FREE( pNtk->pMem );
    ABC_FREE( pNtk );
Alan Mishchenko committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

/**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*************************************************************

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  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
229 230 231 232 233 234 235 236 237
  Synopsis    [Recursively print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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 )
    {
275
        if ( Abc_LitIsCompl(iLit) ) 
276
            fprintf( pFile, "!" );
277
        Kit_DsdPrint2_rec( pFile, pNtk, Abc_Lit2Var(iLit) );
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        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 = " );
301
    if ( Abc_LitIsCompl(pNtk->Root) )
302
        fprintf( pFile, "!" );
303
    Kit_DsdPrint2_rec( pFile, pNtk, Abc_Lit2Var(pNtk->Root) );
304 305 306 307 308 309 310 311 312 313 314 315 316 317
//    fprintf( pFile, "\n" );
}

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

  Synopsis    [Recursively print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

Alan Mishchenko committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    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
349
    if ( pObj->Type == KIT_DSD_PRIME )
350
        Kit_DsdPrintHex( pFile, Kit_DsdObjTruth(pObj), pObj->nFans );
Alan Mishchenko committed
351 352

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

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

  Synopsis    [Print the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  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 )
    {
432
        if ( Abc_LitIsCompl(iLit) ) 
433
            *pBuff++ = '!';
434
        pBuff = Kit_DsdWrite_rec( pBuff, pNtk, Abc_Lit2Var(iLit) );
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
        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 )
{
455
    if ( Abc_LitIsCompl(pNtk->Root) )
456
        *pBuff++ = '!';
457
    pBuff = Kit_DsdWrite_rec( pBuff, pNtk, Abc_Lit2Var(pNtk->Root) );
458 459 460 461 462
    *pBuff = 0;
}

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

Alan Mishchenko committed
463 464 465 466 467 468 469 470 471 472 473 474 475
  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
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    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
493
    Kit_DsdNtk_t * pTemp, * pTemp2;
494 495
//    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 5 );
    pTemp = Kit_DsdDecomposeMux( pTruth, nVars, 8 );
Alan Mishchenko committed
496 497 498 499 500
//    Kit_DsdPrintExpanded( pTemp );
    pTemp2 = Kit_DsdExpand( pTemp );
    Kit_DsdPrint( stdout, pTemp2 );
    Kit_DsdVerify( pTemp2, pTruth, nVars ); 
    Kit_DsdNtkFree( pTemp2 );
Alan Mishchenko committed
501 502 503 504 505
    Kit_DsdNtkFree( pTemp );
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
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     []

***********************************************************************/
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
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
552 553 554 555 556 557 558 559 560
  Synopsis    [Derives the truth table of the DSD node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
561
unsigned * Kit_DsdTruthComputeNode_rec( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, int Id )
Alan Mishchenko committed
562
{
Alan Mishchenko committed
563
    Kit_DsdObj_t * pObj;
Alan Mishchenko committed
564 565 566 567 568 569
    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 );
570
    pTruthRes = (unsigned *)Vec_PtrEntry( p->vTtNodes, Id );
Alan Mishchenko committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

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

    // simple gates
    if ( pObj->Type == KIT_DSD_AND )
    {
        Kit_TruthFill( pTruthRes, pNtk->nVars );
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
610
            Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
611 612 613 614 615 616 617 618 619
        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 );
620
            fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
        }
        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 )
641
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
642 643 644
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
*/
Alan Mishchenko committed
645
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
646
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
647
            Kit_TruthNot( pTruthFans[i], pTruthFans[i], pNtk->nVars );
Alan Mishchenko committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    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++ )
671
        Kit_TruthCopy( (unsigned *)Vec_PtrEntry(p->vTtNodes, i), (unsigned *)Vec_PtrEntry(p->vTtElems, i), p->nVars );
Alan Mishchenko committed
672
    // compute truth table for each node
673
    pTruthRes = Kit_DsdTruthComputeNode_rec( p, pNtk, Abc_Lit2Var(pNtk->Root) );
Alan Mishchenko committed
674
    // complement the truth table if needed
675
    if ( Abc_LitIsCompl(pNtk->Root) )
Alan Mishchenko committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
        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
697 698

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

    // special case: literal of an internal node
    if ( pObj == NULL )
    {
        assert( Id < pNtk->nVars );
Alan Mishchenko committed
706
        assert( !uSupp || uSupp != (uSupp & ~(1<<Id)) );
Alan Mishchenko committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        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
723
        assert( Kit_DsdLitIsLeaf( pNtk, iLit ) );
724 725
        pTruthFans[0] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), uSupp );
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
726 727 728 729 730 731 732
            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
733 734 735 736
    if ( uSupp )
    {
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            if ( uSupp != (uSupp & ~Kit_DsdLitSupport(pNtk, iLit)) )
737
                pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), uSupp );
Alan Mishchenko committed
738 739 740
            else
            {
                pTruthFans[i] = NULL;
Alan Mishchenko committed
741
                nPartial = 1;
Alan Mishchenko committed
742 743 744 745 746
            }
    }
    else
    {
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
747
            pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), uSupp );
Alan Mishchenko committed
748
    }
Alan Mishchenko committed
749 750 751 752 753 754
    // create the truth table

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

Alan Mishchenko committed
778
    if ( uSupp && nPartial )
Alan Mishchenko committed
779 780 781 782 783 784 785 786
    {
        // 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
787
/*
Alan Mishchenko committed
788
    // get the truth table of the prime node
Alan Mishchenko committed
789
    pTruthPrime = Kit_DsdObjTruth( pObj );
Alan Mishchenko committed
790 791 792 793 794 795 796 797 798 799
    // 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
800
        Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
801
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
802 803
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
Alan Mishchenko committed
804
*/
Alan Mishchenko committed
805
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
806
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
807
            Kit_TruthNot( pTruthFans[i], pTruthFans[i], pNtk->nVars );
Alan Mishchenko committed
808 809
    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
810 811 812 813 814 815 816 817 818 819 820 821 822 823
    return pTruthRes;
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

Alan Mishchenko committed
843 844
/**Function*************************************************************

Alan Mishchenko committed
845 846 847 848 849 850 851 852 853 854 855 856 857
  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
858 859 860
    unsigned * pTruthRes, * pTruthFans[16], * pTruthTemp;
    unsigned i, iLit, fCompl, nPartial, uSuppFan, uSuppCur;
//    unsigned m, nMints, * pTruthPrime, * pTruthMint; 
Alan Mishchenko committed
861 862 863 864
    assert( uSupp > 0 );

    // get the node with this ID
    pObj = Kit_DsdNtkObj( pNtk, Id );
865
    pTruthRes = (unsigned *)Vec_PtrEntry( p->vTtNodes, Id );
Alan Mishchenko committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    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
890
        return Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Id, 0 );
Alan Mishchenko committed
891 892 893 894 895 896 897 898 899 900

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

        // create composition/decomposition functions
        if ( pObj->Type == KIT_DSD_AND )
        {
            Kit_TruthFill( pTruthRes, pNtk->nVars );
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
911
                Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
912 913 914 915 916 917 918 919
            return pTruthRes;
        }
        if ( pObj->Type == KIT_DSD_XOR )
        {
            Kit_TruthClear( pTruthRes, pNtk->nVars );
            fCompl = 0;
            Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
            {
920
                fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
                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 )
        {
939
            pTruthFans[i] = Kit_DsdTruthComputeNodeOne_rec( p, pNtk, Abc_Lit2Var(iLit), 0 );
Alan Mishchenko committed
940 941 942 943 944 945 946 947 948 949
            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] )
950
                    Kit_TruthAndPhase( pTruthDec, pTruthDec, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
951
                else
952
                    Kit_TruthAndPhase( pTruthRes, pTruthRes, pTruthFans[i], pNtk->nVars, 0, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
953 954 955 956 957 958 959 960 961
            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 )
            {
962
                fCompl ^= Abc_LitIsCompl(iLit);
Alan Mishchenko committed
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
                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
986
/*
Alan Mishchenko committed
987 988 989 990 991 992 993 994 995 996 997 998 999
    // 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 )
1000
            Kit_TruthAndPhase( pTruthMint, pTruthMint, pTruthFans[i], pNtk->nVars, 0, ((m & (1<<i)) == 0) ^ Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1001 1002
        Kit_TruthOr( pTruthRes, pTruthRes, pTruthMint, pNtk->nVars );
    }
Alan Mishchenko committed
1003
*/
Alan Mishchenko committed
1004
//    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1005
//        assert( !Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1006
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1007
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
1008
            Kit_TruthNot( pTruthFans[i], pTruthFans[i], pNtk->nVars );
Alan Mishchenko committed
1009 1010
    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
1011 1012 1013 1014 1015
    return pTruthRes;
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1108
void Kit_DsdTruthPartial( Kit_DsdMan_t * p, Kit_DsdNtk_t * pNtk, unsigned * pTruthRes, unsigned uSupp )
Alan Mishchenko committed
1109
{
Alan Mishchenko committed
1110
    unsigned * pTruth = Kit_DsdTruthComputeOne( p, pNtk, uSupp );
Alan Mishchenko committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
    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
1131 1132 1133 1134
}

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

Alan Mishchenko committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  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 );
1154 1155
        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
1156 1157 1158 1159 1160 1161 1162 1163
        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
1164
    if ( (int)pObj->nFans > nLutSize ) //+ 1 )
Alan Mishchenko committed
1165 1166 1167 1168 1169
    {
        *pCounter = 1000;
        return 0;
    }
    Kit_DsdObjForEachFanin( pNtk, pObj, iLit, i )
1170
        Kit_DsdCountLuts_rec( pNtk, nLutSize, Abc_Lit2Var(iLit), pCounter );
Alan Mishchenko committed
1171
    (*pCounter)++;
Alan Mishchenko committed
1172 1173
//    if ( (int)pObj->nFans == nLutSize + 1 )
//        (*pCounter)++;
Alan Mishchenko committed
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    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;
1195
    Kit_DsdCountLuts_rec( pNtk, nLutSize, Abc_Lit2Var(pNtk->Root), &Counter );
Alan Mishchenko committed
1196 1197 1198 1199 1200
    if ( Counter >= 1000 )
        return -1;
    return Counter;
}

Alan Mishchenko committed
1201 1202
/**Function*************************************************************

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

  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
1226 1227
/**Function*************************************************************

Alan Mishchenko committed
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 1255
  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
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  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
1269
//    ABC_FREE( pNtk->pSupps );
Alan Mishchenko committed
1270 1271 1272 1273 1274
    Kit_DsdGetSupports( pNtk );
    Kit_DsdNtkForEachObj( pNtk, pObj, i )
    {
        if ( pObj->Type != KIT_DSD_PRIME )
            continue;
1275
        uSupport |= Kit_DsdLitSupport( pNtk, Abc_Var2Lit(pObj->Id,0) );
Alan Mishchenko committed
1276 1277 1278 1279
    }
    return uSupport;
}

Alan Mishchenko committed
1280

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

  Synopsis    [Expands the node.]

  Description [Returns the new literal.]
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Expands the node.]

  Description [Returns the new literal.]
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Expands the node.]

  Description [Returns the new literal.]
               
  SideEffects []

  SeeAlso     []

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

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

    // create new PRIME node
Alan Mishchenko committed
1383
    pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_PRIME, pObj->nFans );
Alan Mishchenko committed
1384
    // copy the truth table
Alan Mishchenko committed
1385 1386
    pTruth = Kit_DsdObjTruth( pObj );
    pTruthNew = Kit_DsdObjTruth( pObjNew );
Alan Mishchenko committed
1387 1388
    Kit_TruthCopy( pTruthNew, pTruth, pObj->nFans );
    // create fanins
Alan Mishchenko committed
1389
    Kit_DsdObjForEachFanin( pNtk, pObj, iLitFanin, i )
Alan Mishchenko committed
1390 1391 1392
    {
        pObjNew->pFans[i] = Kit_DsdExpandNode_rec( pNew, p, iLitFanin );
        // complement the corresponding inputs of the truth table
1393
        if ( Abc_LitIsCompl(pObjNew->pFans[i]) )
Alan Mishchenko committed
1394
        {
1395
            pObjNew->pFans[i] = Abc_LitRegular(pObjNew->pFans[i]);
Alan Mishchenko committed
1396 1397 1398
            Kit_TruthChangePhase( pTruthNew, pObjNew->nFans, i );
        }
    }
Alan Mishchenko committed
1399 1400 1401 1402 1403 1404 1405

    if ( pObj->nFans == 3 && 
        (pTruthNew[0] == 0xCACACACA || pTruthNew[0] == 0xC5C5C5C5 || 
         pTruthNew[0] == 0x3A3A3A3A || pTruthNew[0] == 0x35353535) )
    {
        // translate into regular MUXes
        if ( pTruthNew[0] == 0xC5C5C5C5 )
1406
            pObjNew->pFans[0] = Abc_LitNot(pObjNew->pFans[0]);
Alan Mishchenko committed
1407
        else if ( pTruthNew[0] == 0x3A3A3A3A )
1408
            pObjNew->pFans[1] = Abc_LitNot(pObjNew->pFans[1]);
Alan Mishchenko committed
1409 1410
        else if ( pTruthNew[0] == 0x35353535 )
        {
1411 1412
            pObjNew->pFans[0] = Abc_LitNot(pObjNew->pFans[0]);
            pObjNew->pFans[1] = Abc_LitNot(pObjNew->pFans[1]);
Alan Mishchenko committed
1413 1414 1415
        }
        pTruthNew[0] = 0xCACACACA;
        // resolve the complemented control input
1416
        if ( Abc_LitIsCompl(pObjNew->pFans[2]) )
Alan Mishchenko committed
1417 1418 1419 1420
        {
            unsigned char Temp = pObjNew->pFans[0];
            pObjNew->pFans[0] = pObjNew->pFans[1];
            pObjNew->pFans[1] = Temp;
1421
            pObjNew->pFans[2] = Abc_LitNot(pObjNew->pFans[2]);
Alan Mishchenko committed
1422 1423
        }
        // resolve the complemented true input
1424
        if ( Abc_LitIsCompl(pObjNew->pFans[1]) )
Alan Mishchenko committed
1425
        {
1426 1427 1428
            iLit = Abc_LitNot(iLit);
            pObjNew->pFans[0] = Abc_LitNot(pObjNew->pFans[0]);
            pObjNew->pFans[1] = Abc_LitNot(pObjNew->pFans[1]);
Alan Mishchenko committed
1429
        }
1430
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1431 1432 1433 1434
    }
    else
    {
        // if the incoming phase is complemented, absorb it into the prime node
1435
        if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
1436
            Kit_TruthNot( pTruthNew, pTruthNew, pObj->nFans );
1437
        return Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1438
    }
Alan Mishchenko committed
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
}

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

  Synopsis    [Expands the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

/**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
1546 1547
    Kit_DsdObj_t * pObj;
    Kit_DsdObj_t * pObjNew = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
1548 1549
    unsigned * pTruth, * pTruthNew;
    unsigned i, piLitsNew[16], uSupps[16];
Alan Mishchenko committed
1550
    int iLitFanin, iLitNew;
Alan Mishchenko committed
1551 1552

    // consider the case of simple gate
1553
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
1554
    if ( pObj == NULL )
Alan Mishchenko committed
1555
        return iLit;
Alan Mishchenko committed
1556 1557
    if ( pObj->Type == KIT_DSD_AND )
    {
Alan Mishchenko committed
1558 1559 1560 1561 1562 1563 1564 1565 1566
        // 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
1567 1568
        {
            pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_AND, 2 );
Alan Mishchenko committed
1569 1570
            pObjNew->pFans[0] = Kit_DsdShrink_rec( pNew, p, piLitsNew[i], pPrios );
            pObjNew->pFans[1] = iLitNew;
1571
            iLitNew = Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1572
        }
1573
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1574 1575 1576
    }
    if ( pObj->Type == KIT_DSD_XOR )
    {
Alan Mishchenko committed
1577 1578
        // get the supports
        Kit_DsdObjForEachFanin( p, pObj, iLitFanin, i )
Alan Mishchenko committed
1579
        {
1580
            assert( !Abc_LitIsCompl(iLitFanin) );
Alan Mishchenko committed
1581
            uSupps[i] = Kit_DsdLitSupport( p, iLitFanin );
Alan Mishchenko committed
1582
        }
Alan Mishchenko committed
1583 1584 1585 1586 1587 1588
        // 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
1589
        {
Alan Mishchenko committed
1590 1591 1592
            pObjNew = Kit_DsdObjAlloc( pNew, KIT_DSD_XOR, 2 );
            pObjNew->pFans[0] = Kit_DsdShrink_rec( pNew, p, piLitsNew[i], pPrios );
            pObjNew->pFans[1] = iLitNew;
1593
            iLitNew = Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1594
        }
1595
        return Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(iLit) );
Alan Mishchenko committed
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
    }
    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
1610
        if ( Abc_LitIsCompl(pObjNew->pFans[i]) )
Alan Mishchenko committed
1611
        {
1612
            pObjNew->pFans[i] = Abc_LitRegular(pObjNew->pFans[i]);
Alan Mishchenko committed
1613 1614 1615 1616
            Kit_TruthChangePhase( pTruthNew, pObjNew->nFans, i );
        }
    }
    // if the incoming phase is complemented, absorb it into the prime node
1617
    if ( Abc_LitIsCompl(iLit) )
Alan Mishchenko committed
1618
        Kit_TruthNot( pTruthNew, pTruthNew, pObj->nFans );
1619
    return Abc_Var2Lit( pObjNew->Id, 0 );
Alan Mishchenko committed
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
}

/**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 );
1645
        pNew->Root = Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(p->Root) );
Alan Mishchenko committed
1646 1647 1648 1649 1650 1651
        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];
1652
        pNew->Root = Abc_Var2Lit( pObjNew->Id, Abc_LitIsCompl(p->Root) );
Alan Mishchenko committed
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 1689
        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
1690
                    Weights[k] += pFreqs[v] - 1;
Alan Mishchenko committed
1691 1692
        }
        // find the most frequent fanin
Alan Mishchenko committed
1693 1694
        WeightMax = 0; 
        FaninMax = -1;
Alan Mishchenko committed
1695 1696 1697 1698 1699 1700
        for ( k = 0; k < pObj->nFans; k++ )
            if ( WeightMax < Weights[k] )
            {
                WeightMax = Weights[k];
                FaninMax = k;
            }
Alan Mishchenko committed
1701 1702 1703
        // no need to reorder if there are no frequent fanins
        if ( FaninMax == -1 )
            continue;
Alan Mishchenko committed
1704 1705 1706
        // move the fanins number k to the first place
        nSwaps = 0;
        pIn = Kit_DsdObjTruth(pObj);
Alan Mishchenko committed
1707 1708 1709
        pOut = p->pMem;
//        for ( v = FaninMax; v < ((int)pObj->nFans)-1; v++ )
        for ( v = FaninMax-1; v >= 0; v-- )
Alan Mishchenko committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
        {
            // 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
1720 1721
        if ( nSwaps & 1 )
            Kit_TruthCopy( pOut, pIn, pObj->nFans );
Alan Mishchenko committed
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
    }    
}

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

  Synopsis    [Compute the support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1736
unsigned Kit_DsdGetSupports_rec( Kit_DsdNtk_t * p, int iLit )
Alan Mishchenko committed
1737 1738 1739
{
    Kit_DsdObj_t * pObj;
    unsigned uSupport, k;
Alan Mishchenko committed
1740
    int iFaninLit;
1741
    pObj = Kit_DsdNtkObj( p, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    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
1763
unsigned Kit_DsdGetSupports( Kit_DsdNtk_t * p )
Alan Mishchenko committed
1764 1765
{
    Kit_DsdObj_t * pRoot;
Alan Mishchenko committed
1766
    unsigned uSupport;
Alan Mishchenko committed
1767
    assert( p->pSupps == NULL );
Alan Mishchenko committed
1768
    p->pSupps = ABC_ALLOC( unsigned, p->nNodes );
Alan Mishchenko committed
1769 1770 1771
    // consider simple special cases
    pRoot = Kit_DsdNtkRoot(p);
    if ( pRoot->Type == KIT_DSD_CONST1 )
Alan Mishchenko committed
1772
    {
Alan Mishchenko committed
1773
        assert( p->nNodes == 1 );
Alan Mishchenko committed
1774
        uSupport = p->pSupps[0] = 0;
Alan Mishchenko committed
1775 1776 1777 1778
    }
    if ( pRoot->Type == KIT_DSD_VAR )
    {
        assert( p->nNodes == 1 );
Alan Mishchenko committed
1779
        uSupport = p->pSupps[0] = Kit_DsdLitSupport( p, pRoot->pFans[0] );
Alan Mishchenko committed
1780 1781
    }
    else
Alan Mishchenko committed
1782 1783 1784
        uSupport = Kit_DsdGetSupports_rec( p, p->Root );
    assert( uSupport <= 0xFFFF );
    return uSupport;
Alan Mishchenko committed
1785 1786 1787 1788
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

Alan Mishchenko committed
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
  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 )
{
1826
    return Kit_DsdFindLargeBox_rec( pNtk, Abc_Lit2Var(pNtk->Root), Size );
Alan Mishchenko committed
1827 1828 1829 1830
}

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

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 1914
  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
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
  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++ )
    {
1929
        if ( Abc_Lit2Var(pObj0->pFans[i]) >= 4 )
Alan Mishchenko committed
1930 1931
            continue;
        for ( k = 0; k < pObj1->nFans; k++ )
1932
            if ( Abc_Lit2Var(pObj0->pFans[i]) == Abc_Lit2Var(pObj1->pFans[k]) )
Alan Mishchenko committed
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 1961
                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
1962 1963 1964 1965 1966 1967 1968 1969 1970
  Synopsis    [Performs decomposition of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

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

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

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

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

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

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

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

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

Alan Mishchenko committed
2437
        Kit_DsdNtkFree( pNtk0 );
Alan Mishchenko committed
2438
        Kit_DsdNtkFree( pNtk1 );
Alan Mishchenko committed
2439 2440 2441
    }
    if ( fVerbose )
        printf( "\n" );
Alan Mishchenko committed
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 2471

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

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

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

  Synopsis    [Performs decomposition of the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

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

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

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

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

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

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

Alan Mishchenko committed
2551 2552 2553 2554

    Kit_DsdNtkFree( pNtk );
}

Alan Mishchenko committed
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
/**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" : "" );
2595
            Kit_DsdPrint( stdout, pNtk ), printf( "\n" );
Alan Mishchenko committed
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608

            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
2609
        p = Kit_DsdManAlloc( 4, Kit_DsdNtkObjNum(pNtk) );
Alan Mishchenko committed
2610
        pTruthC = Kit_DsdTruthCompute( p, pNtk );
Alan Mishchenko committed
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
        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
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654

/**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
2655
                Var = Abc_Lit2Var( iFaninLit );
Alan Mishchenko committed
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
                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
2681 2682 2683 2684 2685 2686 2687 2688
    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
2689 2690 2691 2692 2693 2694 2695 2696 2697
    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
2698
    ppCofs[0][0] = ABC_ALLOC( unsigned, 80 * nMemSize );
Alan Mishchenko committed
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 2742
    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
2743
                // free the networks
Alan Mishchenko committed
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
                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 );
2774
                Kit_DsdPrint( stdout, ppNtks[nStep+1][2*i+0] ), printf( "\n" );
Alan Mishchenko committed
2775
                printf( "Cof%d%d: ", nStep+1, 2*i+1 );
2776
                Kit_DsdPrint( stdout, ppNtks[nStep+1][2*i+1] ), printf( "\n" );
Alan Mishchenko committed
2777 2778 2779 2780
            }
        }
    }

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

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

Alan Mishchenko committed
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
/**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 )
2818
        Kit_DsdPrint( stdout, ppNtks[0] ), printf( "\n" );
Alan Mishchenko committed
2819 2820 2821 2822
    Kit_DsdNtkFree( ppNtks[0] );

    // allocate storage for cofactors
    nMemSize = Kit_TruthWordNum(nVars);
Alan Mishchenko committed
2823
    ppCofs[0][0] = ABC_ALLOC( unsigned, 80 * nMemSize );
Alan Mishchenko committed
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 2868
    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 );
2869
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
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 2917
            }
            // 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 );
2918
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
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 2968
            }
            // 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 );
2969
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021
            }
            // 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 );
3022
                Kit_DsdPrint( stdout, ppNtks[i] ), printf( "\n" );
Alan Mishchenko committed
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
            }
            // 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
3034
    ABC_FREE( ppCofs[0][0] );
Alan Mishchenko committed
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 3278

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


3284 3285
ABC_NAMESPACE_IMPL_END