mioUtils.c 45.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    [mioUtils.c]

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

  Synopsis    [File reading/writing for technology mapping.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: mioUtils.c,v 1.6 2004/09/03 18:02:20 satrajit Exp $]

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

19
#include <math.h>
Alan Mishchenko committed
20
#include "mioInt.h"
21
#include "base/main/main.h"
22
#include "exp.h"
23
#include "misc/util/utilTruth.h"
24
#include "opt/dau/dau.h"
25
#include "misc/util/utilNam.h"
26
#include "map/scl/sclLib.h"
27
#include "map/scl/sclCon.h"
Alan Mishchenko committed
28

29 30 31
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
32 33 34 35 36
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
37
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_LibraryDelete( Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate, * pGate2;
    if ( pLib == NULL )
        return;
Alan Mishchenko committed
56
    // free the bindings of nodes to gates from this library for all networks
Alan Mishchenko committed
57
    Abc_FrameUnmapAllNetworks( Abc_FrameGetGlobalFrame() );
Alan Mishchenko committed
58
    // free the library
Alan Mishchenko committed
59
    ABC_FREE( pLib->pName );
Alan Mishchenko committed
60 61
    Mio_LibraryForEachGateSafe( pLib, pGate, pGate2 )
        Mio_GateDelete( pGate );
62
    Mem_FlexStop( pLib->pMmFlex, 0 );
Alan Mishchenko committed
63 64
    Vec_StrFree( pLib->vCube );
    if ( pLib->tName2Gate )
65
        st__free_table( pLib->tName2Gate );
66 67
//    if ( pLib->dd )
//        Cudd_Quit( pLib->dd );
Alan Mishchenko committed
68 69 70
    ABC_FREE( pLib->ppGates0 );
    ABC_FREE( pLib->ppGatesName );
    ABC_FREE( pLib );
Alan Mishchenko committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_GateDelete( Mio_Gate_t * pGate )
{
    Mio_Pin_t * pPin, * pPin2;
87 88 89
    if ( pGate->nInputs > 6 )
        ABC_FREE( pGate->pTruth );
    Vec_IntFreeP( &pGate->vExpr );
Alan Mishchenko committed
90 91 92
    ABC_FREE( pGate->pOutName );
    ABC_FREE( pGate->pName );
    ABC_FREE( pGate->pForm );
93 94
//    if ( pGate->bFunc )
//        Cudd_RecursiveDeref( pGate->pLib->dd, pGate->bFunc );
Alan Mishchenko committed
95
    Mio_GateForEachPinSafe( pGate, pPin, pPin2 )
Alan Mishchenko committed
96
        Mio_PinDelete( pPin );   
Alan Mishchenko committed
97
    ABC_FREE( pGate );
Alan Mishchenko committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_PinDelete( Mio_Pin_t * pPin )
{
Alan Mishchenko committed
113 114
    ABC_FREE( pPin->pName );
    ABC_FREE( pPin );
Alan Mishchenko committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Mio_Pin_t * Mio_PinDup( Mio_Pin_t * pPin )
{
    Mio_Pin_t * pPinNew;

Alan Mishchenko committed
132
    pPinNew = ABC_ALLOC( Mio_Pin_t, 1 );
Alan Mishchenko committed
133
    *pPinNew = *pPin;
134
    pPinNew->pName = (pPinNew->pName ? Abc_UtilStrsav(pPinNew->pName) : NULL);
Alan Mishchenko committed
135 136 137 138 139 140 141 142 143 144
    pPinNew->pNext = NULL;

    return pPinNew;
}




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

145
  Synopsis    [Check if pin characteristics are the same.]
Alan Mishchenko committed
146 147 148 149 150 151 152 153

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
154
int Mio_CheckPins( Mio_Pin_t * pPin1, Mio_Pin_t * pPin2 )
Alan Mishchenko committed
155
{
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    if ( pPin1 == NULL || pPin2 == NULL )
        return 1;
    if ( pPin1->dLoadInput != pPin2->dLoadInput )
        return 0;
    if ( pPin1->dLoadMax != pPin2->dLoadMax )
        return 0;
    if ( pPin1->dDelayBlockRise != pPin2->dDelayBlockRise )
        return 0;
    if ( pPin1->dDelayFanoutRise != pPin2->dDelayFanoutRise )
        return 0;
    if ( pPin1->dDelayBlockFall != pPin2->dDelayBlockFall )
        return 0;
    if ( pPin1->dDelayFanoutFall != pPin2->dDelayFanoutFall )
        return 0;
    return 1;
Alan Mishchenko committed
171
}
172 173 174 175 176 177 178 179 180 181 182 183
int Mio_CheckGates( Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate;
    Mio_Pin_t * pPin0 = NULL, * pPin = NULL;
    Mio_LibraryForEachGate( pLib, pGate )
        Mio_GateForEachPin( pGate, pPin )
            if ( Mio_CheckPins( pPin0, pPin ) )
                pPin0 = pPin;
            else
                return 0;
    return 1;
}
Alan Mishchenko committed
184 185 186 187 188 189 190 191 192 193 194 195

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
196
void Mio_WritePin( FILE * pFile, Mio_Pin_t * pPin, int NameLen, int fAllPins )
Alan Mishchenko committed
197
{
198 199 200 201
    char * pPhaseNames[10] = { "UNKNOWN", "INV", "NONINV" };
    if ( fAllPins )
        fprintf( pFile, "PIN *  " );
    else
202
        fprintf( pFile, "\n    PIN %*s  ", NameLen, pPin->pName );
203 204 205
    fprintf( pFile, "%7s ",   pPhaseNames[pPin->Phase] );
    fprintf( pFile, "%3d ",   (int)pPin->dLoadInput );
    fprintf( pFile, "%3d ",   (int)pPin->dLoadMax );
206 207 208 209
    fprintf( pFile, "%8.2f ", pPin->dDelayBlockRise );
    fprintf( pFile, "%8.2f ", pPin->dDelayFanoutRise );
    fprintf( pFile, "%8.2f ", pPin->dDelayBlockFall );
    fprintf( pFile, "%8.2f",  pPin->dDelayFanoutFall );
210 211 212 213 214 215 216 217 218 219 220
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []
Alan Mishchenko committed
221

222
***********************************************************************/
223
void Mio_WriteGate( FILE * pFile, Mio_Gate_t * pGate, int GateLen, int NameLen, int FormLen, int fPrintSops, int fAllPins )
224 225
{
    char Buffer[5000];
226
    Mio_Pin_t * pPin;
227 228 229 230
    assert( NameLen+FormLen+2 < 5000 );
    sprintf( Buffer, "%s=%s;",    pGate->pOutName, pGate->pForm );
    fprintf( pFile, "GATE %-*s ", GateLen, pGate->pName );
    fprintf( pFile, "%8.2f  ",    pGate->dArea );
231
    fprintf( pFile, "%-*s ",      Abc_MinInt(NameLen+FormLen+2, 60), Buffer );
Alan Mishchenko committed
232 233 234
    // print the pins
    if ( fPrintSops )
        fprintf( pFile, "%s",       pGate->pSop? pGate->pSop : "unspecified\n" );
235 236 237
    if ( fAllPins && pGate->pPins ) // equal pins
        Mio_WritePin( pFile, pGate->pPins, NameLen, 1 );
    else // different pins
238 239 240
        Mio_GateForEachPin( pGate, pPin )
            Mio_WritePin( pFile, pPin, NameLen, 0 );
    fprintf( pFile, "\n" );
Alan Mishchenko committed
241 242 243 244 245 246 247 248 249 250 251 252 253
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
254
void Mio_WriteLibrary( FILE * pFile, Mio_Library_t * pLib, int fPrintSops, int fShort, int fSelected )
Alan Mishchenko committed
255
{
256 257
    Mio_Gate_t * pGate;
    Mio_Pin_t * pPin;
258 259
    Vec_Ptr_t * vGates = Vec_PtrAlloc( 1000 );
    int i, nCells, GateLen = 0, NameLen = 0, FormLen = 0;
260
    int fAllPins = fShort || Mio_CheckGates( pLib );
261 262 263 264 265 266 267 268 269 270 271 272 273
    if ( fSelected )
    {
        Mio_Cell2_t * pCells = Mio_CollectRootsNewDefault2( 6, &nCells, 0 );
        for ( i = 0; i < nCells; i++ )
            Vec_PtrPush( vGates, pCells[i].pMioGate );
        ABC_FREE( pCells );
    }
    else
    {
        for ( i = 0; i < pLib->nGates; i++ )
            Vec_PtrPush( vGates, pLib->ppGates0[i] );
    }
    Vec_PtrForEachEntry( Mio_Gate_t *, vGates, pGate, i )
274 275 276 277 278 279 280
    {
        GateLen = Abc_MaxInt( GateLen, strlen(pGate->pName) );
        NameLen = Abc_MaxInt( NameLen, strlen(pGate->pOutName) );
        FormLen = Abc_MaxInt( FormLen, strlen(pGate->pForm) );
        Mio_GateForEachPin( pGate, pPin )
            NameLen = Abc_MaxInt( NameLen, strlen(pPin->pName) );
    }
281 282 283 284
    fprintf( pFile, "# The genlib library \"%s\" with %d gates written by ABC on %s\n", pLib->pName, Vec_PtrSize(vGates), Extra_TimeStamp() );
    Vec_PtrForEachEntry( Mio_Gate_t *, vGates, pGate, i )
        Mio_WriteGate( pFile, pGate, GateLen, NameLen, FormLen, fPrintSops, fAllPins );
    Vec_PtrFree( vGates );
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
}

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

  Synopsis    [Compares the max delay of two gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_DelayCompare( Mio_Gate_t ** ppG1, Mio_Gate_t ** ppG2 )
{
300 301 302
    int Comp;
    float Eps = (float)0.0094636;
    if ( (*ppG1)->dDelayMax < (*ppG2)->dDelayMax - Eps )
303
        return -1;
304
    if ( (*ppG1)->dDelayMax > (*ppG2)->dDelayMax + Eps )
305
        return 1;
306 307 308 309 310 311 312
    // compare names
    Comp = strcmp( (*ppG1)->pName, (*ppG2)->pName );
    if ( Comp < 0 )
        return -1;
    if ( Comp > 0 )
        return 1;
    assert( 0 );
313
    return 0;
Alan Mishchenko committed
314
}
315 316
int Mio_AreaCompare( Mio_Cell_t * pG1, Mio_Cell_t * pG2 )
{
317 318 319
    int Comp;
    float Eps = (float)0.0094636;
    if ( pG1->nFanins < pG2->nFanins )
320
        return -1;
321
    if ( pG1->nFanins > pG2->nFanins )
322
        return 1;
323
    if ( pG1->Area < pG2->Area - Eps )
324
        return -1;
325
    if ( pG1->Area > pG2->Area + Eps )
326
        return 1;
327 328 329 330 331 332 333
    // compare names
    Comp = strcmp( pG1->pName, pG2->pName );
    if ( Comp < 0 )
        return -1;
    if ( Comp > 0 )
        return 1;
    assert( 0 );
334 335
    return 0;
}
336 337 338 339 340 341 342
int Mio_AreaCompare2( Mio_Cell2_t * pG1, Mio_Cell2_t * pG2 )
{
    int Comp;
    if ( pG1->nFanins < pG2->nFanins )
        return -1;
    if ( pG1->nFanins > pG2->nFanins )
        return 1;
343
    if ( pG1->AreaW < pG2->AreaW )
344
        return -1;
345
    if ( pG1->AreaW > pG2->AreaW )
346 347 348 349 350 351 352 353 354 355
        return 1;
    // compare names
    Comp = strcmp( pG1->pName, pG2->pName );
    if ( Comp < 0 )
        return -1;
    if ( Comp > 0 )
        return 1;
    assert( 0 );
    return 0;
}
Alan Mishchenko committed
356 357 358 359 360 361 362 363 364 365 366 367 368

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

  Synopsis    [Collects the set of root gates.]

  Description [Only collects the gates with unique functionality, 
  which have fewer inputs and shorter delay than the given limits.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
static inline float Mio_CellDelayAve( Mio_Cell_t * pCell )
{
    float CellDelay = 0; int k;
    for ( k = 0; k < (int)pCell->nFanins; k++ )
        CellDelay += pCell->Delays[k];
    if ( pCell->nFanins )
        CellDelay /= pCell->nFanins;
    return CellDelay;
}
static inline float Mio_GateDelayAve( Mio_Gate_t * pGate )
{
    float GateDelay = 0;
    Mio_Pin_t * pPin; 
    Mio_GateForEachPin( pGate, pPin )
        GateDelay += (float)(0.5 * pPin->dDelayBlockRise + 0.5 * pPin->dDelayBlockFall);
    if ( pGate->nInputs )
        GateDelay /= pGate->nInputs;
    return GateDelay;
}
static inline int Mio_CompareTwoGates( Mio_Gate_t * pCell, Mio_Gate_t * pGate )
{
    int Comp;
391
    float Eps = (float)0.0094636;
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    float CellDelay, GateDelay;
    // compare areas
    if ( pCell->dArea > (float)pGate->dArea + Eps )
        return 1;
    if ( pCell->dArea < (float)pGate->dArea - Eps )
        return 0;
    // compare delays
    CellDelay = Mio_GateDelayAve( pCell );
    GateDelay = Mio_GateDelayAve( pGate );
    if ( CellDelay > GateDelay + Eps )
        return 1;
    if ( CellDelay < GateDelay - Eps )
        return 0;
    // compare names
    Comp = strcmp( pCell->pName, pGate->pName );
    if ( Comp > 0 )
        return 1;
    if ( Comp < 0 )
        return 0;
    assert( 0 );
    return 0;
}
414
Mio_Gate_t ** Mio_CollectRoots( Mio_Library_t * pLib, int nInputs, float tDelay, int fSkipInv, int * pnGates, int fVerbose )
Alan Mishchenko committed
415 416 417
{
    Mio_Gate_t * pGate;
    Mio_Gate_t ** ppGates;
418
    int i, nGates, iGate, fProfile;
Alan Mishchenko committed
419
    nGates = Mio_LibraryReadGateNum( pLib );
Alan Mishchenko committed
420
    ppGates = ABC_ALLOC( Mio_Gate_t *, nGates );
Alan Mishchenko committed
421
    iGate = 0;
422 423 424 425
    // check if profile is entered
    fProfile = Mio_LibraryHasProfile( pLib );
    if ( fProfile )
        printf( "Mio_CollectRoots(): Using gate profile to select gates for mapping.\n" );
426 427
    // for each functionality, select gate with the smallest area
    // if equal areas, select gate with lexicographically smaller name
Alan Mishchenko committed
428 429 430 431
    Mio_LibraryForEachGate( pLib, pGate )
    {
        if ( pGate->nInputs > nInputs )
            continue;
432 433
        if ( fProfile && Mio_GateReadProfile(pGate) == 0 && pGate->nInputs > 1 )
            continue;
434
        if ( tDelay > 0.0 && pGate->dDelayMax > (double)tDelay )
Alan Mishchenko committed
435
            continue;
436
        if ( pGate->uTruth == 0 || pGate->uTruth == ~(word)0 )
Alan Mishchenko committed
437
            continue;
438
        if ( pGate->uTruth == ABC_CONST(0xAAAAAAAAAAAAAAAA) )
Alan Mishchenko committed
439
            continue;
440
        if ( pGate->uTruth == ~ABC_CONST(0xAAAAAAAAAAAAAAAA) && fSkipInv )
Alan Mishchenko committed
441
            continue;
442 443
        if ( pGate->pTwin ) // skip multi-output gates for now
            continue;
444 445 446 447
        // check if the gate with this functionality already exists
        for ( i = 0; i < iGate; i++ )
            if ( ppGates[i]->uTruth == pGate->uTruth )
            {
448
                if ( Mio_CompareTwoGates(ppGates[i], pGate) )
449 450 451 452 453
                    ppGates[i] = pGate;
                break;
            }
        if ( i < iGate )
            continue;
Alan Mishchenko committed
454 455
        assert( iGate < nGates );
        ppGates[ iGate++ ] = pGate;
456 457 458
        if ( fVerbose )
            printf( "Selected gate %3d:   %-20s  A = %7.2f  D = %7.2f  %3s = %-s\n", 
                iGate+1, pGate->pName, pGate->dArea, pGate->dDelayMax, pGate->pOutName, pGate->pForm );
Alan Mishchenko committed
459
    }
460 461
    // sort by delay
    if ( iGate > 0 ) 
Alan Mishchenko committed
462 463 464 465 466 467 468 469 470 471
    {
        qsort( (void *)ppGates, iGate, sizeof(Mio_Gate_t *), 
                (int (*)(const void *, const void *)) Mio_DelayCompare );
        assert( Mio_DelayCompare( ppGates, ppGates + iGate - 1 ) <= 0 );
    }
    if ( pnGates )
        *pnGates = iGate;
    return ppGates;
}

472 473 474 475 476 477 478 479 480 481 482 483 484

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

  Synopsis    [Collects the set of root gates.]

  Description [Only collects the gates with unique functionality, 
  which have fewer inputs and shorter delay than the given limits.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
485 486 487
static inline int Mio_CompareTwo( Mio_Cell_t * pCell, Mio_Gate_t * pGate )
{
    int Comp;
488
    float Eps = (float)0.0094636;
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
    float CellDelay, GateDelay;
    // compare areas
    if ( pCell->Area > (float)pGate->dArea + Eps )
        return 1;
    if ( pCell->Area < (float)pGate->dArea - Eps )
        return 0;
    // compare delays
    CellDelay = Mio_CellDelayAve( pCell );
    GateDelay = Mio_GateDelayAve( pGate );
    if ( CellDelay > GateDelay + Eps )
        return 1;
    if ( CellDelay < GateDelay - Eps )
        return 0;
    // compare names
    Comp = strcmp( pCell->pName, pGate->pName );
    if ( Comp > 0 )
        return 1;
    if ( Comp < 0 )
        return 0;
    assert( 0 );
    return 0;
}
static inline void Mio_CollectCopy( Mio_Cell_t * pCell, Mio_Gate_t * pGate )
{
    Mio_Pin_t * pPin;  int k;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    pCell->pName   = pGate->pName;
    pCell->uTruth  = pGate->uTruth;
    pCell->Area    = (float)pGate->dArea;
    pCell->nFanins = pGate->nInputs;
    for ( k = 0, pPin = pGate->pPins; pPin; pPin = pPin->pNext, k++ )
        pCell->Delays[k] = (float)(0.5 * pPin->dDelayBlockRise + 0.5 * pPin->dDelayBlockFall);
}

Mio_Cell_t * Mio_CollectRootsNew( Mio_Library_t * pLib, int nInputs, int * pnGates, int fVerbose )
{
    Mio_Gate_t * pGate;
    Mio_Cell_t * ppCells;
    int i, nGates, iCell = 4;
    nGates = Mio_LibraryReadGateNum( pLib );
    ppCells = ABC_CALLOC( Mio_Cell_t, nGates + 4 );
    // for each functionality, select gate with the smallest area
530 531
    // if equal areas, select gate with smaller average pin delay
    // if these are also equal, select lexicographically smaller name
532 533 534 535 536 537 538 539
    Mio_LibraryForEachGate( pLib, pGate )
    {
        if ( pGate->nInputs > nInputs || pGate->pTwin ) // skip large and multi-output
            continue;
        // check if the gate with this functionality already exists
        for ( i = 0; i < iCell; i++ )
            if ( ppCells[i].pName && ppCells[i].uTruth == pGate->uTruth )
            {
540
                if ( Mio_CompareTwo( ppCells + i, pGate ) )
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
                    Mio_CollectCopy( ppCells + i, pGate );
                break;
            }
        if ( i < iCell )
            continue;
        if ( pGate->uTruth == 0 || pGate->uTruth == ~(word)0 )
        {
            int Idx = (int)(pGate->uTruth == ~(word)0);
            assert( pGate->nInputs == 0 );
            Mio_CollectCopy( ppCells + Idx, pGate );
            continue;
        }
        if ( pGate->uTruth == ABC_CONST(0xAAAAAAAAAAAAAAAA) || pGate->uTruth == ~ABC_CONST(0xAAAAAAAAAAAAAAAA) )
        {
            int Idx = 2 + (int)(pGate->uTruth == ~ABC_CONST(0xAAAAAAAAAAAAAAAA));
            assert( pGate->nInputs == 1 );
            Mio_CollectCopy( ppCells + Idx, pGate );
            continue;
        }
        Mio_CollectCopy( ppCells + iCell++, pGate );
    }
562 563 564 565 566 567 568 569
    if ( ppCells[0].pName == NULL )
        { printf( "Error: Cannot find constant 0 gate in the library.\n" ); return NULL; }
    if ( ppCells[1].pName == NULL )
        { printf( "Error: Cannot find constant 1 gate in the library.\n" ); return NULL; }
    if ( ppCells[2].pName == NULL )
        { printf( "Error: Cannot find buffer gate in the library.\n" );     return NULL; }
    if ( ppCells[3].pName == NULL )
        { printf( "Error: Cannot find inverter gate in the library.\n" );   return NULL; }
570 571 572 573
    // sort by delay
    if ( iCell > 1 ) 
    {
        qsort( (void *)(ppCells + 4), iCell - 4, sizeof(Mio_Cell_t), 
574 575
                (int (*)(const void *, const void *)) Mio_AreaCompare );
        assert( Mio_AreaCompare( ppCells + 4, ppCells + iCell - 1 ) <= 0 );
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    }
    // assign IDs
    for ( i = 0; i < iCell; i++ )
        ppCells[i].Id = ppCells[i].pName ? i : -1;

    // report
    if ( fVerbose )
    {
        // count gates
        int * pCounts = ABC_CALLOC( int, nGates + 4 );
        Mio_LibraryForEachGate( pLib, pGate )
        {
            if ( pGate->nInputs > nInputs || pGate->pTwin ) // skip large and multi-output
                continue;
            for ( i = 0; i < iCell; i++ )
                if ( ppCells[i].pName && ppCells[i].uTruth == pGate->uTruth )
                {
                    pCounts[i]++;
                    break;
                }
            assert( i < iCell );
        }
        for ( i = 0; i < iCell; i++ )
        {
            Mio_Cell_t * pCell = ppCells + i;
            printf( "%4d : ", i );
            if ( pCell->pName == NULL )
                printf( "None\n" );
            else
605
                printf( "%-20s   In = %d   N = %3d   A = %12.6f   D = %12.6f\n", 
606
                    pCell->pName, pCell->nFanins, pCounts[i], pCell->Area, Mio_CellDelayAve(pCell) );
607 608 609 610 611 612 613 614 615 616 617 618
        }
        ABC_FREE( pCounts );
    }
    if ( pnGates )
        *pnGates = iCell;
    return ppCells;
}
Mio_Cell_t * Mio_CollectRootsNewDefault( int nInputs, int * pnGates, int fVerbose )
{
    return Mio_CollectRootsNew( (Mio_Library_t *)Abc_FrameReadLibGen(), nInputs, pnGates, fVerbose );
}

Alan Mishchenko committed
619 620
/**Function*************************************************************

621 622 623 624 625 626 627 628 629 630 631 632 633 634
  Synopsis    [Collects the set of root gates.]

  Description [Only collects the gates with unique functionality, 
  which have fewer inputs and shorter delay than the given limits.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Mio_CompareTwo2( Mio_Cell2_t * pCell1, Mio_Cell2_t * pCell2 )
{
    int Comp;
    // compare areas
635
    if ( pCell1->AreaW > pCell2->AreaW )
636
        return 1;
637
    if ( pCell1->AreaW < pCell2->AreaW )
638 639
        return 0;
    // compare delays
640
    if ( pCell1->iDelayAve > pCell2->iDelayAve )
641
        return 1;
642
    if ( pCell1->iDelayAve < pCell2->iDelayAve )
643 644 645 646 647 648 649 650 651 652 653 654 655
        return 0;
    // compare names
    Comp = strcmp( pCell1->pName, pCell2->pName );
    if ( Comp > 0 )
        return 1;
    if ( Comp < 0 )
        return 0;
    assert( 0 );
    return 0;
}
static inline void Mio_CollectCopy2( Mio_Cell2_t * pCell, Mio_Gate_t * pGate )
{
    Mio_Pin_t * pPin;  int k;
656 657 658 659
    pCell->pName     = pGate->pName;
    pCell->vExpr     = pGate->vExpr;
    pCell->uTruth    = pGate->uTruth;
    pCell->AreaF     = pGate->dArea;
660
    pCell->AreaW     = (word)(SCL_NUM * pGate->dArea);
661 662 663
    pCell->nFanins   = pGate->nInputs;
    pCell->pMioGate  = pGate;
    pCell->iDelayAve = 0;
664 665
    for ( k = 0, pPin = pGate->pPins; pPin; pPin = pPin->pNext, k++ )
    {
666
        pCell->iDelays[k] = (int)(SCL_NUM/2 * pPin->dDelayBlockRise + SCL_NUM/2 * pPin->dDelayBlockFall);
667
        pCell->iDelayAve += pCell->iDelays[k];
668 669
    }
    if ( pCell->nFanins )
670
        pCell->iDelayAve /= pCell->nFanins;
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
}

Mio_Cell2_t * Mio_CollectRootsNew2( Mio_Library_t * pLib, int nInputs, int * pnGates, int fVerbose )
{
    Mio_Gate_t * pGate0;
    Mio_Cell2_t * ppCells0, * ppCells, * pCell;
    int i, nGates, iCell0 = 0, iCell = 4;
    // create space for new cells
    nGates = Mio_LibraryReadGateNum( pLib );
    ppCells  = ABC_CALLOC( Mio_Cell2_t, nGates + 4 );
    // copy all gates first
    ppCells0 = ABC_CALLOC( Mio_Cell2_t, nGates );
    Mio_LibraryForEachGate( pLib, pGate0 )
        if ( !(pGate0->nInputs > nInputs || pGate0->pTwin) ) // skip large and multi-output
            Mio_CollectCopy2( ppCells0 + iCell0++, pGate0 );
    assert( iCell0 <= nGates );
    // for each functionality, select gate with the smallest area
    // if equal areas, select gate with smaller average pin delay
    // if these are also equal, select lexicographically smaller name
    for ( pCell = ppCells0; pCell < ppCells0 + iCell0; pCell++ )
    {
        // check if the gate with this functionality already exists
        for ( i = 0; i < iCell; i++ )
            if ( ppCells[i].pName && ppCells[i].uTruth == pCell->uTruth )
            {
                if ( Mio_CompareTwo2( ppCells + i, pCell ) )
                    ppCells[i] = *pCell;
                break;
            }
        if ( i < iCell )
            continue;
        if ( pCell->uTruth == 0 || pCell->uTruth == ~(word)0 )
        {
            int Idx = (int)(pCell->uTruth == ~(word)0);
            assert( pCell->nFanins == 0 );
            ppCells[Idx] = *pCell;
            continue;
        }
        if ( pCell->uTruth == ABC_CONST(0xAAAAAAAAAAAAAAAA) || pCell->uTruth == ~ABC_CONST(0xAAAAAAAAAAAAAAAA) )
        {
            int Idx = 2 + (int)(pCell->uTruth == ~ABC_CONST(0xAAAAAAAAAAAAAAAA));
            assert( pCell->nFanins == 1 );
            ppCells[Idx] = *pCell;
            continue;
        }
        ppCells[iCell++] = *pCell;
    }
    ABC_FREE( ppCells0 );
    if ( ppCells[0].pName == NULL )
        { printf( "Error: Cannot find constant 0 gate in the library.\n" ); return NULL; }
    if ( ppCells[1].pName == NULL )
        { printf( "Error: Cannot find constant 1 gate in the library.\n" ); return NULL; }
    if ( ppCells[2].pName == NULL )
        { printf( "Error: Cannot find buffer gate in the library.\n" );     return NULL; }
    if ( ppCells[3].pName == NULL )
        { printf( "Error: Cannot find inverter gate in the library.\n" );   return NULL; }
    // sort by delay
    if ( iCell > 1 ) 
    {
        qsort( (void *)(ppCells + 4), iCell - 4, sizeof(Mio_Cell2_t), 
                (int (*)(const void *, const void *)) Mio_AreaCompare2 );
        assert( Mio_AreaCompare2( ppCells + 4, ppCells + iCell - 1 ) <= 0 );
    }
    // assign IDs
    for ( i = 0; i < iCell; i++ )
        ppCells[i].Id = ppCells[i].pName ? i : -1;

    // report
    if ( fVerbose )
    {
        // count gates
        int * pCounts = ABC_CALLOC( int, nGates + 4 );
        Mio_LibraryForEachGate( pLib, pGate0 )
        {
            if ( pGate0->nInputs > nInputs || pGate0->pTwin ) // skip large and multi-output
                continue;
            for ( i = 0; i < iCell; i++ )
                if ( ppCells[i].pName && ppCells[i].uTruth == pGate0->uTruth )
                {
                    pCounts[i]++;
                    break;
                }
            assert( i < iCell );
        }
        for ( i = 0; i < iCell; i++ )
        {
            Mio_Cell2_t * pCell = ppCells + i;
            printf( "%4d : ", i );
            if ( pCell->pName == NULL )
                printf( "None\n" );
            else
                printf( "%-20s   In = %d   N = %3d   A = %12.6f   D = %12.6f\n", 
763
                    pCell->pName, pCell->nFanins, pCounts[i], pCell->AreaF, Scl_Int2Flt(pCell->iDelayAve) );
764 765 766 767 768 769 770 771 772 773 774 775 776 777
        }
        ABC_FREE( pCounts );
    }
    if ( pnGates )
        *pnGates = iCell;
    return ppCells;
}
Mio_Cell2_t * Mio_CollectRootsNewDefault2( int nInputs, int * pnGates, int fVerbose )
{
    return Mio_CollectRootsNew2( (Mio_Library_t *)Abc_FrameReadLibGen(), nInputs, pnGates, fVerbose );
}

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

Alan Mishchenko committed
778 779 780 781 782 783 784 785 786
  Synopsis    [Derives the truth table of the gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
787
word Mio_DeriveTruthTable6( Mio_Gate_t * pGate )
Alan Mishchenko committed
788
{
789 790 791 792 793 794 795 796
    static unsigned uTruths6[6][2] = {
        { 0xAAAAAAAA, 0xAAAAAAAA },
        { 0xCCCCCCCC, 0xCCCCCCCC },
        { 0xF0F0F0F0, 0xF0F0F0F0 },
        { 0xFF00FF00, 0xFF00FF00 },
        { 0xFFFF0000, 0xFFFF0000 },
        { 0x00000000, 0xFFFFFFFF }
    };
797 798 799 800
    union {
      unsigned u[2];
      word w;
    } uTruthRes;
801
    assert( pGate->nInputs <= 6 );
802 803
    Mio_DeriveTruthTable( pGate, uTruths6, pGate->nInputs, 6, uTruthRes.u );
    return uTruthRes.w;
Alan Mishchenko committed
804 805
}

806 807
#if 0

Alan Mishchenko committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
/**Function*************************************************************

  Synopsis    [Recursively derives the truth table of the gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_DeriveTruthTable_rec( DdNode * bFunc, unsigned uTruthsIn[][2], unsigned uTruthRes[] )
{
    unsigned uTruthsCof0[2];
    unsigned uTruthsCof1[2];

    // complement the resulting truth table, if the function is complemented
    if ( Cudd_IsComplement(bFunc) )
    {
        Mio_DeriveTruthTable_rec( Cudd_Not(bFunc), uTruthsIn, uTruthRes );
        uTruthRes[0] = ~uTruthRes[0];
        uTruthRes[1] = ~uTruthRes[1];
        return;
    }

    // if the function is constant 1, return the constant 1 truth table
    if ( bFunc->index == CUDD_CONST_INDEX )
    {
        uTruthRes[0] = MIO_FULL;
        uTruthRes[1] = MIO_FULL;
        return;
    }

    // solve the problem for both cofactors
    Mio_DeriveTruthTable_rec( cuddE(bFunc), uTruthsIn, uTruthsCof0 );
    Mio_DeriveTruthTable_rec( cuddT(bFunc), uTruthsIn, uTruthsCof1 );

    // derive the resulting truth table using the input truth tables
    uTruthRes[0] = (uTruthsCof0[0] & ~uTruthsIn[bFunc->index][0]) |
                   (uTruthsCof1[0] &  uTruthsIn[bFunc->index][0]);
    uTruthRes[1] = (uTruthsCof0[1] & ~uTruthsIn[bFunc->index][1]) |
                   (uTruthsCof1[1] &  uTruthsIn[bFunc->index][1]);
}

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

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
  Synopsis    [Derives the truth table of the gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_DeriveTruthTable( Mio_Gate_t * pGate, unsigned uTruthsIn[][2], int nSigns, int nInputs, unsigned uTruthRes[] )
{
    Mio_DeriveTruthTable_rec( pGate->bFunc, uTruthsIn, uTruthRes );
}

#endif

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

  Synopsis    [Derives the truth table of the gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_DeriveTruthTable( Mio_Gate_t * pGate, unsigned uTruthsIn[][2], int nSigns, int nInputs, unsigned uTruthRes[] )
{
    word uRes, uFanins[6];
    int i;
    assert( pGate->nInputs == nSigns );
    for ( i = 0; i < nSigns; i++ )
        uFanins[i] = (((word)uTruthsIn[i][1]) << 32) | (word)uTruthsIn[i][0];
    uRes = Exp_Truth6( nSigns, pGate->vExpr, (word *)uFanins );
    uTruthRes[0] = uRes & 0xFFFFFFFF;
    uTruthRes[1] = uRes >> 32;
}

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

  Synopsis    [Reads the number of variables in the cover.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_SopGetVarNum( char * pSop )
{
    char * pCur;
    for ( pCur = pSop; *pCur != '\n'; pCur++ )
        if ( *pCur == 0 )
            return -1;
    return pCur - pSop - 2;
}

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

Alan Mishchenko committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
  Synopsis    [Derives the truth table of the root of the gate.]

  Description [Given the truth tables of the leaves of the gate,
  this procedure derives the truth table of the root.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_DeriveTruthTable2( Mio_Gate_t * pGate, unsigned uTruthsIn[][2], int nTruths, int nInputs, unsigned uTruthRes[] )
{
    unsigned uSignCube[2];
    int i, nFanins;
    char * pCube;

    // make sure that the number of input truth tables in equal to the number of gate inputs
    assert( pGate->nInputs == nTruths );
    assert( nInputs < 7 );

935
    nFanins = Mio_SopGetVarNum( pGate->pSop );
Alan Mishchenko committed
936 937 938 939 940 941 942
    assert( nFanins == nInputs );

    // clean the resulting truth table
    uTruthRes[0] = 0;
    uTruthRes[1] = 0;
    if ( nInputs < 6 )
    {
943 944
//        Abc_SopForEachCube( pGate->pSop, nFanins, pCube )
        for ( pCube = pGate->pSop; *pCube; pCube += (nFanins) + 3 )
Alan Mishchenko committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
        {
            // add the clause
            uSignCube[0] = MIO_FULL;
            for ( i = 0; i < nFanins; i++ )
            {
                if ( pCube[i] == '0' )
                    uSignCube[0] &= ~uTruthsIn[i][0];
                else if ( pCube[i] == '1' )
                    uSignCube[0] &=  uTruthsIn[i][0];
            }
        }
        if ( nInputs < 5 )
            uTruthRes[0] &= MIO_MASK(1<<nInputs);
    }
    else
    {
        // consider the case when two unsigneds should be used
962 963
//        Abc_SopForEachCube( pGate->pSop, nFanins, pCube )
        for ( pCube = pGate->pSop; *pCube; pCube += (nFanins) + 3 )
Alan Mishchenko committed
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
        {
            uSignCube[0] = MIO_FULL;
            uSignCube[1] = MIO_FULL;
            for ( i = 0; i < nFanins; i++ )
            {
                if ( pCube[i] == '0' )
                {
                    uSignCube[0] &= ~uTruthsIn[i][0];
                    uSignCube[1] &= ~uTruthsIn[i][1];
                }
                else if ( pCube[i] == '1' )
                {
                    uSignCube[0] &=  uTruthsIn[i][0];
                    uSignCube[1] &=  uTruthsIn[i][1];
                }
            }
            uTruthRes[0] |= uSignCube[0];
            uTruthRes[1] |= uSignCube[1];
        }
    }
}

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

  Synopsis    [Derives the area and delay of the root of the gate.]

  Description [Array of the resulting delays should be initialized 
  to the (negative) SUPER_NO_VAR value.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_DeriveGateDelays( Mio_Gate_t * pGate, 
    float ** ptPinDelays, int nPins, int nInputs, float tDelayZero, 
    float * ptDelaysRes, float * ptPinDelayMax )
{
    Mio_Pin_t * pPin;
    float Delay, DelayMax;
    int i, k;
    assert( pGate->nInputs == nPins );
    // set all the delays to the unused delay
    for ( i = 0; i < nInputs; i++ )
        ptDelaysRes[i] = tDelayZero;
    // compute the delays for each input and the max delay at the same time
    DelayMax = 0;
    for ( i = 0; i < nInputs; i++ )
    {
        for ( k = 0, pPin = pGate->pPins; pPin; pPin = pPin->pNext, k++ )
        {
            if ( ptPinDelays[k][i] < 0 )
                continue;
            Delay = ptPinDelays[k][i] + (float)pPin->dDelayBlockMax;
            if ( ptDelaysRes[i] < Delay )
                ptDelaysRes[i] = Delay;
        }
        if ( k != nPins )
        {
            printf ("DEBUG: problem gate is %s\n", Mio_GateReadName( pGate ));
        }
        assert( k == nPins );
        if ( DelayMax < ptDelaysRes[i] )
            DelayMax = ptDelaysRes[i];
    }
    *ptPinDelayMax = DelayMax;
}


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

  Synopsis    [Creates a pseudo-gate.]

  Description [The pseudo-gate is a N-input gate with all info set to 0.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Mio_Gate_t * Mio_GateCreatePseudo( int nInputs )
{
    Mio_Gate_t * pGate;
    Mio_Pin_t * pPin;
    int i;
    // allocate the gate structure
Alan Mishchenko committed
1050
    pGate = ABC_ALLOC( Mio_Gate_t, 1 );
Alan Mishchenko committed
1051 1052 1053 1054 1055
    memset( pGate, 0, sizeof(Mio_Gate_t) );
    pGate->nInputs = nInputs;
    // create pins
    for ( i = 0; i < nInputs; i++ )
    {
Alan Mishchenko committed
1056
        pPin = ABC_ALLOC( Mio_Pin_t, 1 );
Alan Mishchenko committed
1057 1058 1059 1060 1061 1062 1063
        memset( pPin, 0, sizeof(Mio_Pin_t) );
        pPin->pNext = pGate->pPins;
        pGate->pPins = pPin;
    }
    return pGate;
}

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
/**Function*************************************************************

  Synopsis    [Adds constant value to all delay values.]

  Description [The pseudo-gate is a N-input gate with all info set to 0.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1075
void Mio_LibraryShiftDelay( Mio_Library_t * pLib, double Shift )
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
{
    Mio_Gate_t * pGate;
    Mio_Pin_t * pPin;
    Mio_LibraryForEachGate( pLib, pGate )
    {
        pGate->dDelayMax += Shift;
        Mio_GateForEachPin( pGate, pPin )
        {
            pPin->dDelayBlockRise += Shift;
            pPin->dDelayBlockFall += Shift;
            pPin->dDelayBlockMax  += Shift;
        }
    }
}

1091 1092
/**Function*************************************************************

1093
  Synopsis    [Multiply areas/delays by values proportional to fanin count.]
1094 1095 1096 1097 1098 1099 1100 1101

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1102
void Mio_LibraryMultiArea( Mio_Library_t * pLib, double Multi )
1103 1104 1105 1106
{
    Mio_Gate_t * pGate;
    Mio_LibraryForEachGate( pLib, pGate )
    {
1107 1108
        if ( pGate->nInputs < 2 )
            continue;
1109
//        printf( "Before %8.3f  ", pGate->dArea );
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
        pGate->dArea *= pow( pGate->nInputs, Multi );
//        printf( "After %8.3f  Inputs = %d. Factor = %8.3f\n", pGate->dArea, pGate->nInputs, pow( pGate->nInputs, Multi ) );
    }
}
void Mio_LibraryMultiDelay( Mio_Library_t * pLib, double Multi )
{
    Mio_Gate_t * pGate;
    Mio_Pin_t * pPin;
    Mio_LibraryForEachGate( pLib, pGate )
    {
        if ( pGate->nInputs < 2 )
            continue;
//        printf( "Before %8.3f  ", pGate->dDelayMax );
        pGate->dDelayMax *= pow( pGate->nInputs, Multi );
//        printf( "After %8.3f  Inputs = %d. Factor = %8.3f\n", pGate->dDelayMax, pGate->nInputs, pow( pGate->nInputs, Multi ) );
        Mio_GateForEachPin( pGate, pPin )
        {
            pPin->dDelayBlockRise *= pow( pGate->nInputs, Multi );
            pPin->dDelayBlockFall *= pow( pGate->nInputs, Multi );
            pPin->dDelayBlockMax  *= pow( pGate->nInputs, Multi );
        }
1131 1132 1133
    }
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
/**Function*************************************************************

  Synopsis    [Transfers delays from the second to the first.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_LibraryTransferDelays( Mio_Library_t * pLibD, Mio_Library_t * pLibS )
{
    Mio_Gate_t * pGateD, * pGateS;
    Mio_Pin_t * pPinD, * pPinS;
    Mio_LibraryForEachGate( pLibS, pGateS )
    {
        Mio_LibraryForEachGate( pLibD, pGateD )
        {
            if ( pGateD->uTruth != pGateS->uTruth )
                continue;
            pPinS = Mio_GateReadPins( pGateS );
            Mio_GateForEachPin( pGateD, pPinD )
            {
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
                if (pPinS)
                {
                    pPinD->dDelayBlockRise = pPinS->dDelayBlockRise;
                    pPinD->dDelayBlockFall = pPinS->dDelayBlockFall;
                    pPinD->dDelayBlockMax  = pPinS->dDelayBlockMax;
                    pPinS = Mio_PinReadNext(pPinS);
                }
                else
                {
                    pPinD->dDelayBlockRise = 0;
                    pPinD->dDelayBlockFall = 0;
                    pPinD->dDelayBlockMax  = 0;
                }
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 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 1240 1241 1242 1243 1244 1245
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Nf_ManPrepareGate( int nVars, word uTruth, int * pComp, int * pPerm, Vec_Wrd_t * vResult )
{
    int nPerms = Extra_Factorial( nVars );
    int nMints = (1 << nVars);
    word tCur, tTemp1, tTemp2;
    int i, p, c;
    Vec_WrdClear( vResult );
    for ( i = 0; i < 2; i++ )
    {
        tCur = i ? ~uTruth : uTruth;
        tTemp1 = tCur;
        for ( p = 0; p < nPerms; p++ )
        {
            tTemp2 = tCur;
            for ( c = 0; c < nMints; c++ )
            {
                Vec_WrdPush( vResult, tCur );
                tCur = Abc_Tt6Flip( tCur, pComp[c] );
            }
            assert( tTemp2 == tCur );
            tCur = Abc_Tt6SwapAdjacent( tCur, pPerm[p] );
        }
        assert( tTemp1 == tCur );
    }
}
void Nf_ManPreparePrint( int nVars, int * pComp, int * pPerm, char Line[2*720*64][8] )
{
    int nPerms = Extra_Factorial( nVars );
    int nMints = (1 << nVars);
    char * pChar, * pChar2;
    int i, p, c, n = 0;
    for ( i = 0; i < nVars; i++ )
        Line[0][i] = 'A' + nVars - 1 - i;
    Line[0][nVars] = '+';
    Line[0][nVars+1] = 0;
    for ( i = 0; i < 2; i++ )
    {
        Line[n][nVars] = i ? '-' : '+';
        for ( p = 0; p < nPerms; p++ )
        {
            for ( c = 0; c < nMints; c++ )
            {
                strcpy( Line[n+1], Line[n] ); n++;
                pChar = &Line[n][pComp[c]];
                if ( *pChar >= 'A' && *pChar <= 'Z' )
                    *pChar += 'a' - 'A';
                else if ( *pChar >= 'a' && *pChar <= 'z' )
                    *pChar -= 'a' - 'A';
            }
            pChar  = &Line[n][pPerm[p]];
            pChar2 = pChar + 1;
            ABC_SWAP( char, *pChar, *pChar2 );
        }
    }
    assert( n == 2*nPerms*nMints );
    n = 0;
    for ( i = 0; i < 2; i++ )
        for ( p = 0; p < nPerms; p++ )
            for ( c = 0; c < nMints; c++ )
1246
                printf("%8d : %d %3d %2d : %s\n", n, i, p, c, Line[n]), n++;
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
}

void Nf_ManPrepareLibrary( Mio_Library_t * pLib )
{
//    char Lines[2*720*64][8];
//    Nf_ManPreparePrint( 6, pComp, pPerm, Lines );
    int * pComp[7];
    int * pPerm[7];
    Mio_Gate_t ** ppGates;
    Vec_Wrd_t * vResult;
    word * pTruths;
    int * pSizes;
    int nGates, i, nClasses = 0, nTotal;
    abctime clk = Abc_Clock();

    for ( i = 2; i <= 6; i++ )
        pComp[i] = Extra_GreyCodeSchedule( i );
    for ( i = 2; i <= 6; i++ )
        pPerm[i] = Extra_PermSchedule( i );

    // collect truth tables
    ppGates = Mio_CollectRoots( pLib, 6, (float)1.0e+20, 1, &nGates, 0 );
    pSizes  = ABC_CALLOC( int, nGates );
    pTruths = ABC_CALLOC( word, nGates );
    vResult = Vec_WrdAlloc( 2 * 720 * 64 );
    for ( i = 0; i < nGates; i++ )
    {
        pSizes[i] = Mio_GateReadPinNum( ppGates[i] );
        assert( pSizes[i] > 1 && pSizes[i] <= 6 );
        pTruths[i] = Mio_GateReadTruth( ppGates[i] );

        Nf_ManPrepareGate( pSizes[i], pTruths[i], pComp[pSizes[i]], pPerm[pSizes[i]], vResult );
        Vec_WrdUniqify(vResult);
        nClasses += Vec_WrdSize(vResult);
        nTotal = (1 << (pSizes[i]+1)) * Extra_Factorial(pSizes[i]);

        printf( "%6d : ", i );
        printf( "%16s : ", Mio_GateReadName( ppGates[i] ) );
        printf( "%48s : ", Mio_GateReadForm( ppGates[i] ) );
        printf( "Inputs = %2d   ", pSizes[i] );
        printf( "Total = %6d  ", nTotal );
        printf( "Classes = %6d ", Vec_WrdSize(vResult) );
        printf( "Configs = %8.2f ", 1.0*nTotal/Vec_WrdSize(vResult) );
        printf( "%6.2f %%  ", 100.0*Vec_WrdSize(vResult)/nTotal );
        Dau_DsdPrintFromTruth( &pTruths[i], pSizes[i] );
//        printf( "\n" );
    }
    Vec_WrdFree( vResult );
    ABC_FREE( ppGates );
    ABC_FREE( pSizes );
    ABC_FREE( pTruths );

    for ( i = 2; i <= 6; i++ )
        ABC_FREE( pComp[i] );
    for ( i = 2; i <= 6; i++ )
        ABC_FREE( pPerm[i] );

    printf( "Classes = %d.  ", nClasses );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
}

void Nf_ManPrepareLibraryTest2()
{
    Mio_Library_t * pLib = (Mio_Library_t *)Abc_FrameReadLibGen();
    if ( pLib != NULL )
        Nf_ManPrepareLibrary( pLib );
    else
        printf( "Standard cell library is not available.\n" );

}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
/**Function*************************************************************

  Synopsis    [Install library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_LibraryTransferCellIds()
{
    Mio_Gate_t * pGate;
    Mio_Library_t * pLib = (Mio_Library_t *)Abc_FrameReadLibGen();
    SC_Lib * pScl = (SC_Lib *)Abc_FrameReadLibScl();
    int CellId;
    if ( pScl == NULL )
    {
        printf( "SC library cannot be found.\n" );
        return;
    }
    if ( pLib == NULL )
    {
        printf( "Genlib library cannot be found.\n" );
        return;
    }
    Mio_LibraryForEachGate( pLib, pGate )
    {
        if ( Mio_GateReadPinNum(pGate) == 0 )
            continue;
        CellId = Abc_SclCellFind( pScl, Mio_GateReadName(pGate) );
        if ( CellId < 0 )
            printf( "Cannot find cell ID of gate %s.\n", Mio_GateReadName(pGate) );
        else
            Mio_GateSetCell( pGate, CellId );
    }
}

1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_LibraryReadProfile( FILE * pFile, Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate;    
    char pBuffer[1000];
    while ( fgets( pBuffer, 1000, pFile ) != NULL )
    {
        char * pToken = strtok( pBuffer, " \t\n" );
        if ( pToken == NULL )
            continue;
        if ( pToken[0] == '#' )
            continue;
        // read gate
        pGate = Mio_LibraryReadGateByName( pLib, pToken, NULL );
        if ( pGate == NULL )
        {
1383
            printf( "Cannot find gate \"%s\" in library \"%s\".\n", pToken, Mio_LibraryReadName(pLib) );
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
            continue;
        }
        // read profile
        pToken = strtok( NULL, " \t\n" );
        Mio_GateSetProfile( pGate, atoi(pToken) );
    }
}

void Mio_LibraryWriteProfile( FILE * pFile, Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate;    
    Mio_LibraryForEachGate( pLib, pGate )
        if ( Mio_GateReadProfile(pGate) > 0 )
            fprintf( pFile, "%-24s  %6d\n", Mio_GateReadName(pGate), Mio_GateReadProfile(pGate) );
}

1400 1401 1402 1403 1404 1405 1406 1407 1408
int Mio_LibraryHasProfile( Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate;    
    Mio_LibraryForEachGate( pLib, pGate )
        if ( Mio_GateReadProfile(pGate) > 0 )
            return 1;
    return 0;
}

1409

1410 1411 1412
void Mio_LibraryTransferProfile( Mio_Library_t * pLibDst, Mio_Library_t * pLibSrc )
{
    Mio_Gate_t * pGateSrc, * pGateDst;    
1413 1414
    Mio_LibraryForEachGate( pLibDst, pGateDst )
        Mio_GateSetProfile( pGateDst, 0 );
1415 1416 1417
    Mio_LibraryForEachGate( pLibSrc, pGateSrc )
        if ( Mio_GateReadProfile(pGateSrc) > 0 )
        {
1418
            // find gate by name
1419 1420 1421
            pGateDst = Mio_LibraryReadGateByName( pLibDst, Mio_GateReadName(pGateSrc), NULL );
            if ( pGateDst == NULL )
            {
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
                // find gate by function
                Mio_LibraryForEachGate( pLibDst, pGateDst )
                    if ( pGateDst->uTruth == pGateSrc->uTruth )
                        break;
                if ( pGateDst == NULL )
                {
                    printf( "Cannot find gate \"%s\" in library \"%s\".\n", Mio_GateReadName(pGateSrc), Mio_LibraryReadName(pLibDst) );
                    continue;
                }
            }
            Mio_GateAddToProfile( pGateDst, Mio_GateReadProfile(pGateSrc) );
        }
}
void Mio_LibraryTransferProfile2( Mio_Library_t * pLibDst, Mio_Library_t * pLibSrc )
{
    Mio_Gate_t * pGateSrc, * pGateDst;    
    Mio_LibraryForEachGate( pLibDst, pGateDst )
        Mio_GateSetProfile2( pGateDst, 0 );
    Mio_LibraryForEachGate( pLibSrc, pGateSrc )
        if ( Mio_GateReadProfile2(pGateSrc) > 0 )
        {
            // find gate by name
            pGateDst = Mio_LibraryReadGateByName( pLibDst, Mio_GateReadName(pGateSrc), NULL );
            if ( pGateDst == NULL )
            {
                // find gate by function
                Mio_LibraryForEachGate( pLibDst, pGateDst )
                    if ( pGateDst->uTruth == pGateSrc->uTruth )
                        break;
                if ( pGateDst == NULL )
                {
                    printf( "Cannot find gate \"%s\" in library \"%s\".\n", Mio_GateReadName(pGateSrc), Mio_LibraryReadName(pLibDst) );
                    continue;
                }
1456
            }
1457
            Mio_GateAddToProfile2( pGateDst, Mio_GateReadProfile2(pGateSrc) );
1458 1459 1460 1461 1462 1463 1464 1465 1466
        }
}

void Mio_LibraryCleanProfile2( Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate;    
    Mio_LibraryForEachGate( pLib, pGate )
        Mio_GateSetProfile2( pGate, 0 );
}
1467

Alan Mishchenko committed
1468 1469 1470 1471 1472
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1473 1474
ABC_NAMESPACE_IMPL_END