superGate.c 56.7 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
    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 );
150 151 152 153 154
    if ( vStr ) 
    {
        fwrite( Vec_StrArray(vStr), 1, Vec_StrSize(vStr), pFile );
        Vec_StrFree( vStr );
    }
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    fclose( pFile );
    // 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
178 179
    Super_Man_t * pMan;
    Mio_Gate_t ** ppGates;
180
    int nGates, Level;
181
    abctime clk, clockStart;
Alan Mishchenko committed
182 183

    assert( nVarsMax < 7 );
184
    if ( nGatesMax && nGatesMax < nVarsMax )
185 186 187
    {
        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" );
188
        return NULL;
189
    }
Alan Mishchenko committed
190 191

    // get the root gates
192
    ppGates = Mio_CollectRoots( pLibGen, nVarsMax, tDelayMax, 0, &nGates, fVerbose );
193
    if ( nGatesMax && nGates >= nGatesMax )
194 195 196 197
    {
        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
198 199 200 201

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

    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
218
        ABC_FREE( ppGates );
Alan Mishchenko committed
219

220
        return NULL;
Alan Mishchenko committed
221 222 223 224 225 226
    }

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

    // perform the computation of supergates
227
    clockStart = Abc_Clock();
Alan Mishchenko committed
228 229
if ( fVerbose )
{
230 231
    printf( "Computing supergates with %d inputs, %d levels, and %d max gates.\n", 
        pMan->nVarsMax, nLevels, nGatesMax );
Alan Mishchenko committed
232 233 234 235 236 237
    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++ )
    {
238
        if ( pMan->TimeStop && Abc_Clock() > pMan->TimeStop )
Alan Mishchenko committed
239
            break;
240
clk = Abc_Clock();
241
        Super_Compute( pMan, ppGates, nGates, nGatesMax, fSkipInv );
Alan Mishchenko committed
242 243 244 245 246
        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 );
247
ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
248 249 250
fflush( stdout );
}
    }
251
    pMan->Time = Abc_Clock() - clockStart;
Alan Mishchenko committed
252 253 254 255 256 257 258

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

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


/**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
290
    pMan->pGates    = ABC_ALLOC( Super_Gate_t *, nVarsMax + 2 ); 
Alan Mishchenko committed
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 332 333 334
    // 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
335
  derived from the given set of root gates (from genlib library) by composing
Alan Mishchenko committed
336 337 338 339 340 341 342 343 344 345
  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     []

***********************************************************************/
346
Super_Man_t * Super_Compute( Super_Man_t * pMan, Mio_Gate_t ** ppGates, int nGates, int nGatesMax, int fSkipInv )
Alan Mishchenko committed
347 348 349
{
    Super_Gate_t * pSupers[6], * pGate0, * pGate1, * pGate2, * pGate3, * pGate4, * pGate5, * pGateNew;
    float tPinDelaysRes[6], * ptPinDelays[6], tPinDelayMax, tDelayMio;
Alan Mishchenko committed
350 351
    float Area = 0.0; // Suppress "might be used uninitialized"
    float Area0, Area1, Area2, Area3, Area4, AreaMio;
Alan Mishchenko committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    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 );
    }
374
    qsort( (void *)pMan->pGates, (size_t)pMan->nGates, sizeof(Super_Gate_t *), 
Alan Mishchenko committed
375 376 377 378 379 380 381 382
            (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 );
383
    pMan->TimePrint = Abc_Clock() + CLOCKS_PER_SEC;
Alan Mishchenko committed
384
    ppGatesLimit = ABC_ALLOC( Super_Gate_t *, pMan->nGates );
Alan Mishchenko committed
385 386 387 388 389 390 391 392 393 394 395
    // 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
            {
396
                if ( Mio_GateReadPinNum(ppGates[k]) >= iPruneLimitRoot )
Alan Mishchenko committed
397 398 399
                    continue;
            }
        }
Alan Mishchenko committed
400 401 402 403 404 405
/*
        if ( strcmp(Mio_GateReadName(ppGates[k]), "MUX2IX0") == 0 )
        {
            int s = 0;
        }
*/
Alan Mishchenko committed
406 407 408 409 410 411 412 413 414
        // 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];
415
            if ( ppGatesLimit[t++]->tDelayMax + tDelayMio > pMan->tDelayMax && pMan->tDelayMax > 0.0 )
Alan Mishchenko committed
416 417 418 419 420 421
                break;
        }
        nGatesLimit = t;

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

        // 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 );
430
        qsort( (void *)ppGatesLimit, (size_t)nGatesLimit, sizeof(Super_Gate_t *), 
Alan Mishchenko committed
431 432 433 434 435 436 437
                (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]);
438
        nFanins = Mio_GateReadPinNum(ppGates[k]);
Alan Mishchenko committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
        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;
456
              if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
457 458 459 460 461 462 463 464 465 466
                  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 );
467
              if ( nGatesMax && pMan->nClasses > nGatesMax )
468 469
                  goto done;
            }
Alan Mishchenko committed
470 471 472 473 474
            break;
        case 2: // two-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
475
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
476 477 478 479 480 481 482 483 484
                  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;
485
                if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
486 487 488 489 490 491 492 493 494 495
                    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 );
496
                if ( nGatesMax && pMan->nClasses > nGatesMax )
497
                    goto done;
Alan Mishchenko committed
498 499 500 501 502 503 504
              }
            }
            break;
        case 3: // three-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
505
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
506 507 508 509 510 511 512
                  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;
513
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
514 515 516 517 518 519 520 521 522 523
                    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;
524
                  if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
525 526 527 528 529 530 531 532 533 534
                      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 );
535
                  if ( nGatesMax && pMan->nClasses > nGatesMax )
536
                      goto done;
Alan Mishchenko committed
537 538 539 540 541 542 543 544
                }
              }
            }
            break;
        case 4: // four-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
545
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
546 547 548 549 550 551 552
                  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;
553
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
554 555 556 557 558 559 560
                    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;
561
                  if ( pMan->tAreaMax > 0.0 && Area2 > pMan->tAreaMax )
Alan Mishchenko committed
562 563 564 565 566 567 568 569 570 571
                      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;
572
                    if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
573 574 575 576 577 578 579 580 581 582
                        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 );
583
                    if ( nGatesMax && pMan->nClasses > nGatesMax )
584
                        goto done;
Alan Mishchenko committed
585 586 587 588 589 590 591 592 593
                  }
                }
              }
            }
            break;
        case 5: // five-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
594
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
595 596 597 598 599 600 601
                  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;
602
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
603 604 605 606 607 608 609
                    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;
610
                  if ( pMan->tAreaMax > 0.0 && Area2 > pMan->tAreaMax )
Alan Mishchenko committed
611 612 613 614 615 616 617
                      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;
618
                    if ( pMan->tAreaMax > 0.0 && Area3 > pMan->tAreaMax )
Alan Mishchenko committed
619 620 621 622 623 624 625 626 627 628
                        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;
629
                      if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
630 631 632 633 634 635 636 637 638 639
                          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 );
640
                      if ( nGatesMax && pMan->nClasses > nGatesMax )
641
                          goto done;
Alan Mishchenko committed
642 643 644 645 646 647 648 649 650 651
                    }
                  }
                }
              }
            }
            break;
        case 6: // six-input root gate
            Super_ManForEachGate( ppGatesLimit, nGatesLimit, i0, pGate0 )
            {
              Area0 = AreaMio + pGate0->Area;
652
              if ( pMan->tAreaMax > 0.0 && Area0 > pMan->tAreaMax )
Alan Mishchenko committed
653 654 655 656 657 658 659
                  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;
660
                if ( pMan->tAreaMax > 0.0 && Area1 > pMan->tAreaMax )
Alan Mishchenko committed
661 662 663 664 665 666 667
                    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;
668
                  if ( pMan->tAreaMax > 0.0 && Area2 > pMan->tAreaMax )
Alan Mishchenko committed
669 670 671 672 673 674 675
                      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;
676
                    if ( pMan->tAreaMax > 0.0 && Area3 > pMan->tAreaMax )
Alan Mishchenko committed
677 678 679 680 681 682 683 684 685 686
                        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;
687
                      if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
688 689 690 691 692 693 694 695 696 697
                          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;
698
                        if ( pMan->tAreaMax > 0.0 && Area > pMan->tAreaMax )
Alan Mishchenko committed
699 700 701 702 703 704 705 706 707 708
                            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 );
709
                        if ( nGatesMax && pMan->nClasses > nGatesMax )
710
                            goto done;
Alan Mishchenko committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724
                      }
                    }
                  }
                }
              }
            }
            break;
        default :
            assert( 0 );
            break;
        }
    }
done: 
    Extra_ProgressBarStop( pProgress );
Alan Mishchenko committed
725
    ABC_FREE( ppGatesLimit );
Alan Mishchenko committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    return pMan;
}

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

  Synopsis    [Transfers gates from table into the array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Super_CheckTimeout( ProgressBar * pPro, Super_Man_t * pMan )
{
742
    abctime TimeNow = Abc_Clock();
Alan Mishchenko committed
743 744 745
    if ( TimeNow > pMan->TimePrint )
    {
        Extra_ProgressBarUpdate( pPro, ++pMan->TimeSec, NULL );
746
        pMan->TimePrint = Abc_Clock() + CLOCKS_PER_SEC;
Alan Mishchenko committed
747
    }
748
    if ( pMan->TimeStop && TimeNow > pMan->TimeStop )
Alan Mishchenko committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    {
        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
773
    ABC_PTRUINT_T Key;
Alan Mishchenko committed
774 775

    // put the gates fron the table into the array
Alan Mishchenko committed
776 777
    ABC_FREE( pMan->pGates );
    pMan->pGates = ABC_ALLOC( Super_Gate_t *, pMan->nAdded );
Alan Mishchenko committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    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
801
    ABC_PTRUINT_T Key;
Alan Mishchenko committed
802 803
//    Key = pGate->uTruth[0] + 2003 * pGate->uTruth[1];
    Key = pGate->uTruth[0] ^ pGate->uTruth[1];
Alan Mishchenko committed
804
    if ( !stmm_find_or_add( pMan->tTable, (char *)Key, (char ***)&ppList ) )
805
    {
Alan Mishchenko committed
806
        *ppList = NULL;
807 808
        pMan->nClasses++;
    }
Alan Mishchenko committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    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     []

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

    // 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
848
    if ( !stmm_find( pMan->tTable, (char *)Key, (char ***)&ppList ) )
Alan Mishchenko committed
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 939 940 941
        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;
942
    pMan = ABC_CALLOC( Super_Man_t, 1 );
Alan Mishchenko committed
943
    pMan->pMem     = Extra_MmFixedStart( sizeof(Super_Gate_t) );
944
    pMan->tTable   = stmm_init_table( st__ptrcmp, st__ptrhash );
Alan Mishchenko committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    return pMan;
}

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

  Synopsis    [Stops the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

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





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

  Synopsis    [Writes the supergate library into the file.]

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

    // 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
1001 1002
    ABC_FREE( pMan->pGates );
    pMan->pGates = ABC_ALLOC( Super_Gate_t *, pMan->nAdded );
Alan Mishchenko committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    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;
        }
    }

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


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

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


/**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" );
1072 1073
    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
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    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 );
1084
    fprintf( pFile, "# The total functions       = %.0f (2^%d).\n", pow((double)2,pMan->nMints), pMan->nMints );
Alan Mishchenko committed
1085
    fprintf( pFile, "#\n" );
1086
    fprintf( pFile, "# Generation time           = %10.2f sec.\n", (float)(pMan->Time)/(float)(CLOCKS_PER_SEC) );
Alan Mishchenko committed
1087 1088 1089 1090 1091
    fprintf( pFile, "#\n" );
    fprintf( pFile, "%s\n", pMan->pName );
    fprintf( pFile, "%d\n", pMan->nVarsMax );
    fprintf( pFile, "%d\n", pMan->nGates );
}
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 1136 1137 1138
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
1139 1140 1141

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

1142
  Synopsis    [Compares two gates.]
Alan Mishchenko committed
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 1195 1196 1197

  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     []

***********************************************************************/
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 1237 1238 1239
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
1240 1241 1242 1243
void Super_WriteLibrary( Super_Man_t * pMan )
{
    Super_Gate_t * pGate, * pGateNext;
    FILE * pFile;
Alan Mishchenko committed
1244
    char * FileName;
Alan Mishchenko committed
1245 1246 1247
    char * pNameGeneric;
    int i, Counter;

Alan Mishchenko committed
1248 1249
    FileName = ABC_ALLOC( char, 10000 );

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

    // 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 );

1286 1287 1288 1289 1290
    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
1291 1292

    ABC_FREE( FileName );
Alan Mishchenko committed
1293 1294 1295 1296 1297 1298 1299
}




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

1300
  Synopsis    [Recursively writes the gates.]
Alan Mishchenko committed
1301 1302 1303 1304 1305 1306 1307 1308

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1309
void Super_WriteLibraryTreeFile_rec( FILE * pFile, Super_Man_t * pMan, Super_Gate_t * pSuper, int * pCounter )
Alan Mishchenko committed
1310
{
1311 1312 1313
    int nFanins, i;
    // skip an elementary variable and a gate that was already written
    if ( pSuper->fVar || pSuper->Number > 0 )
Alan Mishchenko committed
1314
        return;
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    // 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
1331
}
1332
void Super_WriteLibraryTreeFile( Super_Man_t * pMan )
Alan Mishchenko committed
1333 1334 1335
{
    Super_Gate_t * pSuper;
    FILE * pFile;
Alan Mishchenko committed
1336
    char * FileName;
Alan Mishchenko committed
1337 1338 1339 1340
    char * pNameGeneric;
    int i, Counter;
    int posStart;

Alan Mishchenko committed
1341 1342
    FileName = ABC_ALLOC( char, 10000 );

Alan Mishchenko committed
1343 1344 1345
    // get the file name
    pNameGeneric = Extra_FileNameGeneric( pMan->pName );
    sprintf( FileName, "%s.super", pNameGeneric );
Alan Mishchenko committed
1346
    ABC_FREE( pNameGeneric );
Alan Mishchenko committed
1347 1348
 
    // write the elementary variables
1349
    pFile = fopen( FileName, "wb" );
Alan Mishchenko committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    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 )
1360
        Super_WriteLibraryTreeFile_rec( pFile, pMan, pSuper, &Counter );
Alan Mishchenko committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    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 );
1371
    printf( "(%0.3f MB).\n", ((double)Extra_FileSize(FileName))/(1<<20) );
Alan Mishchenko committed
1372
}
Alan Mishchenko committed
1373 1374

    ABC_FREE( FileName );
Alan Mishchenko committed
1375 1376
}

1377

Alan Mishchenko committed
1378 1379
/**Function*************************************************************

1380
  Synopsis    [Recursively writes the gates.]
Alan Mishchenko committed
1381 1382 1383 1384 1385 1386 1387 1388

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1389
void Super_WriteLibraryTreeStr_rec( Vec_Str_t * vStr, Super_Man_t * pMan, Super_Gate_t * pSuper, int * pCounter )
Alan Mishchenko committed
1390 1391 1392 1393 1394 1395
{
    int nFanins, i;
    // skip an elementary variable and a gate that was already written
    if ( pSuper->fVar || pSuper->Number > 0 )
        return;
    // write the fanins
1396
    nFanins = Mio_GateReadPinNum(pSuper->pRoot);
Alan Mishchenko committed
1397
    for ( i = 0; i < nFanins; i++ )
1398
        Super_WriteLibraryTreeStr_rec( vStr, pMan, pSuper->pFanins[i], pCounter );
Alan Mishchenko committed
1399 1400
    // finally write the gate
    pSuper->Number = (*pCounter)++;
1401 1402 1403 1404 1405 1406
//    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
1407
    for ( i = 0; i < nFanins; i++ )
1408 1409 1410 1411
    {
        Vec_StrPrintStr( vStr, " " );
        Vec_StrPrintNum( vStr, pSuper->pFanins[i]->Number );    
    }
Alan Mishchenko committed
1412 1413 1414 1415 1416
    // 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 ) );
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 1466 1467 1468
//    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
1469 1470 1471 1472 1473 1474
}

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

1475 1476
ABC_NAMESPACE_IMPL_END