abcPrint.c 66.9 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
/**CFile****************************************************************

  FileName    [abcPrint.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Printing statistics.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21
#include <math.h>
22 23 24 25 26 27
#include "base/abc/abc.h"
#include "bool/dec/dec.h"
#include "base/main/main.h"
#include "map/mio/mio.h"
#include "aig/aig/aig.h"
#include "map/if/if.h"
28 29

#ifdef ABC_USE_CUDD
30
#include "bdd/extrab/extraBdd.h"
31
#endif
Alan Mishchenko committed
32

33 34 35 36
#ifdef WIN32
#include <windows.h>
#endif

37 38 39
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
40 41 42 43
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
44 45 46
//extern int s_TotalNodes = 0;
//extern int s_TotalChanges = 0;

47
abctime s_MappingTime = 0;
Alan Mishchenko committed
48
int s_MappingMem = 0;
49 50
abctime s_ResubTime = 0;
abctime s_ResynTime = 0;
Alan Mishchenko committed
51

Alan Mishchenko committed
52
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
53
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
54 55 56 57
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  Synopsis    [If the network is best, saves it in "best.blif" and returns 1.]

  Description [If the networks are incomparable, saves the new network, 
  returns its parameters in the internal parameter structure, and returns 1.
  If the new network is not a logic network, quits without saving and returns 0.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkCompareAndSaveBest( Abc_Ntk_t * pNtk )
{
    extern void Io_Write( Abc_Ntk_t * pNtk, char * pFileName, Io_FileType_t FileType );
    static struct ParStruct {
        char * pName;  // name of the best saved network
        int    Depth;  // depth of the best saved network
        int    Flops;  // flops in the best saved network 
        int    Nodes;  // nodes in the best saved network
77
        int    Edges;  // edges in the best saved network
Alan Mishchenko committed
78 79 80
        int    nPis;   // the number of primary inputs
        int    nPos;   // the number of primary outputs
    } ParsNew, ParsBest = { 0 };
81
    char * pFileNameOut;
Alan Mishchenko committed
82
    // free storage for the name
Alan Mishchenko committed
83 84
    if ( pNtk == NULL )
    {
Alan Mishchenko committed
85
        ABC_FREE( ParsBest.pName );
Alan Mishchenko committed
86 87 88 89 90 91 92 93 94
        return 0;
    }
    // quit if not a logic network
    if ( !Abc_NtkIsLogic(pNtk) )
        return 0;
    // get the parameters
    ParsNew.Depth = Abc_NtkLevel( pNtk );
    ParsNew.Flops = Abc_NtkLatchNum( pNtk );
    ParsNew.Nodes = Abc_NtkNodeNum( pNtk );
95
    ParsNew.Edges = Abc_NtkGetTotalFanins( pNtk );
Alan Mishchenko committed
96 97 98
    ParsNew.nPis  = Abc_NtkPiNum( pNtk );
    ParsNew.nPos  = Abc_NtkPoNum( pNtk );
    // reset the parameters if the network has the same name
Alan Mishchenko committed
99 100 101 102
    if (  ParsBest.pName == NULL ||
          strcmp(ParsBest.pName, pNtk->pName) ||
          ParsBest.Depth >  ParsNew.Depth ||
         (ParsBest.Depth == ParsNew.Depth && ParsBest.Flops >  ParsNew.Flops) ||
103
         (ParsBest.Depth == ParsNew.Depth && ParsBest.Flops == ParsNew.Flops && ParsBest.Edges >  ParsNew.Edges) )
Alan Mishchenko committed
104
    {
Alan Mishchenko committed
105
        ABC_FREE( ParsBest.pName );
Alan Mishchenko committed
106
        ParsBest.pName = Extra_UtilStrsav( pNtk->pName );
107 108 109
        ParsBest.Depth = ParsNew.Depth;
        ParsBest.Flops = ParsNew.Flops;
        ParsBest.Nodes = ParsNew.Nodes;
110
        ParsBest.Edges = ParsNew.Edges;
111
        ParsBest.nPis  = ParsNew.nPis;
Alan Mishchenko committed
112 113
        ParsBest.nPos  = ParsNew.nPos;
        // writ the network
114 115 116 117
        if ( strcmp(pNtk->pSpec + strlen(pNtk->pSpec) - strlen("_best.blif"), "_best.blif") )
            pFileNameOut = Extra_FileNameGenericAppend( pNtk->pSpec, "_best.blif" );
        else
            pFileNameOut = pNtk->pSpec;
118
        Io_Write( pNtk, pFileNameOut, IO_FILE_BLIF );
Alan Mishchenko committed
119 120 121 122 123 124 125
        return 1;
    }
    return 0;
}

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

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  Synopsis    [Collects memory usage.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
double Abc_NtkMemory( Abc_Ntk_t * p )
{
    Abc_Obj_t * pObj; int i;
    double Memory = sizeof(Abc_Ntk_t);
    Memory += sizeof(Abc_Obj_t) * Abc_NtkObjNum(p);
    Memory += Vec_PtrMemory(p->vPis);
    Memory += Vec_PtrMemory(p->vPos);
    Memory += Vec_PtrMemory(p->vCis);
    Memory += Vec_PtrMemory(p->vCos);
    Memory += Vec_PtrMemory(p->vObjs);
    Memory += Vec_IntMemory(&p->vTravIds);
    Memory += Vec_IntMemory(p->vLevelsR);
    Abc_NtkForEachObj( p, pObj, i )
        Memory += sizeof(int) * (Vec_IntCap(&pObj->vFanins) + Vec_IntCap(&pObj->vFanouts));
    return Memory;
}

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

Alan Mishchenko committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  Synopsis    [Marks nodes for power-optimization.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Abc_NtkMfsTotalSwitching( Abc_Ntk_t * pNtk )
{
    extern Aig_Man_t * Abc_NtkToDar( Abc_Ntk_t * pNtk, int fExors, int fRegisters );
    extern Vec_Int_t * Saig_ManComputeSwitchProbs( Aig_Man_t * p, int nFrames, int nPref, int fProbOne );
    Vec_Int_t * vSwitching;
    float * pSwitching;
    Abc_Ntk_t * pNtkStr;
    Aig_Man_t * pAig;
    Aig_Obj_t * pObjAig;
    Abc_Obj_t * pObjAbc, * pObjAbc2;
    float Result = (float)0;
    int i;
    // strash the network
    pNtkStr = Abc_NtkStrash( pNtk, 0, 1, 0 );
    Abc_NtkForEachObj( pNtk, pObjAbc, i )
178
        if ( Abc_ObjRegular((Abc_Obj_t *)pObjAbc->pTemp)->Type == ABC_FUNC_NONE || (!Abc_ObjIsCi(pObjAbc) && !Abc_ObjIsNode(pObjAbc)) )
Alan Mishchenko committed
179 180 181 182 183 184 185
            pObjAbc->pTemp = NULL;
    // map network into an AIG
    pAig = Abc_NtkToDar( pNtkStr, 0, (int)(Abc_NtkLatchNum(pNtk) > 0) );
    vSwitching = Saig_ManComputeSwitchProbs( pAig, 48, 16, 0 );
    pSwitching = (float *)vSwitching->pArray;
    Abc_NtkForEachObj( pNtk, pObjAbc, i )
    {
186
        if ( (pObjAbc2 = Abc_ObjRegular((Abc_Obj_t *)pObjAbc->pTemp)) && (pObjAig = Aig_Regular((Aig_Obj_t *)pObjAbc2->pTemp)) )
Alan Mishchenko committed
187
        {
Alan Mishchenko committed
188
            Result += Abc_ObjFanoutNum(pObjAbc) * pSwitching[pObjAig->Id];
189
//            Abc_ObjPrint( stdout, pObjAbc );
Alan Mishchenko committed
190 191
//            printf( "%d = %.2f\n", i, Abc_ObjFanoutNum(pObjAbc) * pSwitching[pObjAig->Id] );
        }
Alan Mishchenko committed
192 193 194 195 196 197 198 199 200
    }
    Vec_IntFree( vSwitching );
    Aig_ManStop( pAig );
    Abc_NtkDelete( pNtkStr );
    return Result;
}

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

201 202 203 204 205 206 207 208 209 210 211
  Synopsis    [Compute area using LUT library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
float Abc_NtkGetArea( Abc_Ntk_t * pNtk )
{
212
    If_LibLut_t * pLutLib;
213 214 215 216 217
    Abc_Obj_t * pObj;
    float Counter = 0.0;
    int i;
    assert( Abc_NtkIsLogic(pNtk) );
    // get the library
218
    pLutLib = (If_LibLut_t *)Abc_FrameReadLibLut();
219 220 221 222 223 224 225 226 227 228
    if ( pLutLib && pLutLib->LutMax >= Abc_NtkGetFaninMax(pNtk) )
    {
        Abc_NtkForEachNode( pNtk, pObj, i )
            Counter += pLutLib->pLutAreas[Abc_ObjFaninNum(pObj)];
    }
    return Counter;
}

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

Alan Mishchenko committed
229 230 231 232 233 234 235 236 237
  Synopsis    [Print the vital stats of the network.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
238
void Abc_NtkPrintStats( Abc_Ntk_t * pNtk, int fFactored, int fSaveBest, int fDumpResult, int fUseLutLib, int fPrintMuxes, int fPower, int fGlitch, int fSkipBuf, int fSkipSmall, int fPrintMem )
Alan Mishchenko committed
239
{
240
    int nSingles = fSkipBuf ? Abc_NtkGetBufNum(pNtk) : 0;
241 242 243 244 245
    if ( fPrintMuxes && Abc_NtkIsStrash(pNtk) )
    {
        extern int Abc_NtkCountMuxes( Abc_Ntk_t * pNtk );
        int nXors = Abc_NtkGetExorNum(pNtk);
        int nMuxs = Abc_NtkCountMuxes(pNtk) - nXors;
246
        int nAnds = Abc_NtkNodeNum(pNtk) - (nMuxs + nXors) * 3 - nSingles;
247 248 249 250 251 252 253
        Abc_Print( 1, "XMA stats:  " );
        Abc_Print( 1,"Xor =%7d (%6.2f %%)  ", nXors, 300.0 * nXors / Abc_NtkNodeNum(pNtk) );
        Abc_Print( 1,"Mux =%7d (%6.2f %%)  ", nMuxs, 300.0 * nMuxs / Abc_NtkNodeNum(pNtk) );
        Abc_Print( 1,"And =%7d (%6.2f %%)",   nAnds, 100.0 * nAnds / Abc_NtkNodeNum(pNtk) );
        Abc_Print( 1,"\n" );
        return;
    }
Alan Mishchenko committed
254 255
    if ( fSaveBest )
        Abc_NtkCompareAndSaveBest( pNtk );
256
/*
Alan Mishchenko committed
257 258 259
    if ( fDumpResult )
    {
        char Buffer[1000] = {0};
260
        const char * pNameGen = pNtk->pSpec? Extra_FileNameGeneric( pNtk->pSpec ) : "nameless_";
Alan Mishchenko committed
261 262
        sprintf( Buffer, "%s_dump.blif", pNameGen );
        Io_Write( pNtk, Buffer, IO_FILE_BLIF );
Alan Mishchenko committed
263
        if ( pNtk->pSpec ) ABC_FREE( pNameGen );
Alan Mishchenko committed
264
    }
265
*/
Alan Mishchenko committed
266

Alan Mishchenko committed
267 268
//    if ( Abc_NtkIsStrash(pNtk) )
//        Abc_AigCountNext( pNtk->pManFunc );
Alan Mishchenko committed
269

270 271
#ifdef WIN32
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15 ); // bright
272
    Abc_Print( 1,"%-30s:", pNtk->pName );
273 274
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 7 );  // normal
#else
275
    Abc_Print( 1,"%s%-30s:%s", "\033[1;37m", pNtk->pName, "\033[0m" );  // bright
276
#endif
277
    Abc_Print( 1," i/o =%5d/%5d", Abc_NtkPiNum(pNtk), Abc_NtkPoNum(pNtk) );
278
    if ( Abc_NtkConstrNum(pNtk) )
279 280
        Abc_Print( 1,"(c=%d)", Abc_NtkConstrNum(pNtk) );
    Abc_Print( 1,"  lat =%5d", Abc_NtkLatchNum(pNtk) );
281 282
    if ( pNtk->nBarBufs )
        Abc_Print( 1,"(b=%d)", pNtk->nBarBufs );
Alan Mishchenko committed
283 284
    if ( Abc_NtkIsNetlist(pNtk) )
    {
285
        Abc_Print( 1,"  net =%5d", Abc_NtkNetNum(pNtk) );
286
        Abc_Print( 1,"  nd =%5d",  fSkipSmall ? Abc_NtkGetLargeNodeNum(pNtk) : Abc_NtkNodeNum(pNtk) - nSingles );
287 288
        Abc_Print( 1,"  wbox =%3d", Abc_NtkWhiteboxNum(pNtk) );
        Abc_Print( 1,"  bbox =%3d", Abc_NtkBlackboxNum(pNtk) );
Alan Mishchenko committed
289
    }
Alan Mishchenko committed
290
    else if ( Abc_NtkIsStrash(pNtk) )
291 292
    {
        Abc_Print( 1,"  and =%7d", Abc_NtkNodeNum(pNtk) );
293 294
        if ( Abc_NtkGetChoiceNum(pNtk) )
            Abc_Print( 1," (choice = %d)", Abc_NtkGetChoiceNum(pNtk) );
Alan Mishchenko committed
295
    }
296
    else
Alan Mishchenko committed
297
    {
298
        Abc_Print( 1,"  nd =%6d", fSkipSmall ? Abc_NtkGetLargeNodeNum(pNtk) : Abc_NtkNodeNum(pNtk) - nSingles );
299
        Abc_Print( 1,"  edge =%7d", Abc_NtkGetTotalFanins(pNtk) - nSingles );
Alan Mishchenko committed
300
    }
Alan Mishchenko committed
301

Alan Mishchenko committed
302 303 304
    if ( Abc_NtkIsStrash(pNtk) || Abc_NtkIsNetlist(pNtk) )
    {
    }
305
    else if ( Abc_NtkHasSop(pNtk) )
Alan Mishchenko committed
306
    {
Alan Mishchenko committed
307

308
        Abc_Print( 1,"  cube =%6d",  Abc_NtkGetCubeNum(pNtk) - nSingles );
309
        if ( fFactored )
310
            Abc_Print( 1,"  lit(sop) =%6d",  Abc_NtkGetLitNum(pNtk) - nSingles );
Alan Mishchenko committed
311
        if ( fFactored )
312
            Abc_Print( 1,"  lit(fac) =%6d",  Abc_NtkGetLitFactNum(pNtk) - nSingles );
Alan Mishchenko committed
313
    }
Alan Mishchenko committed
314
    else if ( Abc_NtkHasAig(pNtk) )
315
        Abc_Print( 1,"  aig  =%6d",  Abc_NtkGetAigNodeNum(pNtk) - nSingles );
Alan Mishchenko committed
316
    else if ( Abc_NtkHasBdd(pNtk) )
317
        Abc_Print( 1,"  bdd  =%6d",  Abc_NtkGetBddNodeNum(pNtk) - nSingles );
Alan Mishchenko committed
318
    else if ( Abc_NtkHasMapping(pNtk) )
Alan Mishchenko committed
319
    {
320
        int fHasTimeMan = (int)(pNtk->pManTime != NULL);
321
        assert( pNtk->pManFunc == Abc_FrameReadLibGen() );
322 323
        Abc_Print( 1,"  area =%5.2f", Abc_NtkGetMappedArea(pNtk) );
        Abc_Print( 1,"  delay =%5.2f", Abc_NtkDelayTrace(pNtk, NULL, NULL, 0) );
324 325
        if ( !fHasTimeMan && pNtk->pManTime )
        {
326
            Abc_ManTimeStop( pNtk->pManTime );
327 328
            pNtk->pManTime = NULL;
        }
Alan Mishchenko committed
329
    }
Alan Mishchenko committed
330
    else if ( !Abc_NtkHasBlackbox(pNtk) )
Alan Mishchenko committed
331 332 333
    {
        assert( 0 );
    }
Alan Mishchenko committed
334

Alan Mishchenko committed
335
    if ( Abc_NtkIsStrash(pNtk) )
Alan Mishchenko committed
336 337
    {
        extern int Abc_NtkGetMultiRefNum( Abc_Ntk_t * pNtk );
338 339 340
        Abc_Print( 1,"  lev =%3d", Abc_AigLevel(pNtk) );
//        Abc_Print( 1,"  ff = %5d", Abc_NtkNodeNum(pNtk) + 2 * (Abc_NtkCoNum(pNtk)+Abc_NtkGetMultiRefNum(pNtk)) );
//        Abc_Print( 1,"  var = %5d", Abc_NtkCiNum(pNtk) + Abc_NtkCoNum(pNtk)+Abc_NtkGetMultiRefNum(pNtk) );
Alan Mishchenko committed
341
    }
342
    else
343 344 345
        Abc_Print( 1,"  lev = %d", Abc_NtkLevel(pNtk) );
    if ( pNtk->nBarBufs2 )
        Abc_Print( 1,"  buf = %d", pNtk->nBarBufs2 );
Alan Mishchenko committed
346
    if ( fUseLutLib && Abc_FrameReadLibLut() )
347
        Abc_Print( 1,"  delay =%5.2f", Abc_NtkDelayTraceLut(pNtk, 1) );
348
    if ( fUseLutLib && Abc_FrameReadLibLut() )
349
        Abc_Print( 1,"  area =%5.2f", Abc_NtkGetArea(pNtk) );
Alan Mishchenko committed
350
    if ( fPower )
351
        Abc_Print( 1,"  power =%7.2f", Abc_NtkMfsTotalSwitching(pNtk) );
Alan Mishchenko committed
352 353 354
    if ( fGlitch )
    {
        if ( Abc_NtkIsLogic(pNtk) && Abc_NtkGetFaninMax(pNtk) <= 6 )
355
            Abc_Print( 1,"  glitch =%7.2f %%", Abc_NtkMfsTotalGlitching(pNtk, 4000, 8, 0) );
Alan Mishchenko committed
356
        else
357
            printf( "\nCurrently computes glitching only for K-LUT networks with K <= 6." );
Alan Mishchenko committed
358
    }
359 360
    if ( fPrintMem )
        Abc_Print( 1,"  mem =%5.2f MB", Abc_NtkMemory(pNtk)/(1<<20) );
361
    Abc_Print( 1,"\n" );
Alan Mishchenko committed
362 363

    // print the statistic into a file
364 365 366 367 368 369 370 371 372 373 374 375
    if ( fDumpResult )
    {
        FILE * pTable = fopen( "abcstats.txt", "a+" );
        fprintf( pTable, "%s ",  pNtk->pName );
        fprintf( pTable, "%d ", Abc_NtkPiNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkPoNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkNodeNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkGetTotalFanins(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkLevel(pNtk) );
        fprintf( pTable, "\n" );
        fclose( pTable );
    }
Alan Mishchenko committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
/*
    {
        FILE * pTable;
        pTable = fopen( "ibm/seq_stats.txt", "a+" );
//        fprintf( pTable, "%s ",  pNtk->pName );
//        fprintf( pTable, "%d ", Abc_NtkPiNum(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkPoNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkNodeNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkLatchNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkLevel(pNtk) );
        fprintf( pTable, "\n" );
        fclose( pTable );
    }
*/

/*
    // print the statistic into a file
    {
        FILE * pTable;
395 396 397 398 399 400
        pTable = fopen( "ucsb/stats.txt", "a+" );
//        fprintf( pTable, "%s ",  pNtk->pSpec );
        fprintf( pTable, "%d ",  Abc_NtkNodeNum(pNtk) );
//        fprintf( pTable, "%d ",  Abc_NtkLevel(pNtk) );
//        fprintf( pTable, "%.0f ", Abc_NtkGetMappedArea(pNtk) );
//        fprintf( pTable, "%.2f ", Abc_NtkDelayTrace(pNtk) );
Alan Mishchenko committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        fprintf( pTable, "\n" );
        fclose( pTable );
    }
*/

/*
    // print the statistic into a file
    {
        FILE * pTable;
        pTable = fopen( "x/stats_new.txt", "a+" );
        fprintf( pTable, "%s ",  pNtk->pName );
//        fprintf( pTable, "%d ", Abc_NtkPiNum(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkPoNum(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkLevel(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkNodeNum(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkGetTotalFanins(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkLatchNum(pNtk) );
//        fprintf( pTable, "%.2f ", (float)(s_MappingMem)/(float)(1<<20) );
        fprintf( pTable, "%.2f", (float)(s_MappingTime)/(float)(CLOCKS_PER_SEC) );
//        fprintf( pTable, "%.2f", (float)(s_ResynTime)/(float)(CLOCKS_PER_SEC) );
        fprintf( pTable, "\n" );
        fclose( pTable );

        s_ResynTime = 0;
    }
*/

/*
    // print the statistic into a file
    {
        static int Counter = 0;
        extern int timeRetime;
        FILE * pTable;
        Counter++;
        pTable = fopen( "d/stats.txt", "a+" );
        fprintf( pTable, "%s ", pNtk->pName );
//        fprintf( pTable, "%d ", Abc_NtkPiNum(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkPoNum(pNtk) );
//        fprintf( pTable, "%d ", Abc_NtkLatchNum(pNtk) );
        fprintf( pTable, "%d ", Abc_NtkNodeNum(pNtk) );
        fprintf( pTable, "%.2f ", (float)(timeRetime)/(float)(CLOCKS_PER_SEC) );
        fprintf( pTable, "\n" );
        fclose( pTable );
    }
445

Alan Mishchenko committed
446 447

    s_TotalNodes += Abc_NtkNodeNum(pNtk);
448
    printf( "Total nodes = %6d   %6.2f MB   Changes = %6d.\n", 
Alan Mishchenko committed
449 450 451 452 453
        s_TotalNodes, s_TotalNodes * 20.0 / (1<<20), s_TotalChanges );
*/

//    if ( Abc_NtkHasSop(pNtk) )
//        printf( "The total number of cube pairs = %d.\n", Abc_NtkGetCubePairNum(pNtk) );
454

Alan Mishchenko committed
455
    fflush( stdout );
456
    if ( pNtk->pExdc )
457
        Abc_NtkPrintStats( pNtk->pExdc, fFactored, fSaveBest, fDumpResult, fUseLutLib, fPrintMuxes, fPower, fGlitch, fSkipBuf, fSkipSmall, fPrintMem );
Alan Mishchenko committed
458 459 460 461 462 463 464 465 466 467 468 469 470
}

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

  Synopsis    [Prints PIs/POs and LIs/LOs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
471
void Abc_NtkPrintIo( FILE * pFile, Abc_Ntk_t * pNtk, int fPrintFlops )
Alan Mishchenko committed
472
{
Alan Mishchenko committed
473
    Abc_Obj_t * pObj;
Alan Mishchenko committed
474 475
    int i;

476
    fprintf( pFile, "Primary inputs (%d): ", Abc_NtkPiNum(pNtk) );
Alan Mishchenko committed
477
    Abc_NtkForEachPi( pNtk, pObj, i )
478
        fprintf( pFile, " %d=%s", i, Abc_ObjName(pObj) );
Alan Mishchenko committed
479
//        fprintf( pFile, " %s(%d)", Abc_ObjName(pObj), Abc_ObjFanoutNum(pObj) );
480
    fprintf( pFile, "\n" );
Alan Mishchenko committed
481

482
    fprintf( pFile, "Primary outputs (%d):", Abc_NtkPoNum(pNtk) );
Alan Mishchenko committed
483
    Abc_NtkForEachPo( pNtk, pObj, i )
484
        fprintf( pFile, " %d=%s", i, Abc_ObjName(pObj) );
485
    fprintf( pFile, "\n" );
Alan Mishchenko committed
486

487 488 489
    if ( !fPrintFlops )
        return;

490
    fprintf( pFile, "Latches (%d):  ", Abc_NtkLatchNum(pNtk) );
Alan Mishchenko committed
491
    Abc_NtkForEachLatch( pNtk, pObj, i )
492
        fprintf( pFile, " %s(%s=%s)", Abc_ObjName(pObj),
Alan Mishchenko committed
493
            Abc_ObjName(Abc_ObjFanout0(pObj)), Abc_ObjName(Abc_ObjFanin0(pObj)) );
494
    fprintf( pFile, "\n" );
Alan Mishchenko committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
}

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

  Synopsis    [Prints statistics about latches.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPrintLatch( FILE * pFile, Abc_Ntk_t * pNtk )
{
Alan Mishchenko committed
510
    Abc_Obj_t * pLatch, * pFanin;
Alan Mishchenko committed
511
    int i, Counter0, Counter1, Counter2;
Alan Mishchenko committed
512
    int InitNums[4], Init;
Alan Mishchenko committed
513

Alan Mishchenko committed
514
    assert( !Abc_NtkIsNetlist(pNtk) );
Alan Mishchenko committed
515
    if ( Abc_NtkLatchNum(pNtk) == 0 )
Alan Mishchenko committed
516
    {
Alan Mishchenko committed
517 518
        fprintf( pFile, "The network is combinational.\n" );
        return;
Alan Mishchenko committed
519
    }
Alan Mishchenko committed
520

521
    for ( i = 0; i < 4; i++ )
Alan Mishchenko committed
522
        InitNums[i] = 0;
Alan Mishchenko committed
523 524
    Counter0 = Counter1 = Counter2 = 0;
    Abc_NtkForEachLatch( pNtk, pLatch, i )
Alan Mishchenko committed
525
    {
Alan Mishchenko committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
        Init = Abc_LatchInit( pLatch );
        assert( Init < 4 );
        InitNums[Init]++;

        pFanin = Abc_ObjFanin0(Abc_ObjFanin0(pLatch));
        if ( Abc_NtkIsLogic(pNtk) )
        {
            if ( !Abc_NodeIsConst(pFanin) )
                continue;
        }
        else if ( Abc_NtkIsStrash(pNtk) )
        {
            if ( !Abc_AigNodeIsConst(pFanin) )
                continue;
        }
Alan Mishchenko committed
541 542
        else
            assert( 0 );
Alan Mishchenko committed
543 544 545 546

        // the latch input is a constant node
        Counter0++;
        if ( Abc_LatchIsInitDc(pLatch) )
Alan Mishchenko committed
547
        {
Alan Mishchenko committed
548 549 550 551
            Counter1++;
            continue;
        }
        // count the number of cases when the constant is equal to the initial value
Alan Mishchenko committed
552
        if ( Abc_NtkIsStrash(pNtk) )
Alan Mishchenko committed
553 554 555 556 557 558
        {
            if ( Abc_LatchIsInit1(pLatch) == !Abc_ObjFaninC0(pLatch) )
                Counter2++;
        }
        else
        {
Alan Mishchenko committed
559
            if ( Abc_LatchIsInit1(pLatch) == Abc_NodeIsConst1(Abc_ObjFanin0(Abc_ObjFanin0(pLatch))) )
Alan Mishchenko committed
560
                Counter2++;
Alan Mishchenko committed
561
        }
Alan Mishchenko committed
562
    }
Alan Mishchenko committed
563
//    fprintf( pFile, "%-15s:  ", pNtk->pName );
564
    fprintf( pFile, "Total latches = %5d. Init0 = %d. Init1 = %d. InitDC = %d. Const data = %d.\n",
Alan Mishchenko committed
565 566 567
        Abc_NtkLatchNum(pNtk), InitNums[1], InitNums[2], InitNums[3], Counter0 );
//    fprintf( pFile, "Const fanin = %3d. DC init = %3d. Matching init = %3d. ", Counter0, Counter1, Counter2 );
//    fprintf( pFile, "Self-feed latches = %2d.\n", -1 ); //Abc_NtkCountSelfFeedLatches(pNtk) );
Alan Mishchenko committed
568 569 570 571 572 573 574
}

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

  Synopsis    [Prints the distribution of fanins/fanouts in the network.]

  Description []
575

Alan Mishchenko committed
576 577 578 579 580
  SideEffects []

  SeeAlso     []

***********************************************************************/
581
void Abc_NtkFaninFanoutCounters( Abc_Ntk_t * pNtk, Vec_Int_t * vFan, Vec_Int_t * vFon, Vec_Int_t * vFanR, Vec_Int_t * vFonR )
Alan Mishchenko committed
582 583
{
    Abc_Obj_t * pNode;
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    int i, nFanins, nFanouts;
    int nFaninsMax = 0, nFanoutsMax = 0;
    Abc_NtkForEachObj( pNtk, pNode, i )
    {
        nFaninsMax  = Abc_MaxInt( nFaninsMax,  Abc_ObjFaninNum(pNode) );
        nFanoutsMax = Abc_MaxInt( nFanoutsMax, Abc_ObjFanoutNum(pNode) );
    }
    Vec_IntFill( vFan, nFaninsMax + 1, 0 );
    Vec_IntFill( vFon, nFanoutsMax + 1, 0 );
    Vec_IntFill( vFanR, nFaninsMax + 1, 0 );
    Vec_IntFill( vFonR, nFanoutsMax + 1, 0 );
    Abc_NtkForEachObjReverse( pNtk, pNode, i )
    {
        nFanins  = Abc_ObjFaninNum( pNode );
        nFanouts = Abc_ObjFanoutNum( pNode );
        Vec_IntAddToEntry( vFan,  nFanins, 1 );
        Vec_IntAddToEntry( vFon, nFanouts, 1 );
        Vec_IntWriteEntry( vFanR, nFanins, i );
        Vec_IntWriteEntry( vFonR, nFanouts, i );
    }
}
void Abc_NtkInputOutputCounters( Abc_Ntk_t * pNtk, Vec_Int_t * vFan, Vec_Int_t * vFon, Vec_Int_t * vFanR, Vec_Int_t * vFonR )
{
    Abc_Obj_t * pNode;
    int i, nFanins, nFanouts;
    int nFaninsMax = 0, nFanoutsMax = 0;
    Abc_NtkForEachCi( pNtk, pNode, i )
        nFanoutsMax = Abc_MaxInt( nFanoutsMax, Abc_ObjFanoutNum(pNode) );
    Abc_NtkForEachCo( pNtk, pNode, i )
        nFaninsMax  = Abc_MaxInt( nFaninsMax,  Abc_ObjFaninNum(Abc_ObjFanin0(pNode)) );
    Vec_IntFill( vFan, nFaninsMax + 1, 0 );
    Vec_IntFill( vFon, nFanoutsMax + 1, 0 );
    Vec_IntFill( vFanR, nFaninsMax + 1, 0 );
    Vec_IntFill( vFonR, nFanoutsMax + 1, 0 );
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        nFanouts = Abc_ObjFanoutNum( pNode );
        Vec_IntAddToEntry( vFon, nFanouts, 1 );
        Vec_IntWriteEntry( vFonR, nFanouts, Abc_ObjId(pNode) );
    }
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        nFanins  = Abc_ObjFaninNum( Abc_ObjFanin0(pNode) );
        Vec_IntAddToEntry( vFan,  nFanins, 1 );
        Vec_IntWriteEntry( vFanR, nFanins, Abc_ObjId(pNode) );
    }
}
Vec_Int_t * Abc_NtkCollectCoSupps( Abc_Ntk_t * pNtk, int fVerbose )
{
    abctime clk = Abc_Clock();
    Abc_Obj_t * pNode; int i, k;
    Vec_Ptr_t * vNodes = Abc_NtkDfs( pNtk, 0 );
    Vec_Int_t * vFanin, * vFanout, * vTemp = Vec_IntAlloc( 0 );
    Vec_Int_t * vSuppsCo = Vec_IntAlloc( Abc_NtkCoNum(pNtk) );
    Vec_Wec_t * vSupps = Vec_WecStart( Abc_NtkObjNumMax(pNtk) );
    Abc_NtkForEachCi( pNtk, pNode, i )
        Vec_IntPush( Vec_WecEntry(vSupps, Abc_ObjId(pNode)), i );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
    {
        vFanout = Vec_WecEntry(vSupps, Abc_ObjId(pNode));
        for ( k = 0; k < Abc_ObjFaninNum(pNode); k++ )
Alan Mishchenko committed
645
        {
646 647 648
            vFanin = Vec_WecEntry(vSupps, Abc_ObjFaninId(pNode, k));
            Vec_IntTwoMerge2( vFanout, vFanin, vTemp ); 
            ABC_SWAP( Vec_Int_t, *vFanout, *vTemp );
Alan Mishchenko committed
649 650
        }
    }
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    Abc_NtkForEachCo( pNtk, pNode, i )
        Vec_IntPush( vSuppsCo, Vec_IntSize(Vec_WecEntry(vSupps, Abc_ObjFaninId0(pNode))) );
    Vec_WecFree( vSupps );
    Vec_PtrFree( vNodes );
    Vec_IntFree( vTemp );
    if ( fVerbose )
        Abc_PrintTime( 1, "Input  support computation", Abc_Clock() - clk );
    //Vec_IntPrint( vSuppsCo );
    return vSuppsCo;
}
Vec_Int_t * Abc_NtkCollectCiSupps( Abc_Ntk_t * pNtk, int fVerbose )
{
    abctime clk = Abc_Clock();
    Abc_Obj_t * pNode; int i, k;
    Vec_Ptr_t * vNodes = Abc_NtkDfs( pNtk, 0 );
    Vec_Int_t * vFanin, * vFanout, * vTemp = Vec_IntAlloc( 0 );
    Vec_Int_t * vSuppsCi = Vec_IntAlloc( Abc_NtkCiNum(pNtk) );
    Vec_Wec_t * vSupps = Vec_WecStart( Abc_NtkObjNumMax(pNtk) );
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        vFanout = Vec_WecEntry(vSupps, Abc_ObjId(pNode));
        vFanin = Vec_WecEntry(vSupps, Abc_ObjFaninId0(pNode));
        Vec_IntPush( vFanout, i );
        Vec_IntTwoMerge2( vFanin, vFanout, vTemp );
        ABC_SWAP( Vec_Int_t, *vFanin, *vTemp );
    }
    Vec_PtrForEachEntryReverse( Abc_Obj_t *, vNodes, pNode, i )
678
    {
679 680
        vFanout = Vec_WecEntry(vSupps, Abc_ObjId(pNode));
        for ( k = 0; k < Abc_ObjFaninNum(pNode); k++ )
681
        {
682 683 684
            vFanin = Vec_WecEntry(vSupps, Abc_ObjFaninId(pNode, k));
            Vec_IntTwoMerge2( vFanin, vFanout, vTemp );
            ABC_SWAP( Vec_Int_t, *vFanin, *vTemp );
685 686
        }
    }
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    Abc_NtkForEachCi( pNtk, pNode, i )
        Vec_IntPush( vSuppsCi, Vec_IntSize(Vec_WecEntry(vSupps, Abc_ObjId(pNode))) );
    Vec_WecFree( vSupps );
    Vec_PtrFree( vNodes );
    Vec_IntFree( vTemp );
    if ( fVerbose )
        Abc_PrintTime( 1, "Output support computation", Abc_Clock() - clk );
    //Vec_IntPrint( vSuppsCi );
    return vSuppsCi;
}
void Abc_NtkInOutSupportCounters( Abc_Ntk_t * pNtk, Vec_Int_t * vFan, Vec_Int_t * vFon, Vec_Int_t * vFanR, Vec_Int_t * vFonR )
{
    Abc_Obj_t * pNode;
    Vec_Int_t * vSuppsCo = Abc_NtkCollectCoSupps( pNtk, 1 );
    Vec_Int_t * vSuppsCi = Abc_NtkCollectCiSupps( pNtk, 1 );
    int i, nFanins, nFanouts;
    int nFaninsMax  = Vec_IntFindMax( vSuppsCo );
    int nFanoutsMax = Vec_IntFindMax( vSuppsCi );
    Vec_IntFill( vFan, nFaninsMax + 1, 0 );
    Vec_IntFill( vFon, nFanoutsMax + 1, 0 );
    Vec_IntFill( vFanR, nFaninsMax + 1, 0 );
    Vec_IntFill( vFonR, nFanoutsMax + 1, 0 );
    Abc_NtkForEachCo( pNtk, pNode, i )
Alan Mishchenko committed
710
    {
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
        nFanins  = Vec_IntEntry( vSuppsCo, i );
        Vec_IntAddToEntry( vFan,  nFanins, 1 );
        Vec_IntWriteEntry( vFanR, nFanins, Abc_ObjId(pNode) );
    }
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        nFanouts = Vec_IntEntry( vSuppsCi, i );
        Vec_IntAddToEntry( vFon, nFanouts, 1 );
        Vec_IntWriteEntry( vFonR, nFanouts, Abc_ObjId(pNode) );
    }
    Vec_IntFree( vSuppsCo );
    Vec_IntFree( vSuppsCi );
}

Vec_Int_t * Abc_NtkCollectCoCones( Abc_Ntk_t * pNtk, int fVerbose )
{
    abctime clk = Abc_Clock();
    Abc_Obj_t * pNode; int i, k;
    Vec_Ptr_t * vNodes = Abc_NtkDfs( pNtk, 0 );
    Vec_Int_t * vFanin, * vFanout, * vTemp = Vec_IntAlloc( 0 );
    Vec_Int_t * vSuppsCo = Vec_IntAlloc( Abc_NtkCoNum(pNtk) );
    Vec_Wec_t * vSupps = Vec_WecStart( Abc_NtkObjNumMax(pNtk) );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pNode, i )
    {
        vFanout = Vec_WecEntry(vSupps, Abc_ObjId(pNode));
        for ( k = 0; k < Abc_ObjFaninNum(pNode); k++ )
        {
            vFanin = Vec_WecEntry(vSupps, Abc_ObjFaninId(pNode, k));
            Vec_IntTwoMerge2( vFanout, vFanin, vTemp ); 
            ABC_SWAP( Vec_Int_t, *vFanout, *vTemp );
        }
        Vec_IntPush( vFanout, i );
    }
    Abc_NtkForEachCo( pNtk, pNode, i )
        Vec_IntPush( vSuppsCo, Vec_IntSize(Vec_WecEntry(vSupps, Abc_ObjFaninId0(pNode))) );
    Vec_WecFree( vSupps );
    Vec_PtrFree( vNodes );
    Vec_IntFree( vTemp );
    if ( fVerbose )
        Abc_PrintTime( 1, "Input  cone computation", Abc_Clock() - clk );
    //Vec_IntPrint( vSuppsCo );
    return vSuppsCo;
}
Vec_Int_t * Abc_NtkCollectCiCones( Abc_Ntk_t * pNtk, int fVerbose )
{
    abctime clk = Abc_Clock();
    Abc_Obj_t * pNode; int i, k;
    Vec_Ptr_t * vNodes = Abc_NtkDfs( pNtk, 0 );
    Vec_Int_t * vFanin, * vFanout, * vTemp = Vec_IntAlloc( 0 );
    Vec_Int_t * vSuppsCi = Vec_IntAlloc( Abc_NtkCiNum(pNtk) );
    Vec_Wec_t * vSupps = Vec_WecStart( Abc_NtkObjNumMax(pNtk) );
    Vec_PtrForEachEntryReverse( Abc_Obj_t *, vNodes, pNode, i )
    {
        vFanout = Vec_WecEntry(vSupps, Abc_ObjId(pNode));
        Vec_IntPush( vFanout, i );
        for ( k = 0; k < Abc_ObjFaninNum(pNode); k++ )
        {
            vFanin = Vec_WecEntry(vSupps, Abc_ObjFaninId(pNode, k));
            Vec_IntTwoMerge2( vFanin, vFanout, vTemp );
            ABC_SWAP( Vec_Int_t, *vFanin, *vTemp );
        }
    }
    Abc_NtkForEachCi( pNtk, pNode, i )
        Vec_IntPush( vSuppsCi, Vec_IntSize(Vec_WecEntry(vSupps, Abc_ObjId(pNode))) );
    Vec_WecFree( vSupps );
    Vec_PtrFree( vNodes );
    Vec_IntFree( vTemp );
    if ( fVerbose )
        Abc_PrintTime( 1, "Output cone computation", Abc_Clock() - clk );
    //Vec_IntPrint( vSuppsCi );
    return vSuppsCi;
}
void Abc_NtkInOutConeCounters( Abc_Ntk_t * pNtk, Vec_Int_t * vFan, Vec_Int_t * vFon, Vec_Int_t * vFanR, Vec_Int_t * vFonR )
{
    Abc_Obj_t * pNode;
    Vec_Int_t * vSuppsCo = Abc_NtkCollectCoCones( pNtk, 1 );
    Vec_Int_t * vSuppsCi = Abc_NtkCollectCiCones( pNtk, 1 );
    int i, nFanins, nFanouts;
    int nFaninsMax  = Vec_IntFindMax( vSuppsCo );
    int nFanoutsMax = Vec_IntFindMax( vSuppsCi );
    Vec_IntFill( vFan, nFaninsMax + 1, 0 );
    Vec_IntFill( vFon, nFanoutsMax + 1, 0 );
    Vec_IntFill( vFanR, nFaninsMax + 1, 0 );
    Vec_IntFill( vFonR, nFanoutsMax + 1, 0 );
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        nFanins  = Vec_IntEntry( vSuppsCo, i );
        Vec_IntAddToEntry( vFan,  nFanins, 1 );
        Vec_IntWriteEntry( vFanR, nFanins, Abc_ObjId(pNode) );
    }
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        nFanouts = Vec_IntEntry( vSuppsCi, i );
        Vec_IntAddToEntry( vFon, nFanouts, 1 );
        Vec_IntWriteEntry( vFonR, nFanouts, Abc_ObjId(pNode) );
    }
    Vec_IntFree( vSuppsCo );
    Vec_IntFree( vSuppsCi );
}

void Abc_NtkPrintDistribInternal( FILE * pFile, Abc_Ntk_t * pNtk, char * pFanins, char * pFanouts, char * pNode, char * pFanin, char * pFanout, 
                                 Vec_Int_t * vFan, Vec_Int_t * vFon, Vec_Int_t * vFanR, Vec_Int_t * vFonR )
{
    int k, nSizeMax = Abc_MaxInt( Vec_IntSize(vFan), Vec_IntSize(vFon) );
    fprintf( pFile, "The distribution of %s and %s in the network:\n", pFanins, pFanouts );
    fprintf( pFile, "  Number   %s with %s  %s with %s          Repr1             Repr2\n", pNode, pFanin, pNode, pFanout );
    for ( k = 0; k < nSizeMax; k++ )
    {
        int EntryFan = k < Vec_IntSize(vFan) ? Vec_IntEntry(vFan, k) : 0;
        int EntryFon = k < Vec_IntSize(vFon) ? Vec_IntEntry(vFon, k) : 0;
        if ( EntryFan == 0 && EntryFon == 0 )
Alan Mishchenko committed
822
            continue;
823

Alan Mishchenko committed
824
        fprintf( pFile, "%5d : ", k );
825
        if ( EntryFan == 0 )
Alan Mishchenko committed
826 827
            fprintf( pFile, "              " );
        else
828
            fprintf( pFile, "%12d  ", EntryFan );
Alan Mishchenko committed
829
        fprintf( pFile, "    " );
830
        if ( EntryFon == 0 )
Alan Mishchenko committed
831 832
            fprintf( pFile, "              " );
        else
833 834 835 836 837 838 839 840 841 842 843 844
            fprintf( pFile, "%12d  ", EntryFon );

        fprintf( pFile, "        " );
        if ( EntryFan == 0 )
            fprintf( pFile, "              " );
        else
            fprintf( pFile, "%12s  ", Abc_ObjName(Abc_NtkObj(pNtk, Vec_IntEntry(vFanR, k))) );
        fprintf( pFile, "    " );
        if ( EntryFon == 0 )
            fprintf( pFile, "              " );
        else
            fprintf( pFile, "%12s  ", Abc_ObjName(Abc_NtkObj(pNtk, Vec_IntEntry(vFonR, k))) );
Alan Mishchenko committed
845 846 847
        fprintf( pFile, "\n" );
    }
}
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
void Abc_NtkPrintFanio( FILE * pFile, Abc_Ntk_t * pNtk, int fUseFanio, int fUsePio, int fUseSupp, int fUseCone )
{
    Vec_Int_t * vFan = Vec_IntAlloc( 0 );
    Vec_Int_t * vFon = Vec_IntAlloc( 0 );
    Vec_Int_t * vFanR = Vec_IntAlloc( 0 );
    Vec_Int_t * vFonR = Vec_IntAlloc( 0 );
    assert( fUseFanio + fUsePio + fUseSupp + fUseCone == 1 );
    if ( fUseFanio )
    {
        Abc_NtkFaninFanoutCounters( pNtk, vFan, vFon, vFanR, vFonR );
        Abc_NtkPrintDistribInternal( pFile, pNtk, "fanins", "fanouts", "Nodes", "fanin", "fanout", vFan, vFon, vFanR, vFonR ); 
    }
    else if ( fUsePio )
    {
        Abc_NtkInputOutputCounters( pNtk, vFan, vFon, vFanR, vFonR );
        Abc_NtkPrintDistribInternal( pFile, pNtk, "fanins", "fanouts", "I/O", "fanin", "fanout", vFan, vFon, vFanR, vFonR ); 
    }
    else if ( fUseSupp )
    {
        Abc_NtkInOutSupportCounters( pNtk, vFan, vFon, vFanR, vFonR );
        Abc_NtkPrintDistribInternal( pFile, pNtk, "input supports", "output supports", "I/O", "in-supp", "out-supp", vFan, vFon, vFanR, vFonR ); 
    }
    else if ( fUseCone )
    {
        Abc_NtkInOutConeCounters( pNtk, vFan, vFon, vFanR, vFonR );
        Abc_NtkPrintDistribInternal( pFile, pNtk, "input cones", "output cones", "I/O", "in-cone", "out-cone", vFan, vFon, vFanR, vFonR ); 
    }
    Vec_IntFree( vFan );
    Vec_IntFree( vFon );
    Vec_IntFree( vFanR );
    Vec_IntFree( vFonR );
}

Alan Mishchenko committed
881

Alan Mishchenko committed
882 883 884 885 886
/**Function*************************************************************

  Synopsis    [Prints the distribution of fanins/fanouts in the network.]

  Description []
887

Alan Mishchenko committed
888 889 890 891 892
  SideEffects []

  SeeAlso     []

***********************************************************************/
893
void Abc_NtkPrintFanioNew( FILE * pFile, Abc_Ntk_t * pNtk, int fMffc )
Alan Mishchenko committed
894 895 896 897 898 899 900 901 902 903 904 905
{
    char Buffer[100];
    Abc_Obj_t * pNode;
    Vec_Int_t * vFanins, * vFanouts;
    int nFanins, nFanouts, nFaninsMax, nFanoutsMax, nFaninsAll, nFanoutsAll;
    int i, k, nSizeMax;

    // determine the largest fanin and fanout
    nFaninsMax = nFanoutsMax = 0;
    nFaninsAll = nFanoutsAll = 0;
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
906 907
        if ( fMffc && Abc_ObjFanoutNum(pNode) == 1 )
            continue;
Alan Mishchenko committed
908 909 910
        nFanins  = Abc_ObjFaninNum(pNode);
        if ( Abc_NtkIsNetlist(pNtk) )
            nFanouts = Abc_ObjFanoutNum( Abc_ObjFanout0(pNode) );
911 912
        else if ( fMffc )
            nFanouts = Abc_NodeMffcSize(pNode);
Alan Mishchenko committed
913 914 915 916
        else
            nFanouts = Abc_ObjFanoutNum(pNode);
        nFaninsAll  += nFanins;
        nFanoutsAll += nFanouts;
917 918
        nFaninsMax   = Abc_MaxInt( nFaninsMax, nFanins );
        nFanoutsMax  = Abc_MaxInt( nFanoutsMax, nFanouts );
Alan Mishchenko committed
919 920 921
    }

    // allocate storage for fanin/fanout numbers
922
    nSizeMax = Abc_MaxInt( 10 * (Abc_Base10Log(nFaninsMax) + 1), 10 * (Abc_Base10Log(nFanoutsMax) + 1) );
Alan Mishchenko committed
923 924 925 926 927 928
    vFanins  = Vec_IntStart( nSizeMax );
    vFanouts = Vec_IntStart( nSizeMax );

    // count the number of fanins and fanouts
    Abc_NtkForEachNode( pNtk, pNode, i )
    {
929 930
        if ( fMffc && Abc_ObjFanoutNum(pNode) == 1 )
            continue;
Alan Mishchenko committed
931 932 933
        nFanins  = Abc_ObjFaninNum(pNode);
        if ( Abc_NtkIsNetlist(pNtk) )
            nFanouts = Abc_ObjFanoutNum( Abc_ObjFanout0(pNode) );
934 935
        else if ( fMffc )
            nFanouts = Abc_NodeMffcSize(pNode);
Alan Mishchenko committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
        else
            nFanouts = Abc_ObjFanoutNum(pNode);

        if ( nFanins < 10 )
            Vec_IntAddToEntry( vFanins, nFanins, 1 );
        else if ( nFanins < 100 )
            Vec_IntAddToEntry( vFanins, 10 + nFanins/10, 1 );
        else if ( nFanins < 1000 )
            Vec_IntAddToEntry( vFanins, 20 + nFanins/100, 1 );
        else if ( nFanins < 10000 )
            Vec_IntAddToEntry( vFanins, 30 + nFanins/1000, 1 );
        else if ( nFanins < 100000 )
            Vec_IntAddToEntry( vFanins, 40 + nFanins/10000, 1 );
        else if ( nFanins < 1000000 )
            Vec_IntAddToEntry( vFanins, 50 + nFanins/100000, 1 );
        else if ( nFanins < 10000000 )
            Vec_IntAddToEntry( vFanins, 60 + nFanins/1000000, 1 );

        if ( nFanouts < 10 )
            Vec_IntAddToEntry( vFanouts, nFanouts, 1 );
        else if ( nFanouts < 100 )
            Vec_IntAddToEntry( vFanouts, 10 + nFanouts/10, 1 );
        else if ( nFanouts < 1000 )
            Vec_IntAddToEntry( vFanouts, 20 + nFanouts/100, 1 );
        else if ( nFanouts < 10000 )
            Vec_IntAddToEntry( vFanouts, 30 + nFanouts/1000, 1 );
        else if ( nFanouts < 100000 )
            Vec_IntAddToEntry( vFanouts, 40 + nFanouts/10000, 1 );
        else if ( nFanouts < 1000000 )
            Vec_IntAddToEntry( vFanouts, 50 + nFanouts/100000, 1 );
        else if ( nFanouts < 10000000 )
            Vec_IntAddToEntry( vFanouts, 60 + nFanouts/1000000, 1 );
    }

    fprintf( pFile, "The distribution of fanins and fanouts in the network:\n" );
    fprintf( pFile, "         Number   Nodes with fanin  Nodes with fanout\n" );
    for ( k = 0; k < nSizeMax; k++ )
    {
        if ( vFanins->pArray[k] == 0 && vFanouts->pArray[k] == 0 )
            continue;
        if ( k < 10 )
            fprintf( pFile, "%15d : ", k );
        else
        {
980
            sprintf( Buffer, "%d - %d", (int)pow((double)10, k/10) * (k%10), (int)pow((double)10, k/10) * (k%10+1) - 1 );
Alan Mishchenko committed
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
            fprintf( pFile, "%15s : ", Buffer );
        }
        if ( vFanins->pArray[k] == 0 )
            fprintf( pFile, "              " );
        else
            fprintf( pFile, "%12d  ", vFanins->pArray[k] );
        fprintf( pFile, "    " );
        if ( vFanouts->pArray[k] == 0 )
            fprintf( pFile, "              " );
        else
            fprintf( pFile, "%12d  ", vFanouts->pArray[k] );
        fprintf( pFile, "\n" );
    }
    Vec_IntFree( vFanins );
    Vec_IntFree( vFanouts );

997 998
    fprintf( pFile, "Fanins: Max = %d. Ave = %.2f.  Fanouts: Max = %d. Ave =  %.2f.\n",
        nFaninsMax,  1.0*nFaninsAll/Abc_NtkNodeNum(pNtk),
Alan Mishchenko committed
999
        nFanoutsMax, 1.0*nFanoutsAll/Abc_NtkNodeNum(pNtk)  );
Alan Mishchenko committed
1000 1001 1002 1003 1004 1005 1006
/*
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        printf( "%d ", Abc_ObjFanoutNum(pNode) );
    }
    printf( "\n" );
*/
Alan Mishchenko committed
1007
}
Alan Mishchenko committed
1008 1009 1010 1011 1012 1013

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

  Synopsis    [Prints the fanins/fanouts of a node.]

  Description []
1014

Alan Mishchenko committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodePrintFanio( FILE * pFile, Abc_Obj_t * pNode )
{
    Abc_Obj_t * pNode2;
    int i;
    if ( Abc_ObjIsPo(pNode) )
        pNode = Abc_ObjFanin0(pNode);

1027 1028
    fprintf( pFile, "Node %s", Abc_ObjName(pNode) );
    fprintf( pFile, "\n" );
Alan Mishchenko committed
1029

1030
    fprintf( pFile, "Fanins (%d): ", Abc_ObjFaninNum(pNode) );
Alan Mishchenko committed
1031 1032
    Abc_ObjForEachFanin( pNode, pNode2, i )
        fprintf( pFile, " %s", Abc_ObjName(pNode2) );
1033 1034 1035
    fprintf( pFile, "\n" );

    fprintf( pFile, "Fanouts (%d): ", Abc_ObjFaninNum(pNode) );
Alan Mishchenko committed
1036 1037
    Abc_ObjForEachFanout( pNode, pNode2, i )
        fprintf( pFile, " %s", Abc_ObjName(pNode2) );
1038
    fprintf( pFile, "\n" );
Alan Mishchenko committed
1039 1040 1041 1042
}

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

Alan Mishchenko committed
1043 1044 1045
  Synopsis    [Prints the MFFCs of the nodes.]

  Description []
1046

Alan Mishchenko committed
1047 1048 1049 1050 1051 1052 1053 1054 1055
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPrintMffc( FILE * pFile, Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pNode;
    int i;
Alan Mishchenko committed
1056
    extern void Abc_NodeMffcConeSuppPrint( Abc_Obj_t * pNode );
Alan Mishchenko committed
1057
    Abc_NtkForEachNode( pNtk, pNode, i )
1058 1059
        if ( Abc_ObjFanoutNum(pNode) > 1 )
            Abc_NodeMffcConeSuppPrint( pNode );
Alan Mishchenko committed
1060 1061 1062 1063
}

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

Alan Mishchenko committed
1064 1065 1066
  Synopsis    [Prints the factored form of one node.]

  Description []
1067

Alan Mishchenko committed
1068 1069 1070 1071 1072
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1073
void Abc_NtkPrintFactor( FILE * pFile, Abc_Ntk_t * pNtk, int fUseRealNames )
Alan Mishchenko committed
1074 1075 1076
{
    Abc_Obj_t * pNode;
    int i;
Alan Mishchenko committed
1077
    assert( Abc_NtkIsSopLogic(pNtk) );
Alan Mishchenko committed
1078
    Abc_NtkForEachNode( pNtk, pNode, i )
Alan Mishchenko committed
1079
        Abc_NodePrintFactor( pFile, pNode, fUseRealNames );
Alan Mishchenko committed
1080 1081 1082 1083 1084 1085 1086
}

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

  Synopsis    [Prints the factored form of one node.]

  Description []
1087

Alan Mishchenko committed
1088 1089 1090 1091 1092
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1093
void Abc_NodePrintFactor( FILE * pFile, Abc_Obj_t * pNode, int fUseRealNames )
Alan Mishchenko committed
1094
{
Alan Mishchenko committed
1095
    Dec_Graph_t * pGraph;
Alan Mishchenko committed
1096
    Vec_Ptr_t * vNamesIn;
Alan Mishchenko committed
1097
    if ( Abc_ObjIsCo(pNode) )
Alan Mishchenko committed
1098 1099 1100
        pNode = Abc_ObjFanin0(pNode);
    if ( Abc_ObjIsPi(pNode) )
    {
Alan Mishchenko committed
1101
        fprintf( pFile, "Skipping the PI node.\n" );
Alan Mishchenko committed
1102 1103 1104 1105
        return;
    }
    if ( Abc_ObjIsLatch(pNode) )
    {
Alan Mishchenko committed
1106
        fprintf( pFile, "Skipping the latch.\n" );
Alan Mishchenko committed
1107 1108 1109
        return;
    }
    assert( Abc_ObjIsNode(pNode) );
1110
    pGraph = Dec_Factor( (char *)pNode->pData );
Alan Mishchenko committed
1111 1112 1113
    if ( fUseRealNames )
    {
        vNamesIn = Abc_NodeGetFaninNames(pNode);
Alan Mishchenko committed
1114
        Dec_GraphPrint( stdout, pGraph, (char **)vNamesIn->pArray, Abc_ObjName(pNode) );
Alan Mishchenko committed
1115 1116 1117
        Abc_NodeFreeNames( vNamesIn );
    }
    else
Alan Mishchenko committed
1118 1119
        Dec_GraphPrint( stdout, pGraph, (char **)NULL, Abc_ObjName(pNode) );
    Dec_GraphFree( pGraph );
Alan Mishchenko committed
1120 1121
}

Alan Mishchenko committed
1122 1123 1124 1125 1126 1127

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

  Synopsis    [Prints the level stats of the PO node.]

  Description []
1128

Alan Mishchenko committed
1129 1130 1131 1132 1133
  SideEffects []

  SeeAlso     []

***********************************************************************/
1134
void Abc_NtkPrintLevel( FILE * pFile, Abc_Ntk_t * pNtk, int fProfile, int fListNodes, int fVerbose )
Alan Mishchenko committed
1135 1136
{
    Abc_Obj_t * pNode;
Alan Mishchenko committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    int i, k, Length;

    if ( fListNodes )
    {
        int nLevels;
        nLevels = Abc_NtkLevel(pNtk);
        printf( "Nodes by level:\n" );
        for ( i = 0; i <= nLevels; i++ )
        {
            printf( "%2d : ", i );
            Abc_NtkForEachNode( pNtk, pNode, k )
                if ( (int)pNode->Level == i )
                    printf( " %s", Abc_ObjName(pNode) );
            printf( "\n" );
        }
        return;
    }
Alan Mishchenko committed
1154 1155

    // print the delay profile
Alan Mishchenko committed
1156
    if ( fProfile && Abc_NtkHasMapping(pNtk) )
Alan Mishchenko committed
1157 1158 1159 1160 1161 1162 1163
    {
        int nIntervals = 12;
        float DelayMax, DelayCur, DelayDelta;
        int * pLevelCounts;
        int DelayInt, nOutsSum, nOutsTotal;

        // get the max delay and delta
1164
        DelayMax   = Abc_NtkDelayTrace( pNtk, NULL, NULL, 0 );
Alan Mishchenko committed
1165 1166
        DelayDelta = DelayMax/nIntervals;
        // collect outputs by delay
Alan Mishchenko committed
1167
        pLevelCounts = ABC_ALLOC( int, nIntervals );
Alan Mishchenko committed
1168 1169 1170
        memset( pLevelCounts, 0, sizeof(int) * nIntervals );
        Abc_NtkForEachCo( pNtk, pNode, i )
        {
1171 1172 1173 1174
            if ( Abc_ObjIsNode(Abc_ObjFanin0(pNode)) && Abc_ObjFaninNum(Abc_ObjFanin0(pNode)) == 0 )
                DelayInt = 0;
            else
            {
1175
                DelayCur  = Abc_NodeReadArrivalWorst( Abc_ObjFanin0(pNode) );
1176 1177 1178 1179
                DelayInt  = (int)(DelayCur / DelayDelta);
                if ( DelayInt >= nIntervals )
                    DelayInt = nIntervals - 1;
            }
Alan Mishchenko committed
1180 1181 1182 1183 1184 1185 1186 1187
            pLevelCounts[DelayInt]++;
        }

        nOutsSum   = 0;
        nOutsTotal = Abc_NtkCoNum(pNtk);
        for ( i = 0; i < nIntervals; i++ )
        {
            nOutsSum += pLevelCounts[i];
1188
            printf( "[%8.2f - %8.2f] :   COs = %4d.   %5.1f %%\n",
Alan Mishchenko committed
1189 1190
                DelayDelta * i, DelayDelta * (i+1), pLevelCounts[i], 100.0 * nOutsSum/nOutsTotal );
        }
Alan Mishchenko committed
1191
        ABC_FREE( pLevelCounts );
Alan Mishchenko committed
1192 1193 1194
        return;
    }
    else if ( fProfile )
Alan Mishchenko committed
1195 1196 1197 1198
    {
        int LevelMax, * pLevelCounts;
        int nOutsSum, nOutsTotal;

Alan Mishchenko committed
1199
        if ( !Abc_NtkIsStrash(pNtk) )
Alan Mishchenko committed
1200
            Abc_NtkLevel(pNtk);
Alan Mishchenko committed
1201

Alan Mishchenko committed
1202 1203 1204 1205
        LevelMax = 0;
        Abc_NtkForEachCo( pNtk, pNode, i )
            if ( LevelMax < (int)Abc_ObjFanin0(pNode)->Level )
                LevelMax = Abc_ObjFanin0(pNode)->Level;
Alan Mishchenko committed
1206
        pLevelCounts = ABC_ALLOC( int, LevelMax + 1 );
Alan Mishchenko committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
        memset( pLevelCounts, 0, sizeof(int) * (LevelMax + 1) );
        Abc_NtkForEachCo( pNtk, pNode, i )
            pLevelCounts[Abc_ObjFanin0(pNode)->Level]++;

        nOutsSum   = 0;
        nOutsTotal = Abc_NtkCoNum(pNtk);
        for ( i = 0; i <= LevelMax; i++ )
            if ( pLevelCounts[i] )
            {
                nOutsSum += pLevelCounts[i];
                printf( "Level = %4d.  COs = %4d.   %5.1f %%\n", i, pLevelCounts[i], 100.0 * nOutsSum/nOutsTotal );
            }
Alan Mishchenko committed
1219
        ABC_FREE( pLevelCounts );
Alan Mishchenko committed
1220 1221
        return;
    }
Alan Mishchenko committed
1222
    assert( Abc_NtkIsStrash(pNtk) );
Alan Mishchenko committed
1223

1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    if ( fVerbose )
    {
        // find the longest name
        Length = 0;
        Abc_NtkForEachCo( pNtk, pNode, i )
            if ( Length < (int)strlen(Abc_ObjName(pNode)) )
                Length = strlen(Abc_ObjName(pNode));
        if ( Length < 5 )
            Length = 5;
        // print stats for each output
        Abc_NtkForEachCo( pNtk, pNode, i )
        {
            fprintf( pFile, "CO %4d :  %*s    ", i, Length, Abc_ObjName(pNode) );
            Abc_NodePrintLevel( pFile, pNode );
    }
Alan Mishchenko committed
1239 1240 1241 1242 1243 1244 1245 1246
    }
}

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

  Synopsis    [Prints the factored form of one node.]

  Description []
1247

Alan Mishchenko committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodePrintLevel( FILE * pFile, Abc_Obj_t * pNode )
{
    Abc_Obj_t * pDriver;
    Vec_Ptr_t * vNodes;

    pDriver = Abc_ObjIsCo(pNode)? Abc_ObjFanin0(pNode) : pNode;
    if ( Abc_ObjIsPi(pDriver) )
    {
        fprintf( pFile, "Primary input.\n" );
        return;
    }
    if ( Abc_ObjIsLatch(pDriver) )
    {
        fprintf( pFile, "Latch.\n" );
        return;
    }
    if ( Abc_NodeIsConst(pDriver) )
    {
        fprintf( pFile, "Constant %d.\n", !Abc_ObjFaninC0(pNode) );
        return;
    }
    // print the level
    fprintf( pFile, "Level = %3d.  ", pDriver->Level );
    // print the size of MFFC
    fprintf( pFile, "Mffc = %5d.  ", Abc_NodeMffcSize(pDriver) );
    // print the size of the shole cone
    vNodes = Abc_NtkDfsNodes( pNode->pNtk, &pDriver, 1 );
    fprintf( pFile, "Cone = %5d.  ", Vec_PtrSize(vNodes) );
    Vec_PtrFree( vNodes );
    fprintf( pFile, "\n" );
}

Alan Mishchenko committed
1285 1286 1287 1288 1289
/**Function*************************************************************

  Synopsis    [Prints the factored form of one node.]

  Description []
1290

Alan Mishchenko committed
1291 1292 1293 1294 1295 1296 1297
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NodePrintKMap( Abc_Obj_t * pNode, int fUseRealNames )
{
1298
#ifdef ABC_USE_CUDD
Alan Mishchenko committed
1299 1300 1301 1302
    Vec_Ptr_t * vNamesIn;
    if ( fUseRealNames )
    {
        vNamesIn = Abc_NodeGetFaninNames(pNode);
1303
        Extra_PrintKMap( stdout, (DdManager *)pNode->pNtk->pManFunc, (DdNode *)pNode->pData, Cudd_Not(pNode->pData),
Alan Mishchenko committed
1304 1305 1306 1307
            Abc_ObjFaninNum(pNode), NULL, 0, (char **)vNamesIn->pArray );
        Abc_NodeFreeNames( vNamesIn );
    }
    else
1308
        Extra_PrintKMap( stdout, (DdManager *)pNode->pNtk->pManFunc, (DdNode *)pNode->pData, Cudd_Not(pNode->pData),
Alan Mishchenko committed
1309
            Abc_ObjFaninNum(pNode), NULL, 0, NULL );
1310
#endif
Alan Mishchenko committed
1311 1312 1313 1314 1315 1316 1317
}

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

  Synopsis    [Prints statistics about gates used in the network.]

  Description []
1318

Alan Mishchenko committed
1319 1320 1321 1322 1323
  SideEffects []

  SeeAlso     []

***********************************************************************/
1324
void Abc_NtkPrintGates( Abc_Ntk_t * pNtk, int fUseLibrary, int fUpdateProfile )
Alan Mishchenko committed
1325 1326 1327
{
    Abc_Obj_t * pObj;
    int fHasBdds, i;
1328
    int CountConst, CountBuf, CountInv, CountAnd, CountOr, CountOther, CounterTotal, TotalDiff = 0;
Alan Mishchenko committed
1329 1330 1331 1332
    char * pSop;

    if ( fUseLibrary && Abc_NtkHasMapping(pNtk) )
    {
Alan Mishchenko committed
1333
        Mio_Gate_t ** ppGates;
Alan Mishchenko committed
1334
        double Area, AreaTotal;
1335
        int Counter, nGates, i, nGateNameLen;
Alan Mishchenko committed
1336 1337

        // clean value of all gates
1338
        nGates = Mio_LibraryReadGateNum( (Mio_Library_t *)pNtk->pManFunc );
1339
        ppGates = Mio_LibraryReadGateArray( (Mio_Library_t *)pNtk->pManFunc );
Alan Mishchenko committed
1340
        for ( i = 0; i < nGates; i++ )
1341
        {
Alan Mishchenko committed
1342
            Mio_GateSetValue( ppGates[i], 0 );
1343 1344
            if ( fUpdateProfile )
                Mio_GateSetProfile2( ppGates[i], 0 );
1345
        }
Alan Mishchenko committed
1346 1347 1348

        // count the gates by name
        CounterTotal = 0;
1349
        Abc_NtkForEachNodeNotBarBuf( pNtk, pObj, i )
Alan Mishchenko committed
1350 1351
        {
            if ( i == 0 ) continue;
1352
            Mio_GateSetValue( (Mio_Gate_t *)pObj->pData, 1 + Mio_GateReadValue((Mio_Gate_t *)pObj->pData) );
1353 1354
            if ( fUpdateProfile )
                Mio_GateIncProfile2( (Mio_Gate_t *)pObj->pData );
Alan Mishchenko committed
1355
            CounterTotal++;
1356
            // assuming that twin gates follow each other
1357
            if ( Abc_NtkFetchTwinNode(pObj) )
1358
                i++;
Alan Mishchenko committed
1359
        }
1360 1361

        // determine the longest gate name
1362
        nGateNameLen = 5;
1363 1364 1365 1366 1367 1368 1369 1370
        for ( i = 0; i < nGates; i++ )
        {
            Counter = Mio_GateReadValue( ppGates[i] );
            if ( Counter == 0 )
                continue;
            nGateNameLen = Abc_MaxInt( nGateNameLen, strlen(Mio_GateReadName(ppGates[i])) );
        }

Alan Mishchenko committed
1371 1372
        // print the gates
        AreaTotal = Abc_NtkGetMappedArea(pNtk);
Alan Mishchenko committed
1373
        for ( i = 0; i < nGates; i++ )
Alan Mishchenko committed
1374
        {
Alan Mishchenko committed
1375
            Counter = Mio_GateReadValue( ppGates[i] );
1376
            if ( Counter == 0 && Mio_GateReadProfile(ppGates[i]) == 0 )
Alan Mishchenko committed
1377
                continue;
1378 1379
            if ( Mio_GateReadPinNum(ppGates[i]) > 1 )
                TotalDiff += Abc_AbsInt( Mio_GateReadProfile(ppGates[i]) - Mio_GateReadProfile2(ppGates[i]) );
Alan Mishchenko committed
1380
            Area = Counter * Mio_GateReadArea( ppGates[i] );
1381
            printf( "%-*s   Fanin = %2d   Instance = %8d   Area = %10.2f   %6.2f %%   %8d  %8d   %s\n",
1382 1383
                nGateNameLen, Mio_GateReadName( ppGates[i] ),
                Mio_GateReadPinNum( ppGates[i] ),
Alan Mishchenko committed
1384
                Counter, Area, 100.0 * Area / AreaTotal,
1385 1386
                Mio_GateReadProfile(ppGates[i]),
                Mio_GateReadProfile2(ppGates[i]),
Alan Mishchenko committed
1387
                Mio_GateReadForm(ppGates[i]) );
Alan Mishchenko committed
1388
        }
1389
        printf( "%-*s                Instance = %8d   Area = %10.2f   %6.2f %%   AbsDiff = %d\n",
1390
            nGateNameLen, "TOTAL",
1391
            CounterTotal, AreaTotal, 100.0, TotalDiff );
Alan Mishchenko committed
1392 1393 1394 1395 1396 1397 1398
        return;
    }

    if ( Abc_NtkIsAigLogic(pNtk) )
        return;

    // transform logic functions from BDD to SOP
Alan Mishchenko committed
1399
    if ( (fHasBdds = Abc_NtkIsBddLogic(pNtk)) )
Alan Mishchenko committed
1400
    {
1401
        if ( !Abc_NtkBddToSop(pNtk, -1, ABC_INFINITY) )
Alan Mishchenko committed
1402 1403 1404 1405 1406 1407 1408 1409
        {
            printf( "Abc_NtkPrintGates(): Converting to SOPs has failed.\n" );
            return;
        }
    }

    // get hold of the SOP of the node
    CountConst = CountBuf = CountInv = CountAnd = CountOr = CountOther = CounterTotal = 0;
1410
    Abc_NtkForEachNodeNotBarBuf( pNtk, pObj, i )
Alan Mishchenko committed
1411 1412 1413
    {
        if ( i == 0 ) continue;
        if ( Abc_NtkHasMapping(pNtk) )
1414
            pSop = Mio_GateReadSop((Mio_Gate_t *)pObj->pData);
Alan Mishchenko committed
1415
        else
1416
            pSop = (char *)pObj->pData;
Alan Mishchenko committed
1417 1418 1419 1420 1421 1422 1423
        // collect the stats
        if ( Abc_SopIsConst0(pSop) || Abc_SopIsConst1(pSop) )
            CountConst++;
        else if ( Abc_SopIsBuf(pSop) )
            CountBuf++;
        else if ( Abc_SopIsInv(pSop) )
            CountInv++;
Alan Mishchenko committed
1424 1425
        else if ( (!Abc_SopIsComplement(pSop) && Abc_SopIsAndType(pSop)) ||
                  ( Abc_SopIsComplement(pSop) && Abc_SopIsOrType(pSop)) )
Alan Mishchenko committed
1426
            CountAnd++;
Alan Mishchenko committed
1427 1428
        else if ( ( Abc_SopIsComplement(pSop) && Abc_SopIsAndType(pSop)) ||
                  (!Abc_SopIsComplement(pSop) && Abc_SopIsOrType(pSop)) )
Alan Mishchenko committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
            CountOr++;
        else
            CountOther++;
        CounterTotal++;
    }
    printf( "Const        = %8d    %6.2f %%\n", CountConst  ,  100.0 * CountConst   / CounterTotal );
    printf( "Buffer       = %8d    %6.2f %%\n", CountBuf    ,  100.0 * CountBuf     / CounterTotal );
    printf( "Inverter     = %8d    %6.2f %%\n", CountInv    ,  100.0 * CountInv     / CounterTotal );
    printf( "And          = %8d    %6.2f %%\n", CountAnd    ,  100.0 * CountAnd     / CounterTotal );
    printf( "Or           = %8d    %6.2f %%\n", CountOr     ,  100.0 * CountOr      / CounterTotal );
    printf( "Other        = %8d    %6.2f %%\n", CountOther  ,  100.0 * CountOther   / CounterTotal );
    printf( "TOTAL        = %8d    %6.2f %%\n", CounterTotal,  100.0 * CounterTotal / CounterTotal );

    // convert the network back into BDDs if this is how it was
    if ( fHasBdds )
        Abc_NtkSopToBdd(pNtk);
}

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

  Synopsis    [Prints statistics about gates used in the network.]

  Description []
1452

Alan Mishchenko committed
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPrintSharing( Abc_Ntk_t * pNtk )
{
    Vec_Ptr_t * vNodes1, * vNodes2;
    Abc_Obj_t * pObj1, * pObj2, * pNode1, * pNode2;
    int i, k, m, n, Counter;

    // print the template
    printf( "Statistics about sharing of logic nodes among the CO pairs.\n" );
    printf( "(CO1,CO2)=NumShared : " );
    // go though the CO pairs
    Abc_NtkForEachCo( pNtk, pObj1, i )
    {
        vNodes1 = Abc_NtkDfsNodes( pNtk, &pObj1, 1 );
        // mark the nodes
1472
        Vec_PtrForEachEntry( Abc_Obj_t *, vNodes1, pNode1, m )
Alan Mishchenko committed
1473 1474 1475 1476 1477 1478 1479 1480 1481
            pNode1->fMarkA = 1;
        // go through the second COs
        Abc_NtkForEachCo( pNtk, pObj2, k )
        {
            if ( i >= k )
                continue;
            vNodes2 = Abc_NtkDfsNodes( pNtk, &pObj2, 1 );
            // count the number of marked
            Counter = 0;
1482
            Vec_PtrForEachEntry( Abc_Obj_t *, vNodes2, pNode2, n )
Alan Mishchenko committed
1483 1484 1485 1486 1487 1488
                Counter += pNode2->fMarkA;
            // print
            printf( "(%d,%d)=%d ", i, k, Counter );
            Vec_PtrFree( vNodes2 );
        }
        // unmark the nodes
1489
        Vec_PtrForEachEntry( Abc_Obj_t *, vNodes1, pNode1, m )
Alan Mishchenko committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
            pNode1->fMarkA = 0;
        Vec_PtrFree( vNodes1 );
    }
    printf( "\n" );
}

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

  Synopsis    [Prints info for each output cone.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1507 1508 1509 1510 1511 1512 1513 1514
int Abc_NtkCountPis( Vec_Ptr_t * vSupp )
{
    Abc_Obj_t * pObj;
    int i, Counter = 0;
    Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pObj, i )
        Counter += Abc_ObjIsPi(pObj);
    return Counter;
}
1515
void Abc_NtkPrintStrSupports( Abc_Ntk_t * pNtk, int fMatrix )
Alan Mishchenko committed
1516 1517 1518
{
    Vec_Ptr_t * vSupp, * vNodes;
    Abc_Obj_t * pObj;
1519
    int i, k, nPis;
Alan Mishchenko committed
1520 1521 1522 1523 1524
    printf( "Structural support info:\n" );
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        vSupp  = Abc_NtkNodeSupport( pNtk, &pObj, 1 );
        vNodes = Abc_NtkDfsNodes( pNtk, &pObj, 1 );
1525 1526 1527
        nPis   = Abc_NtkCountPis( vSupp );
        printf( "%5d  %20s :  Cone = %5d.  Supp = %5d. (PIs = %5d. FFs = %5d.)\n",
            i, Abc_ObjName(pObj), vNodes->nSize, vSupp->nSize, nPis, vSupp->nSize - nPis );
Alan Mishchenko committed
1528 1529 1530
        Vec_PtrFree( vNodes );
        Vec_PtrFree( vSupp );
    }
1531
    if ( !fMatrix )
1532 1533
    {
        Abc_NtkCleanMarkA( pNtk );
1534
        return;
1535
    }
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554

    Abc_NtkForEachCi( pNtk, pObj, k )
        pObj->fMarkA = 0;

    printf( "Actual support info:\n" );
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        vSupp  = Abc_NtkNodeSupport( pNtk, &pObj, 1 );
        Vec_PtrForEachEntry( Abc_Obj_t *, vSupp, pObj, k )
            pObj->fMarkA = 1;
        Vec_PtrFree( vSupp );

        Abc_NtkForEachCi( pNtk, pObj, k )
            printf( "%d", pObj->fMarkA );
        printf( "\n" );

        Abc_NtkForEachCi( pNtk, pObj, k )
            pObj->fMarkA = 0;
    }
1555
    Abc_NtkCleanMarkA( pNtk );
Alan Mishchenko committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
}

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

  Synopsis    [Prints information about the object.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ObjPrint( FILE * pFile, Abc_Obj_t * pObj )
{
    Abc_Obj_t * pFanin;
    int i;
    fprintf( pFile, "Object %5d : ", pObj->Id );
    switch ( pObj->Type )
    {
1576 1577
        case ABC_OBJ_NONE:
            fprintf( pFile, "NONE   " );
Alan Mishchenko committed
1578
            break;
1579 1580
        case ABC_OBJ_CONST1:
            fprintf( pFile, "Const1 " );
Alan Mishchenko committed
1581
            break;
1582 1583
        case ABC_OBJ_PI:
            fprintf( pFile, "PI     " );
Alan Mishchenko committed
1584
            break;
1585 1586
        case ABC_OBJ_PO:
            fprintf( pFile, "PO     " );
Alan Mishchenko committed
1587
            break;
1588 1589
        case ABC_OBJ_BI:
            fprintf( pFile, "BI     " );
Alan Mishchenko committed
1590
            break;
1591 1592
        case ABC_OBJ_BO:
            fprintf( pFile, "BO     " );
Alan Mishchenko committed
1593
            break;
1594 1595
        case ABC_OBJ_NET:
            fprintf( pFile, "Net    " );
Alan Mishchenko committed
1596
            break;
1597 1598
        case ABC_OBJ_NODE:
            fprintf( pFile, "Node   " );
Alan Mishchenko committed
1599
            break;
1600 1601
        case ABC_OBJ_LATCH:
            fprintf( pFile, "Latch  " );
Alan Mishchenko committed
1602
            break;
1603 1604
        case ABC_OBJ_WHITEBOX:
            fprintf( pFile, "Whitebox" );
Alan Mishchenko committed
1605
            break;
1606 1607
        case ABC_OBJ_BLACKBOX:
            fprintf( pFile, "Blackbox" );
Alan Mishchenko committed
1608 1609
            break;
        default:
1610
            assert(0);
Alan Mishchenko committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
            break;
    }
    // print the fanins
    fprintf( pFile, " Fanins ( " );
    Abc_ObjForEachFanin( pObj, pFanin, i )
        fprintf( pFile, "%d ", pFanin->Id );
    fprintf( pFile, ") " );
/*
    fprintf( pFile, " Fanouts ( " );
    Abc_ObjForEachFanout( pObj, pFanin, i )
        fprintf( pFile, "%d(%c) ", pFanin->Id, Abc_NodeIsTravIdCurrent(pFanin)? '+' : '-' );
    fprintf( pFile, ") " );
*/
    // print the logic function
    if ( Abc_ObjIsNode(pObj) && Abc_NtkIsSopLogic(pObj->pNtk) )
Alan Mishchenko committed
1626
        fprintf( pFile, " %s", (char*)pObj->pData );
1627 1628
    else if ( Abc_ObjIsNode(pObj) && Abc_NtkIsMappedLogic(pObj->pNtk) )
        fprintf( pFile, " %s\n", Mio_GateReadName((Mio_Gate_t *)pObj->pData) );
Alan Mishchenko committed
1629 1630 1631 1632 1633
    else
        fprintf( pFile, "\n" );
}


Alan Mishchenko committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
/**Function*************************************************************

  Synopsis    [Checks the status of the miter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPrintMiter( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj, * pChild, * pConst1 = Abc_AigConst1(pNtk);
1648
    int i, iOut = -1;
1649
    abctime Time = Abc_Clock();
Alan Mishchenko committed
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
    int nUnsat = 0;
    int nSat   = 0;
    int nUndec = 0;
    int nPis   = 0;
    Abc_NtkForEachPi( pNtk, pObj, i )
        nPis += (int)( Abc_ObjFanoutNum(pObj) > 0 );
    Abc_NtkForEachPo( pNtk, pObj, i )
    {
        pChild = Abc_ObjChild0(pObj);
        // check if the output is constant 0
        if ( pChild == Abc_ObjNot(pConst1) )
            nUnsat++;
        // check if the output is constant 1
        else if ( pChild == pConst1 )
        {
            nSat++;
            if ( iOut == -1 )
                iOut = i;
        }
        // check if the output is a primary input
        else if ( Abc_ObjIsPi(Abc_ObjRegular(pChild)) )
        {
            nSat++;
            if ( iOut == -1 )
                iOut = i;
        }
    // check if the output is 1 for the 0000 pattern
        else if ( Abc_ObjRegular(pChild)->fPhase != (unsigned)Abc_ObjIsComplement(pChild) )
        {
            nSat++;
            if ( iOut == -1 )
                iOut = i;
        }
        else
            nUndec++;
    }
    printf( "Miter:  I =%6d", nPis );
    printf( "  N =%7d", Abc_NtkNodeNum(pNtk) );
    printf( "  ? =%7d", nUndec );
    printf( "  U =%6d", nUnsat );
    printf( "  S =%6d", nSat );
1691
    Time = Abc_Clock() - Time;
Alan Mishchenko committed
1692 1693
    printf(" %7.2f sec\n", (float)(Time)/(float)(CLOCKS_PER_SEC));
    if ( iOut >= 0 )
1694
        printf( "The first satisfiable output is number %d (%s).\n", iOut, Abc_ObjName( Abc_NtkPo(pNtk, iOut) ) );
Alan Mishchenko committed
1695 1696
}

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
/**Function*************************************************************

  Synopsis    [Checks the status of the miter.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkPrintPoEquivs( Abc_Ntk_t * pNtk )
{
    Abc_Obj_t * pObj, * pDriver, * pRepr;  int i, iRepr;
    Vec_Int_t * vMap = Vec_IntStartFull( Abc_NtkObjNumMax(pNtk) );
    Abc_NtkForEachPo( pNtk, pObj, i )
    {
        pDriver = Abc_ObjFanin0(pObj);
        if ( Abc_NtkIsStrash(pNtk) && pDriver == Abc_AigConst1(pNtk) )
        {
            printf( "%s = Const%d\n", Abc_ObjName(pObj), !Abc_ObjFaninC0(pObj) );
            continue;
        }
        else if ( !Abc_NtkIsStrash(pNtk) && Abc_NodeIsConst(pDriver) )
        {
            printf( "%s = Const%d\n", Abc_ObjName(pObj), Abc_NodeIsConst1(pDriver) );
            continue;
        }
        iRepr = Vec_IntEntry( vMap, Abc_ObjId(pDriver) );
        if ( iRepr == -1 )
        {
            Vec_IntWriteEntry( vMap, Abc_ObjId(pDriver), i );
            continue;
        }
        pRepr = Abc_NtkCo(pNtk, iRepr);
        printf( "%s = %s%s\n", Abc_ObjName(pObj), Abc_ObjFaninC0(pRepr) == Abc_ObjFaninC0(pObj) ? "" : "!", Abc_ObjName(pRepr) );
    }
    Vec_IntFree( vMap );
}

Alan Mishchenko committed
1737 1738 1739 1740 1741 1742 1743 1744 1745



typedef struct Gli_Man_t_ Gli_Man_t;

extern Gli_Man_t * Gli_ManAlloc( int nObjs, int nRegs, int nFanioPairs );
extern void        Gli_ManStop( Gli_Man_t * p );
extern int         Gli_ManCreateCi( Gli_Man_t * p, int nFanouts );
extern int         Gli_ManCreateCo( Gli_Man_t * p, int iFanin );
1746
extern int         Gli_ManCreateNode( Gli_Man_t * p, Vec_Int_t * vFanins, int nFanouts, word * pGateTruth );
Alan Mishchenko committed
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756

extern void        Gli_ManSwitchesAndGlitches( Gli_Man_t * p, int nPatterns, float PiTransProb, int fVerbose );
extern int         Gli_ObjNumSwitches( Gli_Man_t * p, int iNode );
extern int         Gli_ObjNumGlitches( Gli_Man_t * p, int iNode );

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

  Synopsis    [Returns the percentable of increased power due to glitching.]

  Description []
1757

Alan Mishchenko committed
1758 1759 1760 1761 1762
  SideEffects []

  SeeAlso     []

***********************************************************************/
1763
float Abc_NtkMfsTotalGlitchingLut( Abc_Ntk_t * pNtk, int nPats, int Prob, int fVerbose )
Alan Mishchenko committed
1764 1765 1766 1767 1768 1769
{
    int nSwitches, nGlitches;
    Gli_Man_t * p;
    Vec_Ptr_t * vNodes;
    Vec_Int_t * vFanins, * vTruth;
    Abc_Obj_t * pObj, * pFanin;
1770
    Vec_Wrd_t * vTruths; word * pTruth;
Alan Mishchenko committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
    unsigned * puTruth;
    int i, k;
    assert( Abc_NtkIsLogic(pNtk) );
    assert( Abc_NtkGetFaninMax(pNtk) <= 6 );
    if ( Abc_NtkGetFaninMax(pNtk) > 6 )
    {
        printf( "Abc_NtkMfsTotalGlitching() This procedure works only for mapped networks with LUTs size up to 6 inputs.\n" );
        return -1.0;
    }
    Abc_NtkToAig( pNtk );
    vNodes = Abc_NtkDfs( pNtk, 0 );
    vFanins = Vec_IntAlloc( 6 );
    vTruth = Vec_IntAlloc( 1 << 12 );
1784
    vTruths = Vec_WrdStart( Abc_NtkObjNumMax(pNtk) );
Alan Mishchenko committed
1785 1786

    // derive network for glitch computation
1787
    p = Gli_ManAlloc( Vec_PtrSize(vNodes) + Abc_NtkCiNum(pNtk) + Abc_NtkCoNum(pNtk),
Alan Mishchenko committed
1788 1789 1790 1791 1792
        Abc_NtkLatchNum(pNtk), Abc_NtkGetTotalFanins(pNtk) + Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachObj( pNtk, pObj, i )
        pObj->iTemp = -1;
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->iTemp = Gli_ManCreateCi( p, Abc_ObjFanoutNum(pObj) );
1793
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
Alan Mishchenko committed
1794 1795 1796 1797
    {
        Vec_IntClear( vFanins );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vFanins, pFanin->iTemp );
1798
        puTruth = Hop_ManConvertAigToTruth( (Hop_Man_t *)pNtk->pManFunc, (Hop_Obj_t *)pObj->pData, Abc_ObjFaninNum(pObj), vTruth, 0 );
1799 1800 1801
        pTruth = Vec_WrdEntryP( vTruths, Abc_ObjId(pObj) );
        *pTruth = ((word)puTruth[Abc_ObjFaninNum(pObj) == 6] << 32) | (word)puTruth[0];
        pObj->iTemp = Gli_ManCreateNode( p, vFanins, Abc_ObjFanoutNum(pObj), pTruth );
Alan Mishchenko committed
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
    }
    Abc_NtkForEachCo( pNtk, pObj, i )
        Gli_ManCreateCo( p, Abc_ObjFanin0(pObj)->iTemp );

    // compute glitching
    Gli_ManSwitchesAndGlitches( p, 4000, 1.0/8.0, 0 );

    // compute the ratio
    nSwitches = nGlitches = 0;
    Abc_NtkForEachObj( pNtk, pObj, i )
        if ( pObj->iTemp >= 0 )
        {
            nSwitches += Abc_ObjFanoutNum(pObj) * Gli_ObjNumSwitches(p, pObj->iTemp);
            nGlitches += Abc_ObjFanoutNum(pObj) * Gli_ObjNumGlitches(p, pObj->iTemp);
        }

    Gli_ManStop( p );
    Vec_PtrFree( vNodes );
    Vec_IntFree( vTruth );
    Vec_IntFree( vFanins );
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
    Vec_WrdFree( vTruths );
    return nSwitches ? 100.0*(nGlitches-nSwitches)/nSwitches : 0.0;
}

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

  Synopsis    [Returns the percentable of increased power due to glitching.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
float Abc_NtkMfsTotalGlitching( Abc_Ntk_t * pNtk, int nPats, int Prob, int fVerbose )
{
    int nSwitches, nGlitches;
    Gli_Man_t * p;
    Vec_Ptr_t * vNodes;
    Vec_Int_t * vFanins;
    Abc_Obj_t * pObj, * pFanin;
    int i, k, nFaninMax = Abc_NtkGetFaninMax(pNtk);
    if ( !Abc_NtkIsMappedLogic(pNtk) )
        return Abc_NtkMfsTotalGlitchingLut( pNtk, nPats, Prob, fVerbose );
    assert( Abc_NtkIsMappedLogic(pNtk) );
    if ( nFaninMax > 16 )
    {
        printf( "Abc_NtkMfsTotalGlitching() This procedure works only for mapped networks with LUTs size up to 6 inputs.\n" );
        return -1.0;
    }
    vNodes = Abc_NtkDfs( pNtk, 0 );
    vFanins = Vec_IntAlloc( 6 );

    // derive network for glitch computation
    p = Gli_ManAlloc( Vec_PtrSize(vNodes) + Abc_NtkCiNum(pNtk) + Abc_NtkCoNum(pNtk),
        Abc_NtkLatchNum(pNtk), Abc_NtkGetTotalFanins(pNtk) + Abc_NtkCoNum(pNtk) );
    Abc_NtkForEachObj( pNtk, pObj, i )
        pObj->iTemp = -1;
    Abc_NtkForEachCi( pNtk, pObj, i )
        pObj->iTemp = Gli_ManCreateCi( p, Abc_ObjFanoutNum(pObj) );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
        Vec_IntClear( vFanins );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_IntPush( vFanins, pFanin->iTemp );
        pObj->iTemp = Gli_ManCreateNode( p, vFanins, Abc_ObjFanoutNum(pObj), Mio_GateReadTruthP((Mio_Gate_t *)pObj->pData) );
    }
    Abc_NtkForEachCo( pNtk, pObj, i )
        Gli_ManCreateCo( p, Abc_ObjFanin0(pObj)->iTemp );

    // compute glitching
    Gli_ManSwitchesAndGlitches( p, nPats, 1.0/Prob, fVerbose );

    // compute the ratio
    nSwitches = nGlitches = 0;
    Abc_NtkForEachObj( pNtk, pObj, i )
        if ( pObj->iTemp >= 0 )
        {
            nSwitches += Abc_ObjFanoutNum(pObj) * Gli_ObjNumSwitches(p, pObj->iTemp);
            nGlitches += Abc_ObjFanoutNum(pObj) * Gli_ObjNumGlitches(p, pObj->iTemp);
        }

    Gli_ManStop( p );
    Vec_PtrFree( vNodes );
    Vec_IntFree( vFanins );
Alan Mishchenko committed
1888 1889 1890
    return nSwitches ? 100.0*(nGlitches-nSwitches)/nSwitches : 0.0;
}

1891 1892 1893 1894 1895
/**Function*************************************************************

  Synopsis    [Prints K-map of 6-var function represented by truth table.]

  Description []
1896

1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_Show6VarFunc( word F0, word F1 )
{
    // order of cells in the Karnaugh map
//    int Cells[8] = { 0, 1, 3, 2, 6, 7, 5, 4 };
    int Cells[8] = { 0, 4, 6, 2, 3, 7, 5, 1 };
    // intermediate variables
    int s; // symbol counter
    int h; // horizontal coordinate;
    int v; // vertical coordinate;
    assert( (F0 & F1) == 0 );

    // output minterms above
    for ( s = 0; s < 4; s++ )
        printf( " " );
    printf( " " );
    for ( h = 0; h < 8; h++ )
    {
        for ( s = 0; s < 3; s++ )
            printf( "%d",  ((Cells[h] >> (2-s)) & 1) );
        printf( " " );
    }
    printf( "\n" );

    // output horizontal line above
    for ( s = 0; s < 4; s++ )
        printf( " " );
    printf( "+" );
    for ( h = 0; h < 8; h++ )
    {
        for ( s = 0; s < 3; s++ )
            printf( "-" );
        printf( "+" );
    }
    printf( "\n" );

    // output lines with function values
    for ( v = 0; v < 8; v++ )
    {
        for ( s = 0; s < 3; s++ )
            printf( "%d",  ((Cells[v] >> (2-s)) & 1) );
        printf( " |" );

        for ( h = 0; h < 8; h++ )
        {
            printf( " " );
            if ( ((F0 >> ((Cells[v]*8)+Cells[h])) & 1) )
                printf( "0" );
            else if ( ((F1 >> ((Cells[v]*8)+Cells[h])) & 1) )
                printf( "1" );
            else
                printf( " " );
            printf( " |" );
        }
        printf( "\n" );

        // output horizontal line above
        for ( s = 0; s < 4; s++ )
            printf( " " );
//        printf( "%c", v == 7 ? '+' : '|' );
        printf( "+" );
        for ( h = 0; h < 8; h++ )
        {
            for ( s = 0; s < 3; s++ )
                printf( "-" );
//            printf( "%c", v == 7 ? '+' : '|' );
            printf( "%c", (v == 7 || h == 7) ? '+' : '|' );
        }
        printf( "\n" );
    }
}

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

  Synopsis    [Prints K-map of 6-var function represented by truth table.]

  Description []
1978

1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkShow6VarFunc( char * pF0, char * pF1 )
{
    word F0, F1;
    if ( strlen(pF0) != 16 )
    {
1989
        printf( "Wrong length (%d) of 6-var truth table.\n", (int)strlen(pF0) );
1990 1991 1992 1993
        return;
    }
    if ( strlen(pF1) != 16 )
    {
1994
        printf( "Wrong length (%d) of 6-var truth table.\n", (int)strlen(pF1) );
1995 1996 1997 1998 1999 2000 2001 2002
        return;
    }
    Extra_ReadHexadecimal( (unsigned *)&F0, pF0, 6 );
    Extra_ReadHexadecimal( (unsigned *)&F1, pF1, 6 );
    Abc_Show6VarFunc( F0, F1 );
}


Alan Mishchenko committed
2003 2004 2005 2006 2007
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


2008
ABC_NAMESPACE_IMPL_END