superGate.c 56.6 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**CFile****************************************************************

  FileName    [superGate.c]

  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]

  Synopsis    [Pre-computation of supergates.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - September 8, 2003.]

  Revision    [$Id: superGate.c,v 1.7 2004/08/03 00:11:40 satrajit Exp $]

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

19
#include <math.h>
Alan Mishchenko committed
20 21
#include "superInt.h"

22 23 24
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

// the bit masks
#define SUPER_MASK(n)     ((~((unsigned)0)) >> (32-(n)))
#define SUPER_FULL         (~((unsigned)0))
#define SUPER_NO_VAR         (-9999.0)
#define SUPER_EPSILON        (0.001)

// data structure for supergate precomputation
typedef struct Super_ManStruct_t_     Super_Man_t;   // manager
typedef struct Super_GateStruct_t_    Super_Gate_t;  // supergate

struct Super_ManStruct_t_
{
    // parameters
    char *              pName;        // the original genlib file name
    int                 nVarsMax;     // the number of inputs
    int                 nMints;       // the number of minterms
    int                 nLevels;      // the number of logic levels
46
    int                 nGatesMax;    // the number of gates computed
Alan Mishchenko committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    float               tDelayMax;    // the max delay of the supergates in the library
    float               tAreaMax;     // the max area of the supergates in the library
    int                 fSkipInv;     // the flag says about skipping inverters
    int                 fWriteOldFormat; // in addition, writes the file in the old format
    int                 fVerbose;

    // supergates
    Super_Gate_t *      pInputs[10];  // the input supergates
    int                 nGates;       // the number of gates in the library
    Super_Gate_t **     pGates;       // the gates themselves
    stmm_table *        tTable;       // mapping of truth tables into gates

    // memory managers
    Extra_MmFixed_t *   pMem;         // memory manager for the supergates
    Extra_MmFlex_t *    pMemFlex;     // memory manager for the fanin arrays

    // statistics
    int                 nTried;       // the total number of tried
    int                 nAdded;       // the number of entries added
    int                 nRemoved;     // the number of entries removed
67
    int                 nClasses;     // the number of gate classes
Alan Mishchenko committed
68 69 70 71 72
    int                 nUnique;      // the number of unique gates
    int                 nLookups;     // the number of hash table lookups
    int                 nAliases;     // the number of hash table lookups thrown away due to aliasing

    // runtime
73
    abctime             Time;         // the runtime of the generation procedure
Alan Mishchenko committed
74 75
    int                 TimeLimit;    // the runtime limit (in seconds)
    int                 TimeSec;      // the time passed (in seconds)
76 77
    abctime             TimeStop;     // the time to stop computation (in miliseconds)
    abctime             TimePrint;    // the time to print message
Alan Mishchenko committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
};

struct Super_GateStruct_t_
{
    Mio_Gate_t *        pRoot;        // the root gate for this supergate
    unsigned            fVar :     1; // the flag signaling the elementary variable
    unsigned            fSuper :   1; // the flag signaling the elementary variable
    unsigned            nFanins :  6; // the number of fanin gates
    unsigned            Number :  24; // the number assigned in the process
    unsigned            uTruth[2];    // the truth table of this supergate
    Super_Gate_t *      pFanins[6];   // the fanins of the gate
    float               Area;         // the area of this gate
    float               ptDelays[6];  // the pin-to-pin delays for all inputs
    float               tDelayMax;    // the maximum delay
    Super_Gate_t *      pNext;        // the next gate in the table
};


// iterating through the gates in the library
#define Super_ManForEachGate( GateArray, Limit, Index, Gate )    \
    for ( Index = 0;                                             \
          Index < Limit && (Gate = GateArray[Index]);            \
          Index++ )

// static functions
static Super_Man_t *  Super_ManStart();
static void           Super_ManStop( Super_Man_t * pMan );

static void           Super_AddGateToTable( Super_Man_t * pMan, Super_Gate_t * pGate );
static void           Super_First( Super_Man_t * pMan, int nVarsMax );
108
static Super_Man_t *  Super_Compute( Super_Man_t * pMan, Mio_Gate_t ** ppGates, int nGates, int nGatesMax, int fSkipInv );
Alan Mishchenko committed
109
static Super_Gate_t * Super_CreateGateNew( Super_Man_t * pMan, Mio_Gate_t * pRoot, Super_Gate_t ** pSupers, int nSupers, unsigned uTruth[], float Area, float tPinDelaysRes[], float tDelayMax, int nPins );
110
static int            Super_CompareGates( Super_Man_t * pMan, unsigned uTruth[], float Area, float tPinDelaysRes[], int nPins );
Alan Mishchenko committed
111 112 113 114 115
static int            Super_DelayCompare( Super_Gate_t ** ppG1, Super_Gate_t ** ppG2 );
static int            Super_AreaCompare( Super_Gate_t ** ppG1, Super_Gate_t ** ppG2 );
static void           Super_TranferGatesToArray( Super_Man_t * pMan );
static int            Super_CheckTimeout( ProgressBar * pPro, Super_Man_t * pMan );
 
116
static Vec_Str_t *    Super_Write( Super_Man_t * pMan );
Alan Mishchenko committed
117 118 119 120 121
static int            Super_WriteCompare( Super_Gate_t ** ppG1, Super_Gate_t ** ppG2 );
static void           Super_WriteFileHeader( Super_Man_t * pMan, FILE * pFile );

static void           Super_WriteLibrary( Super_Man_t * pMan );

122 123
static void           Super_WriteLibraryTreeFile( Super_Man_t * pMan );
static Vec_Str_t *    Super_WriteLibraryTreeStr( Super_Man_t * pMan );
Alan Mishchenko committed
124 125

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
126
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
127 128 129 130 131 132 133 134 135 136 137 138 139
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Precomputes the library of supergates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
140
void Super_Precompute( Mio_Library_t * pLibGen, int nVarsMax, int nLevels, int nGatesMax, float tDelayMax, float tAreaMax, int TimeLimit, int fSkipInv, int fVerbose, char * pFileName )
Alan Mishchenko committed
141
{
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    Vec_Str_t * vStr;
    FILE * pFile = fopen( pFileName, "wb" );
    if ( pFile == NULL )
    {     
        printf( "Cannot open output file \"%s\".\n", pFileName );
        return;
    }
    vStr = Super_PrecomputeStr( pLibGen, nVarsMax, nLevels, nGatesMax, tDelayMax, tAreaMax, TimeLimit, fSkipInv, fVerbose );
    fwrite( Vec_StrArray(vStr), 1, Vec_StrSize(vStr), pFile );
    fclose( pFile );
    Vec_StrFree( vStr );
    // report the result of writing
    if ( fVerbose )
    {
        printf( "The supergates are written using new format \"%s\" ", pFileName );
        printf( "(%0.3f MB).\n", ((double)Extra_FileSize(pFileName))/(1<<20) );
    }
}

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

  Synopsis    [Precomputes the library of supergates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Str_t * Super_PrecomputeStr( Mio_Library_t * pLibGen, int nVarsMax, int nLevels, int nGatesMax, float tDelayMax, float tAreaMax, int TimeLimit, int fSkipInv, int fVerbose )
{
    Vec_Str_t * vStr;
Alan Mishchenko committed
175 176
    Super_Man_t * pMan;
    Mio_Gate_t ** ppGates;
177
    int nGates, Level;
178
    abctime clk, clockStart;
Alan Mishchenko committed
179 180

    assert( nVarsMax < 7 );
181
    if ( nGatesMax && nGatesMax < nVarsMax )
182 183 184
    {
        fprintf( stderr, "Erro! The number of supergates requested (%d) in less than the number of variables (%d).\n", nGatesMax, nVarsMax );
        fprintf( stderr, "The library cannot be computed.\n" );
185
        return NULL;
186
    }
Alan Mishchenko committed
187 188

    // get the root gates
189
    ppGates = Mio_CollectRoots( pLibGen, nVarsMax, tDelayMax, 0, &nGates, fVerbose );
190
    if ( nGatesMax && nGates >= nGatesMax )
191 192 193 194
    {
        fprintf( stdout, "Warning! Genlib library contains more gates than can be computed.\n");
        fprintf( stdout, "Only one-gate supergates are included in the supergate library.\n" );
    }
Alan Mishchenko committed
195 196 197 198

    // start the manager
    pMan = Super_ManStart();
    pMan->pName     = Mio_LibraryReadName(pLibGen);
199
    pMan->nGatesMax = nGatesMax;
Alan Mishchenko committed
200 201 202 203
    pMan->fSkipInv  = fSkipInv;
    pMan->tDelayMax = tDelayMax;
    pMan->tAreaMax  = tAreaMax;
    pMan->TimeLimit = TimeLimit; // in seconds
204
    pMan->TimeStop  = TimeLimit ? TimeLimit * CLOCKS_PER_SEC + Abc_Clock() : 0; // in CPU ticks
205
    pMan->fVerbose  = fVerbose;
Alan Mishchenko committed
206 207 208 209 210 211 212 213 214

    if ( nGates == 0 )
    {
        fprintf( stderr, "Error: No genlib gates satisfy the limits criteria. Stop.\n");
        fprintf( stderr, "Limits: max delay =  %.2f, max area =  %.2f, time limit = %d sec.\n", 
            pMan->tDelayMax, pMan->tAreaMax, pMan->TimeLimit );

        // stop the manager
        Super_ManStop( pMan );
Alan Mishchenko committed
215
        ABC_FREE( ppGates );
Alan Mishchenko committed
216

217
        return NULL;
Alan Mishchenko committed
218 219 220 221 222 223
    }

    // get the starting supergates
    Super_First( pMan, nVarsMax );

    // perform the computation of supergates
224
    clockStart = Abc_Clock();
Alan Mishchenko committed
225 226
if ( fVerbose )
{
227 228
    printf( "Computing supergates with %d inputs, %d levels, and %d max gates.\n", 
        pMan->nVarsMax, nLevels, nGatesMax );
Alan Mishchenko committed
229 230 231 232 233 234
    printf( "Limits: max delay =  %.2f, max area =  %.2f, time limit = %d sec.\n", 
        pMan->tDelayMax, pMan->tAreaMax, pMan->TimeLimit );
}

    for ( Level = 1; Level <= nLevels; Level++ )
    {
235
        if ( pMan->TimeStop && Abc_Clock() > pMan->TimeStop )
Alan Mishchenko committed
236
            break;
237
clk = Abc_Clock();
238
        Super_Compute( pMan, ppGates, nGates, nGatesMax, fSkipInv );
Alan Mishchenko committed
239 240 241 242 243
        pMan->nLevels = Level;
if ( fVerbose )
{
        printf( "Lev %d: Try =%12d. Add =%6d. Rem =%5d. Save =%6d. Lookups =%12d. Aliases =%12d. ",
           Level, pMan->nTried, pMan->nAdded, pMan->nRemoved, pMan->nAdded - pMan->nRemoved, pMan->nLookups, pMan->nAliases );
244
ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
245 246 247
fflush( stdout );
}
    }
248
    pMan->Time = Abc_Clock() - clockStart;
Alan Mishchenko committed
249 250 251 252 253 254 255

if ( fVerbose )
{
printf( "Writing the output file...\n" );
fflush( stdout );
}
    // write them into a file
256
    vStr = Super_Write( pMan );
Alan Mishchenko committed
257 258 259

    // stop the manager
    Super_ManStop( pMan );
Alan Mishchenko committed
260
    ABC_FREE( ppGates );
261
    return vStr;
Alan Mishchenko committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
}


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

  Synopsis    [Derives the starting supergates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Super_First( Super_Man_t * pMan, int nVarsMax )
{
    Super_Gate_t * pSuper;
    int nMintLimit, nVarLimit;
    int v, m;
    // set the parameters
    pMan->nVarsMax  = nVarsMax;
    pMan->nMints    = (1 << nVarsMax);
    pMan->nLevels   = 0;
    // allocate room for the gates
    pMan->nGates    = nVarsMax;  
Alan Mishchenko committed
287
    pMan->pGates    = ABC_ALLOC( Super_Gate_t *, nVarsMax + 2 ); 
Alan Mishchenko committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    // create the gates corresponding to the elementary variables
    for ( v = 0; v < nVarsMax; v++ )
    {
        // get a new gate
        pSuper = (Super_Gate_t *)Extra_MmFixedEntryFetch( pMan->pMem );
        memset( pSuper, 0, sizeof(Super_Gate_t) );
        // assign the elementary variable, the truth table, and the delays
        pSuper->fVar = 1;
        pSuper->Number = v;
        for ( m = 0; m < nVarsMax; m++ )
            pSuper->ptDelays[m] = SUPER_NO_VAR;
        pSuper->ptDelays[v] = 0.0;
        // set the gate
        pMan->pGates[v] = pSuper;
        Super_AddGateToTable( pMan, pSuper );
        pMan->pInputs[v] = pSuper;
    }
    // set up their truth tables
    nVarLimit  = (nVarsMax >= 5)? 5 : nVarsMax;
    nMintLimit = (1 << nVarLimit);
    for ( m = 0; m < nMintLimit; m++ )
        for ( v = 0; v < nVarLimit; v++ )
            if ( m & (1 << v) )
                pMan->pGates[v]->uTruth[0] |= (1 << m);
    // make adjustments for the case of 6 variables
    if ( nVarsMax == 6 )
    {
        for ( v = 0; v < 5; v++ )
            pMan->pGates[v]->uTruth[1] = pMan->pGates[v]->uTruth[0];
        pMan->pGates[5]->uTruth[0] = 0;
        pMan->pGates[5]->uTruth[1] = ~((unsigned)0);
    }
    else
    {
        for ( v = 0; v < nVarsMax; v++ )
            pMan->pGates[v]->uTruth[1] = 0;
    }
}

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

  Synopsis    [Precomputes one level of supergates.]

  Description [This procedure computes the set of supergates that can be
332
  derived from the given set of root gates (from genlib library) by composing
Alan Mishchenko committed
333 334 335 336 337 338 339 340 341 342
  the root gates with the currently available supergates. This procedure is
  smart in the sense that it tries to avoid useless emuration by imposing
  tight bounds by area and delay. Only the supergates and are guaranteed to 
  have smaller area and delay are enumereated. See comments below for details.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
343
Super_Man_t * Super_Compute( Super_Man_t * pMan, Mio_Gate_t ** ppGates, int nGates, int nGatesMax, int fSkipInv )
Alan Mishchenko committed
344 345 346
{
    Super_Gate_t * pSupers[6], * pGate0, * pGate1, * pGate2, * pGate3, * pGate4, * pGate5, * pGateNew;
    float tPinDelaysRes[6], * ptPinDelays[6], tPinDelayMax, tDelayMio;
Alan Mishchenko committed
347 348
    float Area = 0.0; // Suppress "might be used uninitialized"
    float Area0, Area1, Area2, Area3, Area4, AreaMio;
Alan Mishchenko committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    unsigned uTruth[2], uTruths[6][2];
    int i0, i1, i2, i3, i4, i5; 
    Super_Gate_t ** ppGatesLimit;
    int nFanins, nGatesLimit, k, s, t;
    ProgressBar * pProgress;
    int fTimeOut;
    int fPrune = 1;                     // Shall we prune?
    int iPruneLimit = 3;                // Each of the gates plugged into the root gate will have 
                                        // less than these many fanins
    int iPruneLimitRoot = 4;            // The root gate may have only less than these many fanins

    // put the gates from the unique table into the array
    // the gates from the array will be used to compose other gates
    // the gates in tbe table are used to check uniqueness of collected gates
    Super_TranferGatesToArray( pMan );

    // sort the gates in the increasing order of maximum delay
    if ( pMan->nGates > 10000 )
    {
        printf( "Sorting array of %d supergates...\r", pMan->nGates );
        fflush( stdout );
    }
    qsort( (void *)pMan->pGates, pMan->nGates, sizeof(Super_Gate_t *), 
            (int (*)(const void *, const void *)) Super_DelayCompare );
    assert( Super_DelayCompare( pMan->pGates, pMan->pGates + pMan->nGates - 1 ) <= 0 );
    if ( pMan->nGates > 10000 )
    {
        printf( "                                       \r" );
    }

    pProgress = Extra_ProgressBarStart( stdout, pMan->TimeLimit );
380
    pMan->TimePrint = Abc_Clock() + CLOCKS_PER_SEC;
Alan Mishchenko committed
381
    ppGatesLimit = ABC_ALLOC( Super_Gate_t *, pMan->nGates );
Alan Mishchenko committed
382 383 384 385 386 387 388 389 390 391 392
    // go through the root gates
    // the root gates are sorted in the increasing gelay
    fTimeOut = 0;
    for ( k = 0; k < nGates; k++ )
    {
        if ( fTimeOut ) break;

        if ( fPrune )
        {
            if ( pMan->nLevels >= 1 )  // First level gates have been computed
            {
393
                if ( Mio_GateReadPinNum(ppGates[k]) >= iPruneLimitRoot )
Alan Mishchenko committed
394 395 396
                    continue;
            }
        }
Alan Mishchenko committed
397 398 399 400 401 402
/*
        if ( strcmp(Mio_GateReadName(ppGates[k]), "MUX2IX0") == 0 )
        {
            int s = 0;
        }
*/
Alan Mishchenko committed
403 404 405 406 407 408 409 410 411
        // select the subset of gates to be considered with this root gate
        // all the gates past this point will lead to delay larger than the limit
        tDelayMio = (float)Mio_GateReadDelayMax(ppGates[k]);
        for ( s = 0, t = 0; s < pMan->nGates; s++ )
        {
            if ( fPrune && ( pMan->nLevels >= 1 ) && ( ((int)pMan->pGates[s]->nFanins) >= iPruneLimit ))
                continue;
            
            ppGatesLimit[t] = pMan->pGates[s];
412
            if ( ppGatesLimit[t++]->tDelayMax + tDelayMio > pMan->tDelayMax && pMan->tDelayMax > 0.0 )
Alan Mishchenko committed
413 414 415 416 417 418
                break;
        }
        nGatesLimit = t;

        if ( pMan->fVerbose )
        {
419
            printf ("Trying %d choices for %d inputs\r", t, Mio_GateReadPinNum(ppGates[k]) );
Alan Mishchenko committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        }

        // resort part of this range by area
        // now we can prune the search by going up in the list until we reach the limit on area
        // all the gates beyond this point can be skipped because their area can be only larger
        if ( nGatesLimit > 10000 )
            printf( "Sorting array of %d supergates...\r", nGatesLimit );
        qsort( (void *)ppGatesLimit, nGatesLimit, sizeof(Super_Gate_t *), 
                (int (*)(const void *, const void *)) Super_AreaCompare );
        assert( Super_AreaCompare( ppGatesLimit, ppGatesLimit + nGatesLimit - 1 ) <= 0 );
        if ( nGatesLimit > 10000 )
            printf( "                                       \r" );

        // consider the combinations of gates with the root gate on top
        AreaMio = (float)Mio_GateReadArea(ppGates[k]);
435
        nFanins = Mio_GateReadPinNum(ppGates[k]);
Alan Mishchenko committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
        switch ( nFanins )
        {
        case 0: // should not happen
            assert( 0 ); 
            break;
        case 1: // interter root
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              if ( fTimeOut ) break;
              fTimeOut = Super_CheckTimeout( pProgress, pMan );
              // skip the inverter as the root gate before the elementary variable
              // as a result, the supergates will not have inverters on the input side
              // but inverters still may occur at the output of or inside complex supergates
              if ( fSkipInv && pGate0->tDelayMax == 0 )
                  continue;
              // compute area
              Area = AreaMio + pGate0->Area;
453
              if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
454 455 456 457 458 459 460 461 462 463
                  break;

              pSupers[0] = pGate0;  uTruths[0][0] = pGate0->uTruth[0];  uTruths[0][1] = pGate0->uTruth[1];  ptPinDelays[0] = pGate0->ptDelays; 
              Mio_DeriveGateDelays( ppGates[k], ptPinDelays, nFanins, pMan->nVarsMax, SUPER_NO_VAR, tPinDelaysRes, &tPinDelayMax );
              Mio_DeriveTruthTable( ppGates[k], uTruths, nFanins, pMan->nVarsMax, uTruth );
              if ( !Super_CompareGates( pMan, uTruth, Area, tPinDelaysRes, pMan->nVarsMax ) )
                  continue;
              // create a new gate
              pGateNew = Super_CreateGateNew( pMan, ppGates[k], pSupers, nFanins, uTruth, Area, tPinDelaysRes, tPinDelayMax, pMan->nVarsMax );
              Super_AddGateToTable( pMan, pGateNew );
464
              if ( nGatesMax && pMan->nClasses > nGatesMax )
465 466
                  goto done;
            }
Alan Mishchenko committed
467 468 469 470 471
            break;
        case 2: // two-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
472
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
473 474 475 476 477 478 479 480 481
                  break;
              pSupers[0] = pGate0;  uTruths[0][0] = pGate0->uTruth[0];  uTruths[0][1] = pGate0->uTruth[1];  ptPinDelays[0] = pGate0->ptDelays; 
              Super_ManForEachGate( ppGatesLimit, nGatesLimit, i1, pGate1 )
              if ( i1 != i0 )
              {
                if ( fTimeOut ) goto done;
                fTimeOut = Super_CheckTimeout( pProgress, pMan );
                // compute area
                Area = Area0 + pGate1->Area;
482
                if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
483 484 485 486 487 488 489 490 491 492
                    break;

                pSupers[1] = pGate1;  uTruths[1][0] = pGate1->uTruth[0];  uTruths[1][1] = pGate1->uTruth[1];  ptPinDelays[1] = pGate1->ptDelays;
                Mio_DeriveGateDelays( ppGates[k], ptPinDelays, nFanins, pMan->nVarsMax, SUPER_NO_VAR, tPinDelaysRes, &tPinDelayMax );
                Mio_DeriveTruthTable( ppGates[k], uTruths, nFanins, pMan->nVarsMax, uTruth );
                if ( !Super_CompareGates( pMan, uTruth, Area, tPinDelaysRes, pMan->nVarsMax ) )
                    continue;
                // create a new gate
                pGateNew = Super_CreateGateNew( pMan, ppGates[k], pSupers, nFanins, uTruth, Area, tPinDelaysRes, tPinDelayMax, pMan->nVarsMax );
                Super_AddGateToTable( pMan, pGateNew );
493
                if ( nGatesMax && pMan->nClasses > nGatesMax )
494
                    goto done;
Alan Mishchenko committed
495 496 497 498 499 500 501
              }
            }
            break;
        case 3: // three-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
502
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
503 504 505 506 507 508 509
                  break;
              pSupers[0] = pGate0;  uTruths[0][0] = pGate0->uTruth[0];  uTruths[0][1] = pGate0->uTruth[1];  ptPinDelays[0] = pGate0->ptDelays; 

              Super_ManForEachGate( ppGatesLimit, nGatesLimit, i1, pGate1 )
              if ( i1 != i0 )
              {
                Area1 = Area0 + pGate1->Area;
510
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
511 512 513 514 515 516 517 518 519 520
                    break;
                pSupers[1] = pGate1;  uTruths[1][0] = pGate1->uTruth[0];  uTruths[1][1] = pGate1->uTruth[1];  ptPinDelays[1] = pGate1->ptDelays;

                Super_ManForEachGate( ppGatesLimit, nGatesLimit, i2, pGate2 )
                if ( i2 != i0 && i2 != i1 )
                {
                  if ( fTimeOut ) goto done;
                  fTimeOut = Super_CheckTimeout( pProgress, pMan );
                  // compute area
                  Area = Area1 + pGate2->Area;
521
                  if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
522 523 524 525 526 527 528 529 530 531
                      break;
                  pSupers[2] = pGate2;  uTruths[2][0] = pGate2->uTruth[0];  uTruths[2][1] = pGate2->uTruth[1];   ptPinDelays[2] = pGate2->ptDelays;

                  Mio_DeriveGateDelays( ppGates[k], ptPinDelays, nFanins, pMan->nVarsMax, SUPER_NO_VAR, tPinDelaysRes, &tPinDelayMax );
                  Mio_DeriveTruthTable( ppGates[k], uTruths, nFanins, pMan->nVarsMax, uTruth );
                  if ( !Super_CompareGates( pMan, uTruth, Area, tPinDelaysRes, pMan->nVarsMax ) )
                      continue;
                  // create a new gate
                  pGateNew = Super_CreateGateNew( pMan, ppGates[k], pSupers, nFanins, uTruth, Area, tPinDelaysRes, tPinDelayMax, pMan->nVarsMax );
                  Super_AddGateToTable( pMan, pGateNew );
532
                  if ( nGatesMax && pMan->nClasses > nGatesMax )
533
                      goto done;
Alan Mishchenko committed
534 535 536 537 538 539 540 541
                }
              }
            }
            break;
        case 4: // four-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
542
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
543 544 545 546 547 548 549
                  break;
              pSupers[0] = pGate0;  uTruths[0][0] = pGate0->uTruth[0];  uTruths[0][1] = pGate0->uTruth[1];  ptPinDelays[0] = pGate0->ptDelays; 

              Super_ManForEachGate( ppGatesLimit, nGatesLimit, i1, pGate1 )
              if ( i1 != i0 )
              {
                Area1 = Area0 + pGate1->Area;
550
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
551 552 553 554 555 556 557
                    break;
                pSupers[1] = pGate1;  uTruths[1][0] = pGate1->uTruth[0];  uTruths[1][1] = pGate1->uTruth[1];  ptPinDelays[1] = pGate1->ptDelays;

                Super_ManForEachGate( ppGatesLimit, nGatesLimit, i2, pGate2 )
                if ( i2 != i0 && i2 != i1 )
                {
                  Area2 = Area1 + pGate2->Area;
558
                  if ( pMan->tAreaMax > 0.0 && Area2 > pMan->tAreaMax )
Alan Mishchenko committed
559 560 561 562 563 564 565 566 567 568
                      break;
                  pSupers[2] = pGate2;  uTruths[2][0] = pGate2->uTruth[0];  uTruths[2][1] = pGate2->uTruth[1];   ptPinDelays[2] = pGate2->ptDelays;

                  Super_ManForEachGate( ppGatesLimit, nGatesLimit, i3, pGate3 )
                  if ( i3 != i0 && i3 != i1 && i3 != i2 )
                  {
                    if ( fTimeOut ) goto done;
                    fTimeOut = Super_CheckTimeout( pProgress, pMan );
                    // compute area
                    Area = Area2 + pGate3->Area;
569
                    if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
570 571 572 573 574 575 576 577 578 579
                        break;
                    pSupers[3] = pGate3;   uTruths[3][0] = pGate3->uTruth[0];  uTruths[3][1] = pGate3->uTruth[1];   ptPinDelays[3] = pGate3->ptDelays;

                    Mio_DeriveGateDelays( ppGates[k], ptPinDelays, nFanins, pMan->nVarsMax, SUPER_NO_VAR, tPinDelaysRes, &tPinDelayMax );
                    Mio_DeriveTruthTable( ppGates[k], uTruths, nFanins, pMan->nVarsMax, uTruth );
                    if ( !Super_CompareGates( pMan, uTruth, Area, tPinDelaysRes, pMan->nVarsMax ) )
                        continue;
                    // create a new gate
                    pGateNew = Super_CreateGateNew( pMan, ppGates[k], pSupers, nFanins, uTruth, Area, tPinDelaysRes, tPinDelayMax, pMan->nVarsMax );
                    Super_AddGateToTable( pMan, pGateNew );
580
                    if ( nGatesMax && pMan->nClasses > nGatesMax )
581
                        goto done;
Alan Mishchenko committed
582 583 584 585 586 587 588 589 590
                  }
                }
              }
            }
            break;
        case 5: // five-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
591
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
592 593 594 595 596 597 598
                  break;
              pSupers[0] = pGate0;  uTruths[0][0] = pGate0->uTruth[0];  uTruths[0][1] = pGate0->uTruth[1];  ptPinDelays[0] = pGate0->ptDelays; 

              Super_ManForEachGate( ppGatesLimit, nGatesLimit, i1, pGate1 )
              if ( i1 != i0 )
              {
                Area1 = Area0 + pGate1->Area;
599
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
600 601 602 603 604 605 606
                    break;
                pSupers[1] = pGate1;  uTruths[1][0] = pGate1->uTruth[0];  uTruths[1][1] = pGate1->uTruth[1];  ptPinDelays[1] = pGate1->ptDelays;

                Super_ManForEachGate( ppGatesLimit, nGatesLimit, i2, pGate2 )
                if ( i2 != i0 && i2 != i1 )
                {
                  Area2 = Area1 + pGate2->Area;
607
                  if ( pMan->tAreaMax > 0.0 && Area2 > pMan->tAreaMax )
Alan Mishchenko committed
608 609 610 611 612 613 614
                      break;
                  pSupers[2] = pGate2;  uTruths[2][0] = pGate2->uTruth[0];  uTruths[2][1] = pGate2->uTruth[1];   ptPinDelays[2] = pGate2->ptDelays;

                  Super_ManForEachGate( ppGatesLimit, nGatesLimit, i3, pGate3 )
                  if ( i3 != i0 && i3 != i1 && i3 != i2 )
                  {
                    Area3 = Area2 + pGate3->Area;
615
                    if ( pMan->tAreaMax > 0.0 && Area3 > pMan->tAreaMax )
Alan Mishchenko committed
616 617 618 619 620 621 622 623 624 625
                        break;
                    pSupers[3] = pGate3;   uTruths[3][0] = pGate3->uTruth[0];  uTruths[3][1] = pGate3->uTruth[1];   ptPinDelays[3] = pGate3->ptDelays;

                    Super_ManForEachGate( ppGatesLimit, nGatesLimit, i4, pGate4 )
                    if ( i4 != i0 && i4 != i1 && i4 != i2 && i4 != i3 )
                    {
                      if ( fTimeOut ) goto done;
                      fTimeOut = Super_CheckTimeout( pProgress, pMan );
                      // compute area
                      Area = Area3 + pGate4->Area;
626
                      if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
627 628 629 630 631 632 633 634 635 636
                          break;
                      pSupers[4] = pGate4;   uTruths[4][0] = pGate4->uTruth[0];  uTruths[4][1] = pGate4->uTruth[1];  ptPinDelays[4] = pGate4->ptDelays;

                      Mio_DeriveGateDelays( ppGates[k], ptPinDelays, nFanins, pMan->nVarsMax, SUPER_NO_VAR, tPinDelaysRes, &tPinDelayMax );
                      Mio_DeriveTruthTable( ppGates[k], uTruths, nFanins, pMan->nVarsMax, uTruth );
                      if ( !Super_CompareGates( pMan, uTruth, Area, tPinDelaysRes, pMan->nVarsMax ) )
                          continue;
                      // create a new gate
                      pGateNew = Super_CreateGateNew( pMan, ppGates[k], pSupers, nFanins, uTruth, Area, tPinDelaysRes, tPinDelayMax, pMan->nVarsMax );
                      Super_AddGateToTable( pMan, pGateNew );
637
                      if ( nGatesMax && pMan->nClasses > nGatesMax )
638
                          goto done;
Alan Mishchenko committed
639 640 641 642 643 644 645 646 647 648
                    }
                  }
                }
              }
            }
            break;
        case 6: // six-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
649
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
650 651 652 653 654 655 656
                  break;
              pSupers[0] = pGate0;  uTruths[0][0] = pGate0->uTruth[0];  uTruths[0][1] = pGate0->uTruth[1];  ptPinDelays[0] = pGate0->ptDelays; 

              Super_ManForEachGate( ppGatesLimit, nGatesLimit, i1, pGate1 )
              if ( i1 != i0 )
              {
                Area1 = Area0 + pGate1->Area;
657
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
658 659 660 661 662 663 664
                    break;
                pSupers[1] = pGate1;  uTruths[1][0] = pGate1->uTruth[0];  uTruths[1][1] = pGate1->uTruth[1];  ptPinDelays[1] = pGate1->ptDelays;

                Super_ManForEachGate( ppGatesLimit, nGatesLimit, i2, pGate2 )
                if ( i2 != i0 && i2 != i1 )
                {
                  Area2 = Area1 + pGate2->Area;
665
                  if ( pMan->tAreaMax > 0.0 && Area2 > pMan->tAreaMax )
Alan Mishchenko committed
666 667 668 669 670 671 672
                      break;
                  pSupers[2] = pGate2;  uTruths[2][0] = pGate2->uTruth[0];  uTruths[2][1] = pGate2->uTruth[1];   ptPinDelays[2] = pGate2->ptDelays;

                  Super_ManForEachGate( ppGatesLimit, nGatesLimit, i3, pGate3 )
                  if ( i3 != i0 && i3 != i1 && i3 != i2 )
                  {
                    Area3 = Area2 + pGate3->Area;
673
                    if ( pMan->tAreaMax > 0.0 && Area3 > pMan->tAreaMax )
Alan Mishchenko committed
674 675 676 677 678 679 680 681 682 683
                        break;
                    pSupers[3] = pGate3;   uTruths[3][0] = pGate3->uTruth[0];  uTruths[3][1] = pGate3->uTruth[1];   ptPinDelays[3] = pGate3->ptDelays;

                    Super_ManForEachGate( ppGatesLimit, nGatesLimit, i4, pGate4 )
                    if ( i4 != i0 && i4 != i1 && i4 != i2 && i4 != i3 )
                    {
                      if ( fTimeOut ) break;
                      fTimeOut = Super_CheckTimeout( pProgress, pMan );
                      // compute area
                      Area4 = Area3 + pGate4->Area;
684
                      if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
685 686 687 688 689 690 691 692 693 694
                          break;
                      pSupers[4] = pGate4;   uTruths[4][0] = pGate4->uTruth[0];  uTruths[4][1] = pGate4->uTruth[1];  ptPinDelays[4] = pGate4->ptDelays;

                      Super_ManForEachGate( ppGatesLimit, nGatesLimit, i5, pGate5 )
                      if ( i5 != i0 && i5 != i1 && i5 != i2 && i5 != i3 && i5 != i4 )
                      {
                        if ( fTimeOut ) goto done;
                        fTimeOut = Super_CheckTimeout( pProgress, pMan );
                        // compute area
                        Area = Area4 + pGate5->Area;
695
                        if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
696 697 698 699 700 701 702 703 704 705
                            break;
                        pSupers[5] = pGate5;   uTruths[5][0] = pGate5->uTruth[0];  uTruths[5][1] = pGate5->uTruth[1];  ptPinDelays[5] = pGate5->ptDelays;

                        Mio_DeriveGateDelays( ppGates[k], ptPinDelays, nFanins, pMan->nVarsMax, SUPER_NO_VAR, tPinDelaysRes, &tPinDelayMax );
                        Mio_DeriveTruthTable( ppGates[k], uTruths, nFanins, pMan->nVarsMax, uTruth );
                        if ( !Super_CompareGates( pMan, uTruth, Area, tPinDelaysRes, pMan->nVarsMax ) )
                            continue;
                        // create a new gate
                        pGateNew = Super_CreateGateNew( pMan, ppGates[k], pSupers, nFanins, uTruth, Area, tPinDelaysRes, tPinDelayMax, pMan->nVarsMax );
                        Super_AddGateToTable( pMan, pGateNew );
706
                        if ( nGatesMax && pMan->nClasses > nGatesMax )
707
                            goto done;
Alan Mishchenko committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721
                      }
                    }
                  }
                }
              }
            }
            break;
        default :
            assert( 0 );
            break;
        }
    }
done: 
    Extra_ProgressBarStop( pProgress );
Alan Mishchenko committed
722
    ABC_FREE( ppGatesLimit );
Alan Mishchenko committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    return pMan;
}

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

  Synopsis    [Transfers gates from table into the array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Super_CheckTimeout( ProgressBar * pPro, Super_Man_t * pMan )
{
739
    abctime TimeNow = Abc_Clock();
Alan Mishchenko committed
740 741 742
    if ( TimeNow > pMan->TimePrint )
    {
        Extra_ProgressBarUpdate( pPro, ++pMan->TimeSec, NULL );
743
        pMan->TimePrint = Abc_Clock() + CLOCKS_PER_SEC;
Alan Mishchenko committed
744
    }
745
    if ( pMan->TimeStop && TimeNow > pMan->TimeStop )
Alan Mishchenko committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    {
        printf ("Timeout!\n");
        return 1;
    }
    pMan->nTried++;
    return 0;
}


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

  Synopsis    [Transfers gates from table into the array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Super_TranferGatesToArray( Super_Man_t * pMan )
{
    stmm_generator * gen;
    Super_Gate_t * pGate, * pList;
Alan Mishchenko committed
770
    ABC_PTRUINT_T Key;
Alan Mishchenko committed
771 772

    // put the gates fron the table into the array
Alan Mishchenko committed
773 774
    ABC_FREE( pMan->pGates );
    pMan->pGates = ABC_ALLOC( Super_Gate_t *, pMan->nAdded );
Alan Mishchenko committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
    pMan->nGates = 0;
    stmm_foreach_item( pMan->tTable, gen, (char **)&Key, (char **)&pList )
    {
        for ( pGate = pList; pGate; pGate = pGate->pNext )
            pMan->pGates[ pMan->nGates++ ] = pGate;
    }
//    assert( pMan->nGates == pMan->nAdded - pMan->nRemoved );
}

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

  Synopsis    [Adds one supergate into the unique table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Super_AddGateToTable( Super_Man_t * pMan, Super_Gate_t * pGate )
{
    Super_Gate_t ** ppList;
Alan Mishchenko committed
798
    ABC_PTRUINT_T Key;
Alan Mishchenko committed
799 800
//    Key = pGate->uTruth[0] + 2003 * pGate->uTruth[1];
    Key = pGate->uTruth[0] ^ pGate->uTruth[1];
Alan Mishchenko committed
801
    if ( !stmm_find_or_add( pMan->tTable, (char *)Key, (char ***)&ppList ) )
802
    {
Alan Mishchenko committed
803
        *ppList = NULL;
804 805
        pMan->nClasses++;
    }
Alan Mishchenko committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    pGate->pNext = *ppList;
    *ppList = pGate;
    pMan->nAdded++;
}

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

  Synopsis    [Check the manager's unique table for comparable gates.]

  Description [Returns 0 if the gate is dominated by others. Returns 1 
  if the gate is new or is better than the available ones. In this case, 
  cleans the table by removing the gates that are worse than the given one.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
824
int Super_CompareGates( Super_Man_t * pMan, unsigned uTruth[], float Area, float tPinDelaysRes[], int nPins )
Alan Mishchenko committed
825 826 827
{
    Super_Gate_t ** ppList, * pPrev, * pGate, * pGate2;
    int i, fNewIsBetter, fGateIsBetter;
Alan Mishchenko committed
828
    ABC_PTRUINT_T Key;
Alan Mishchenko committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

    // skip constant functions
    if ( pMan->nVarsMax < 6 )
    {
        if ( uTruth[0] == 0 || ~uTruth[0] == 0 )
            return 0;
    }
    else
    {
        if ( ( uTruth[0] == 0 && uTruth[1] == 0 ) || ( ~uTruth[0] == 0 && ~uTruth[1] == 0 ) )
            return 0;
    }

    // get hold of the place where the entry is stored
//    Key = uTruth[0] + 2003 * uTruth[1];
    Key = uTruth[0] ^ uTruth[1];
Alan Mishchenko committed
845
    if ( !stmm_find( pMan->tTable, (char *)Key, (char ***)&ppList ) )
Alan Mishchenko committed
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
        return 1; 
    // the entry with this truth table is found
    pPrev = NULL;
    for ( pGate = *ppList, pGate2 = pGate? pGate->pNext: NULL; pGate; 
        pGate = pGate2, pGate2 = pGate? pGate->pNext: NULL )
    {
        pMan->nLookups++;
        if ( pGate->uTruth[0] != uTruth[0] || pGate->uTruth[1] != uTruth[1] )
        {
            pMan->nAliases++;
            continue;
        }
        fGateIsBetter = 0;
        fNewIsBetter  = 0;
        if ( pGate->Area + SUPER_EPSILON < Area )
            fGateIsBetter = 1;
        else if ( pGate->Area > Area + SUPER_EPSILON )
            fNewIsBetter = 1;
        for ( i = 0; i < nPins; i++ )
        {
            if ( pGate->ptDelays[i] == SUPER_NO_VAR || tPinDelaysRes[i] == SUPER_NO_VAR )
                continue;
            if ( pGate->ptDelays[i] + SUPER_EPSILON < tPinDelaysRes[i] )
                fGateIsBetter = 1;
            else if ( pGate->ptDelays[i] > tPinDelaysRes[i] + SUPER_EPSILON )
                fNewIsBetter = 1;
            if ( fGateIsBetter && fNewIsBetter )
                break;
        }
        // consider 4 cases
        if ( fGateIsBetter && fNewIsBetter ) // Pareto points; save both
            pPrev = pGate;
        else if ( fNewIsBetter ) // gate is worse; remove the gate
        {
            if ( pPrev == NULL )
                *ppList = pGate->pNext;
            else
                pPrev->pNext = pGate->pNext;
            Extra_MmFixedEntryRecycle( pMan->pMem, (char *)pGate );
            pMan->nRemoved++;
        }
        else if ( fGateIsBetter ) // new is worse, already dominated no need to see others
            return 0;
        else // if ( !fGateIsBetter && !fNewIsBetter ) // they are identical, no need to see others
            return 0;
    }
    return 1;
}


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

  Synopsis    [Create a new supergate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Super_Gate_t * Super_CreateGateNew( Super_Man_t * pMan, Mio_Gate_t * pRoot, Super_Gate_t ** pSupers, int nSupers, 
    unsigned uTruth[], float Area, float tPinDelaysRes[], float tDelayMax, int nPins )
{
    Super_Gate_t * pSuper;
    pSuper = (Super_Gate_t *)Extra_MmFixedEntryFetch( pMan->pMem );
    memset( pSuper, 0, sizeof(Super_Gate_t) );
    pSuper->pRoot     = pRoot;
    pSuper->uTruth[0] = uTruth[0];
    pSuper->uTruth[1] = uTruth[1];
    memcpy( pSuper->ptDelays, tPinDelaysRes, sizeof(float) * nPins );
    pSuper->Area      = Area;
    pSuper->nFanins   = nSupers;
    memcpy( pSuper->pFanins, pSupers, sizeof(Super_Gate_t *) * nSupers );
    pSuper->pNext     = NULL;
    pSuper->tDelayMax = tDelayMax;
    return pSuper;
}

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

  Synopsis    [Starts the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Super_Man_t * Super_ManStart()
{
    Super_Man_t * pMan;
939
    pMan = ABC_CALLOC( Super_Man_t, 1 );
Alan Mishchenko committed
940
    pMan->pMem     = Extra_MmFixedStart( sizeof(Super_Gate_t) );
941
    pMan->tTable   = stmm_init_table( st__ptrcmp, st__ptrhash );
Alan Mishchenko committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    return pMan;
}

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

  Synopsis    [Stops the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Super_ManStop( Super_Man_t * pMan )
{
Alan Mishchenko committed
958
    Extra_MmFixedStop( pMan->pMem );
Alan Mishchenko committed
959
    if ( pMan->tTable ) stmm_free_table( pMan->tTable );
Alan Mishchenko committed
960 961
    ABC_FREE( pMan->pGates );
    ABC_FREE( pMan );
Alan Mishchenko committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}





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

  Synopsis    [Writes the supergate library into the file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
979
Vec_Str_t * Super_Write( Super_Man_t * pMan )
Alan Mishchenko committed
980
{
981
    Vec_Str_t * vStr;
Alan Mishchenko committed
982 983
    Super_Gate_t * pGateRoot, * pGate;
    stmm_generator * gen;
984
    int fZeroFound, v;
985
    abctime clk;
Alan Mishchenko committed
986
    ABC_PTRUINT_T Key;
Alan Mishchenko committed
987 988 989 990

    if ( pMan->nGates < 1 )
    {
        printf( "The generated library is empty. No output file written.\n" );
991
        return NULL;
Alan Mishchenko committed
992 993 994 995 996 997
    }

    // Filters the supergates by removing those that have fewer inputs than 
    // the given limit, provided that the inputs are not consequtive. 
    // For example, NAND2(a,c) is removed, but NAND2(a,b) is left, 
    // because a and b are consequtive.
Alan Mishchenko committed
998 999
    ABC_FREE( pMan->pGates );
    pMan->pGates = ABC_ALLOC( Super_Gate_t *, pMan->nAdded );
Alan Mishchenko committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    pMan->nGates = 0;
    stmm_foreach_item( pMan->tTable, gen, (char **)&Key, (char **)&pGateRoot )
    {
        for ( pGate = pGateRoot; pGate; pGate = pGate->pNext )
        {
            // skip the elementary variables
            if ( pGate->pRoot == NULL )
                continue;
            // skip the non-consequtive gates
            fZeroFound = 0;
            for ( v = 0; v < pMan->nVarsMax; v++ )
                if ( pGate->ptDelays[v] < SUPER_NO_VAR + SUPER_EPSILON )
                    fZeroFound = 1;
                else if ( fZeroFound )
                    break;
            if ( v < pMan->nVarsMax )
                continue;
            // save the unique gate
            pMan->pGates[ pMan->nGates++ ] = pGate;
        }
    }

1022
clk = Abc_Clock();
Alan Mishchenko committed
1023 1024 1025 1026 1027 1028
    // sort the supergates by truth table
    qsort( (void *)pMan->pGates, pMan->nGates, sizeof(Super_Gate_t *), 
            (int (*)(const void *, const void *)) Super_WriteCompare );
    assert( Super_WriteCompare( pMan->pGates, pMan->pGates + pMan->nGates - 1 ) <= 0 );
if ( pMan->fVerbose )
{
1029
ABC_PRT( "Sorting", Abc_Clock() - clk );
Alan Mishchenko committed
1030 1031 1032 1033
}


    // write library in the old format
1034
clk = Abc_Clock();
Alan Mishchenko committed
1035 1036 1037 1038
    if ( pMan->fWriteOldFormat )
        Super_WriteLibrary( pMan );
if ( pMan->fVerbose )
{
1039
ABC_PRT( "Writing old format", Abc_Clock() - clk );
Alan Mishchenko committed
1040 1041 1042
}

    // write the tree-like structure of supergates
1043
clk = Abc_Clock();
1044
    vStr = Super_WriteLibraryTreeStr( pMan );
Alan Mishchenko committed
1045 1046
if ( pMan->fVerbose )
{
1047
ABC_PRT( "Writing new format", Abc_Clock() - clk );
Alan Mishchenko committed
1048
}
1049
    return vStr;
Alan Mishchenko committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
}


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

  Synopsis    [Writes the file header.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Super_WriteFileHeader( Super_Man_t * pMan, FILE * pFile )
{
    fprintf( pFile, "#\n" );
    fprintf( pFile, "# Supergate library derived for \"%s\" on %s.\n", pMan->pName, Extra_TimeStamp() );
    fprintf( pFile, "#\n" );
1069 1070
    fprintf( pFile, "# Command line: \"super -I %d -L %d -N %d -T %d -D %.2f -A %.2f %s %s\".\n", 
        pMan->nVarsMax, pMan->nLevels, pMan->nGatesMax, pMan->TimeLimit, pMan->tDelayMax, pMan->tAreaMax, (pMan->fSkipInv? "" : "-s"), pMan->pName );
Alan Mishchenko committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
    fprintf( pFile, "#\n" );
    fprintf( pFile, "# The number of inputs      = %10d.\n", pMan->nVarsMax );
    fprintf( pFile, "# The number of levels      = %10d.\n", pMan->nLevels );
    fprintf( pFile, "# The maximum delay         = %10.2f.\n", pMan->tDelayMax  );
    fprintf( pFile, "# The maximum area          = %10.2f.\n", pMan->tAreaMax );
    fprintf( pFile, "# The maximum runtime (sec) = %10d.\n", pMan->TimeLimit );
    fprintf( pFile, "#\n" );
    fprintf( pFile, "# The number of attempts    = %10d.\n", pMan->nTried  );
    fprintf( pFile, "# The number of supergates  = %10d.\n", pMan->nGates  );
    fprintf( pFile, "# The number of functions   = %10d.\n", pMan->nUnique );
1081
    fprintf( pFile, "# The total functions       = %.0f (2^%d).\n", pow((double)2,pMan->nMints), pMan->nMints );
Alan Mishchenko committed
1082
    fprintf( pFile, "#\n" );
1083
    fprintf( pFile, "# Generation time           = %10.2f sec.\n", (float)(pMan->Time)/(float)(CLOCKS_PER_SEC) );
Alan Mishchenko committed
1084 1085 1086 1087 1088
    fprintf( pFile, "#\n" );
    fprintf( pFile, "%s\n", pMan->pName );
    fprintf( pFile, "%d\n", pMan->nVarsMax );
    fprintf( pFile, "%d\n", pMan->nGates );
}
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
void Super_WriteFileHeaderStr( Super_Man_t * pMan, Vec_Str_t * vStr )
{
    char pBuffer[1000];
    sprintf( pBuffer, "#\n" );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# Supergate library derived for \"%s\" on %s.\n", pMan->pName, Extra_TimeStamp() );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "#\n" );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# Command line: \"super -I %d -L %d -N %d -T %d -D %.2f -A %.2f %s %s\".\n", 
        pMan->nVarsMax, pMan->nLevels, pMan->nGatesMax, pMan->TimeLimit, pMan->tDelayMax, pMan->tAreaMax, (pMan->fSkipInv? "" : "-s"), pMan->pName );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "#\n" );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The number of inputs      = %10d.\n", pMan->nVarsMax );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The number of levels      = %10d.\n", pMan->nLevels );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The maximum delay         = %10.2f.\n", pMan->tDelayMax  );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The maximum area          = %10.2f.\n", pMan->tAreaMax );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The maximum runtime (sec) = %10d.\n", pMan->TimeLimit );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "#\n" );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The number of attempts    = %10d.\n", pMan->nTried  );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The number of supergates  = %10d.\n", pMan->nGates  );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The number of functions   = %10d.\n", pMan->nUnique );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# The total functions       = %.0f (2^%d).\n", pow((double)2,pMan->nMints), pMan->nMints );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "#\n" );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "# Generation time           = %10.2f sec.\n", (float)(pMan->Time)/(float)(CLOCKS_PER_SEC) );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "#\n" );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "%s\n", pMan->pName );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "%d\n", pMan->nVarsMax );
    Vec_StrPrintStr( vStr, pBuffer );
    sprintf( pBuffer, "%d\n", pMan->nGates );
    Vec_StrPrintStr( vStr, pBuffer );
}
Alan Mishchenko committed
1136 1137 1138

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

1139
  Synopsis    [Compares two gates.]
Alan Mishchenko committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Super_WriteCompare( Super_Gate_t ** ppG1, Super_Gate_t ** ppG2 )
{
    unsigned * pTruth1 = (*ppG1)->uTruth;
    unsigned * pTruth2 = (*ppG2)->uTruth;
    if ( pTruth1[1] < pTruth2[1] )
        return -1;
    if ( pTruth1[1] > pTruth2[1] )
        return 1;
    if ( pTruth1[0] < pTruth2[0] )
        return -1;
    if ( pTruth1[0] > pTruth2[0] )
        return 1;
    return 0;
}
int Super_DelayCompare( Super_Gate_t ** ppG1, Super_Gate_t ** ppG2 )
{
    if ( (*ppG1)->tDelayMax < (*ppG2)->tDelayMax )
        return -1;
    if ( (*ppG1)->tDelayMax > (*ppG2)->tDelayMax )
        return 1;
    return 0;
}
int Super_AreaCompare( Super_Gate_t ** ppG1, Super_Gate_t ** ppG2 )
{
    if ( (*ppG1)->Area < (*ppG2)->Area )
        return -1;
    if ( (*ppG1)->Area > (*ppG2)->Area )
        return 1;
    return 0;
}






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

  Synopsis    [Writes the gates into the file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
void Super_WriteLibraryGateName_rec( Super_Gate_t * pGate, char * pBuffer )
{
    char Buffer[10];
    int i;

    if ( pGate->pRoot == NULL )
    {
        sprintf( Buffer, "%c", 'a' + pGate->Number );
        strcat( pBuffer, Buffer );
        return;
    }
    strcat( pBuffer, Mio_GateReadName(pGate->pRoot) );
    strcat( pBuffer, "(" );
    for ( i = 0; i < (int)pGate->nFanins; i++ )
    {
        if ( i )
            strcat( pBuffer, "," );
        Super_WriteLibraryGateName_rec( pGate->pFanins[i], pBuffer );
    }
    strcat( pBuffer, ")" );
}
char * Super_WriteLibraryGateName( Super_Gate_t * pGate )
{
    static char Buffer[2000];
    Buffer[0] = 0;
    Super_WriteLibraryGateName_rec( pGate, Buffer );
    return Buffer;
}
void Super_WriteLibraryGate( FILE * pFile, Super_Man_t * pMan, Super_Gate_t * pGate, int Num )
{
    int i;
    fprintf( pFile, "%04d  ", Num );                         // the number
    Extra_PrintBinary( pFile, pGate->uTruth, pMan->nMints ); // the truth table
    fprintf( pFile, "   %5.2f", pGate->tDelayMax );          // the max delay
    fprintf( pFile, "  " );                                  
    for ( i = 0; i < pMan->nVarsMax; i++ )                   // the pin-to-pin delays
        fprintf( pFile, " %5.2f", pGate->ptDelays[i]==SUPER_NO_VAR? 0.0 : pGate->ptDelays[i] );  
    fprintf( pFile, "   %5.2f", pGate->Area );               // the area
    fprintf( pFile, "   " );
    fprintf( pFile, "%s", Super_WriteLibraryGateName(pGate) );      // the symbolic expression
    fprintf( pFile, "\n" );
}
Alan Mishchenko committed
1237 1238 1239 1240
void Super_WriteLibrary( Super_Man_t * pMan )
{
    Super_Gate_t * pGate, * pGateNext;
    FILE * pFile;
Alan Mishchenko committed
1241
    char * FileName;
Alan Mishchenko committed
1242 1243 1244
    char * pNameGeneric;
    int i, Counter;

Alan Mishchenko committed
1245 1246
    FileName = ABC_ALLOC( char, 10000 );

Alan Mishchenko committed
1247 1248 1249
    // get the file name
    pNameGeneric = Extra_FileNameGeneric( pMan->pName );
    sprintf( FileName, "%s.super_old", pNameGeneric );
Alan Mishchenko committed
1250
    ABC_FREE( pNameGeneric );
Alan Mishchenko committed
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

    // count the number of unique functions
    pMan->nUnique = 1;
    Super_ManForEachGate( pMan->pGates, pMan->nGates, i, pGate )
    {
        if ( i == pMan->nGates - 1 )
            break;
        // print the newline if this gate is different from the following one
        pGateNext = pMan->pGates[i+1];
        if ( pGateNext->uTruth[0] != pGate->uTruth[0] || pGateNext->uTruth[1] != pGate->uTruth[1] )
            pMan->nUnique++;
    }

    // start the file
    pFile = fopen( FileName, "w" );
    Super_WriteFileHeader( pMan, pFile );

    // print the gates
    Counter = 0;
    Super_ManForEachGate( pMan->pGates, pMan->nGates, i, pGate )
    {
        Super_WriteLibraryGate( pFile, pMan, pGate, ++Counter );
        if ( i == pMan->nGates - 1 )
            break;
        // print the newline if this gate is different from the following one
        pGateNext = pMan->pGates[i+1];
        if ( pGateNext->uTruth[0] != pGate->uTruth[0] || pGateNext->uTruth[1] != pGate->uTruth[1] )
            fprintf( pFile, "\n" );
    }
    assert( Counter == pMan->nGates );
    fclose( pFile );

1283 1284 1285 1286 1287
    if ( pMan->fVerbose )
    {
        printf( "The supergates are written using old format \"%s\" ", FileName );
        printf( "(%0.3f MB).\n", ((double)Extra_FileSize(FileName))/(1<<20) );
    }
Alan Mishchenko committed
1288 1289

    ABC_FREE( FileName );
Alan Mishchenko committed
1290 1291 1292 1293 1294 1295 1296
}




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

1297
  Synopsis    [Recursively writes the gates.]
Alan Mishchenko committed
1298 1299 1300 1301 1302 1303 1304 1305

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1306
void Super_WriteLibraryTreeFile_rec( FILE * pFile, Super_Man_t * pMan, Super_Gate_t * pSuper, int * pCounter )
Alan Mishchenko committed
1307
{
1308 1309 1310
    int nFanins, i;
    // skip an elementary variable and a gate that was already written
    if ( pSuper->fVar || pSuper->Number > 0 )
Alan Mishchenko committed
1311
        return;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    // write the fanins
    nFanins = Mio_GateReadPinNum(pSuper->pRoot);
    for ( i = 0; i < nFanins; i++ )
        Super_WriteLibraryTreeFile_rec( pFile, pMan, pSuper->pFanins[i], pCounter );
    // finally write the gate
    pSuper->Number = (*pCounter)++;
    fprintf( pFile, "%s", pSuper->fSuper? "* " : "" );
    fprintf( pFile, "%s", Mio_GateReadName(pSuper->pRoot) );
    for ( i = 0; i < nFanins; i++ )
        fprintf( pFile, " %d", pSuper->pFanins[i]->Number );
    // write the formula 
    // this step is optional, the resulting library will work in any case
    // however, it may be helpful to for debugging to compare the same library 
    // written in the old format and written in the new format with formulas
//    fprintf( pFile, "    # %s", Super_WriteLibraryGateName( pSuper ) );
    fprintf( pFile, "\n" );
Alan Mishchenko committed
1328
}
1329
void Super_WriteLibraryTreeFile( Super_Man_t * pMan )
Alan Mishchenko committed
1330 1331 1332
{
    Super_Gate_t * pSuper;
    FILE * pFile;
Alan Mishchenko committed
1333
    char * FileName;
Alan Mishchenko committed
1334 1335 1336 1337
    char * pNameGeneric;
    int i, Counter;
    int posStart;

Alan Mishchenko committed
1338 1339
    FileName = ABC_ALLOC( char, 10000 );

Alan Mishchenko committed
1340 1341 1342
    // get the file name
    pNameGeneric = Extra_FileNameGeneric( pMan->pName );
    sprintf( FileName, "%s.super", pNameGeneric );
Alan Mishchenko committed
1343
    ABC_FREE( pNameGeneric );
Alan Mishchenko committed
1344 1345
 
    // write the elementary variables
1346
    pFile = fopen( FileName, "wb" );
Alan Mishchenko committed
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    Super_WriteFileHeader( pMan, pFile );
    // write the place holder for the number of lines
    posStart = ftell( pFile );
    fprintf( pFile, "         \n" );
    // mark the real supergates
    Super_ManForEachGate( pMan->pGates, pMan->nGates, i, pSuper )
        pSuper->fSuper = 1;
    // write the supergates
    Counter = pMan->nVarsMax;
    Super_ManForEachGate( pMan->pGates, pMan->nGates, i, pSuper )
1357
        Super_WriteLibraryTreeFile_rec( pFile, pMan, pSuper, &Counter );
Alan Mishchenko committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    fclose( pFile );
    // write the number of lines
    pFile = fopen( FileName, "rb+" );
    fseek( pFile, posStart, SEEK_SET );
    fprintf( pFile, "%d", Counter );
    fclose( pFile );

if ( pMan->fVerbose )
{
    printf( "The supergates are written using new format \"%s\" ", FileName );
1368
    printf( "(%0.3f MB).\n", ((double)Extra_FileSize(FileName))/(1<<20) );
Alan Mishchenko committed
1369
}
Alan Mishchenko committed
1370 1371

    ABC_FREE( FileName );
Alan Mishchenko committed
1372 1373
}

1374

Alan Mishchenko committed
1375 1376
/**Function*************************************************************

1377
  Synopsis    [Recursively writes the gates.]
Alan Mishchenko committed
1378 1379 1380 1381 1382 1383 1384 1385

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1386
void Super_WriteLibraryTreeStr_rec( Vec_Str_t * vStr, Super_Man_t * pMan, Super_Gate_t * pSuper, int * pCounter )
Alan Mishchenko committed
1387 1388 1389 1390 1391 1392
{
    int nFanins, i;
    // skip an elementary variable and a gate that was already written
    if ( pSuper->fVar || pSuper->Number > 0 )
        return;
    // write the fanins
1393
    nFanins = Mio_GateReadPinNum(pSuper->pRoot);
Alan Mishchenko committed
1394
    for ( i = 0; i < nFanins; i++ )
1395
        Super_WriteLibraryTreeStr_rec( vStr, pMan, pSuper->pFanins[i], pCounter );
Alan Mishchenko committed
1396 1397
    // finally write the gate
    pSuper->Number = (*pCounter)++;
1398 1399 1400 1401 1402 1403
//    fprintf( pFile, "%s", pSuper->fSuper? "* " : "" );
//    fprintf( pFile, "%s", Mio_GateReadName(pSuper->pRoot) );
//    for ( i = 0; i < nFanins; i++ )
//        fprintf( pFile, " %d", pSuper->pFanins[i]->Number );
    Vec_StrPrintStr( vStr, pSuper->fSuper? "* " : "" );
    Vec_StrPrintStr( vStr, Mio_GateReadName(pSuper->pRoot) );
Alan Mishchenko committed
1404
    for ( i = 0; i < nFanins; i++ )
1405 1406 1407 1408
    {
        Vec_StrPrintStr( vStr, " " );
        Vec_StrPrintNum( vStr, pSuper->pFanins[i]->Number );    
    }
Alan Mishchenko committed
1409 1410 1411 1412 1413
    // write the formula 
    // this step is optional, the resulting library will work in any case
    // however, it may be helpful to for debugging to compare the same library 
    // written in the old format and written in the new format with formulas
//    fprintf( pFile, "    # %s", Super_WriteLibraryGateName( pSuper ) );
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
//    fprintf( pFile, "\n" );
    Vec_StrPrintStr( vStr, "\n" );
}
Vec_Str_t * Super_WriteLibraryTreeStr( Super_Man_t * pMan )
{
    char pInsert[16];
    Vec_Str_t * vStr;
    Super_Gate_t * pSuper;
    int i, Counter;
    int posStart;
     // write the elementary variables
    vStr = Vec_StrAlloc( 1000 );
    Super_WriteFileHeaderStr( pMan, vStr );
    // write the place holder for the number of lines
    posStart = Vec_StrSize( vStr );
    for ( i = 0; i < 9; i++ )
        Vec_StrPush( vStr, ' ' );
    Vec_StrPush( vStr, '\n' );
    // mark the real supergates
    Super_ManForEachGate( pMan->pGates, pMan->nGates, i, pSuper )
        pSuper->fSuper = 1;
    // write the supergates
    Counter = pMan->nVarsMax;
    Super_ManForEachGate( pMan->pGates, pMan->nGates, i, pSuper )
        Super_WriteLibraryTreeStr_rec( vStr, pMan, pSuper, &Counter );
    Vec_StrPush( vStr, 0 );
    // write the number of lines
    sprintf( pInsert, "%d", Counter );
    for ( i = 0; i < (int)strlen(pInsert); i++ )
        Vec_StrWriteEntry( vStr, posStart + i, pInsert[i] );
    return vStr;
}
void Super_WriteLibraryTree( Super_Man_t * pMan )
{
    Vec_Str_t * vStr;
    char * pFileName = Extra_FileNameGenericAppend( pMan->pName, ".super" );
    FILE * pFile = fopen( pFileName, "wb" );
    if ( pFile == NULL )
    {     
        printf( "Cannot open output file \"%s\".\n", pFileName );
        return;
    }
    vStr = Super_WriteLibraryTreeStr( pMan );
    fwrite( Vec_StrArray(vStr), 1, Vec_StrSize(vStr), pFile );
    fclose( pFile );
    Vec_StrFree( vStr );
    // report the result of writing
    if ( pMan->fVerbose )
    {
        printf( "The supergates are written using new format \"%s\" ", pFileName );
        printf( "(%0.3f MB).\n", ((double)Extra_FileSize(pFileName))/(1<<20) );
    }
Alan Mishchenko committed
1466 1467 1468 1469 1470 1471
}

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

1472 1473
ABC_NAMESPACE_IMPL_END