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

  FileName    [ifTime.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapping based on priority cuts.]

  Synopsis    [Computation of delay paramters depending on the library.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 21, 2006.]

  Revision    [$Id: ifTime.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]

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

#include "if.h"
22
#include "bool/kit/kit.h"
23 24 25

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
26 27 28 29 30

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

31 32
#define IF_BIG_CHAR 120

33
static float s_ExtraDel[2][3] = { {1.0, 1.0, (float)0.1}, {1.0, 1.0, (float)0.1} };
34

Alan Mishchenko committed
35 36
static void If_CutSortInputPins( If_Man_t * p, If_Cut_t * pCut, int * pPinPerm, float * pPinDelays );

37 38 39
int s_timeNew;
int s_timeOld;

Alan Mishchenko committed
40 41 42 43 44 45
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

46 47 48 49 50 51 52 53 54 55 56 57 58
  Synopsis    [Inserts the entry while sorting them by delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word If_AndVerifyArray( Vec_Wrd_t * vAnds, int nVars )
{
    Vec_Wrd_t * vTruths;
    If_And_t This;
59
    word Entry, Truth0, Truth1, TruthR = 0;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    int i;
    static word Truth[8] = {
        0xAAAAAAAAAAAAAAAA,
        0xCCCCCCCCCCCCCCCC,
        0xF0F0F0F0F0F0F0F0,
        0xFF00FF00FF00FF00,
        0xFFFF0000FFFF0000,
        0xFFFFFFFF00000000,
        0x0000000000000000,
        0xFFFFFFFFFFFFFFFF
    };
    if ( Vec_WrdSize(vAnds) == 0 )
        return Truth[6];
    if ( Vec_WrdSize(vAnds) == 1 && Vec_WrdEntry(vAnds,0) == 0 )
        return Truth[7];
    vTruths = Vec_WrdAlloc( Vec_WrdSize(vAnds) );
    for ( i = 0; i < nVars; i++ )
        Vec_WrdPush( vTruths, Truth[i] );
    Vec_WrdForEachEntryStart( vAnds, Entry, i, nVars )
    {
        This   = If_WrdToAnd(Entry);
        Truth0 = Vec_WrdEntry( vTruths, This.iFan0 );
        Truth0 = This.fCompl0 ? ~Truth0 : Truth0;
        Truth1 = Vec_WrdEntry( vTruths, This.iFan1 );
        Truth1 = This.fCompl1 ? ~Truth1 : Truth1;
        TruthR = Truth0 & Truth1;
        Vec_WrdPush( vTruths, TruthR );
    }
    Vec_WrdFree( vTruths );
    TruthR = This.fCompl ? ~TruthR : TruthR;
    return TruthR;
}

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

  Synopsis    [Inserts the entry while sorting them by delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_AndInsertSorted( Vec_Wrd_t * vAnds, If_And_t And )
{
    If_And_t This, Prev;
    int i;
    Vec_WrdPush( vAnds, If_AndToWrd(And) );
    for ( i = Vec_WrdSize(vAnds) - 1; i > 0; i-- )
    {
        This = If_WrdToAnd( Vec_WrdEntry(vAnds, i) );
        Prev = If_WrdToAnd( Vec_WrdEntry(vAnds, i-1) );
        if ( This.Delay <= Prev.Delay )
            break;
        Vec_WrdWriteEntry( vAnds, i,   If_AndToWrd(Prev) );
        Vec_WrdWriteEntry( vAnds, i-1, If_AndToWrd(This) );
    }
}

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

  Synopsis    [Decomposes the cube into a bunch of AND gates.]

  Description [Records the result of decomposition into vLits. Returns
  the last AND gate of the decomposition.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
If_And_t If_CutDelaySopCube( Vec_Wrd_t * vCube, Vec_Wrd_t * vAnds, int fOrGate )
{
    If_And_t This, Prev, Next;
    assert( Vec_WrdSize(vCube) > 0 );
    while ( Vec_WrdSize(vCube) > 1 )
    {
        // get last
        This = If_WrdToAnd( Vec_WrdPop(vCube) );
        Prev = If_WrdToAnd( Vec_WrdPop(vCube) );
        // create new
        If_AndClear( &Next );
        Next.iFan0   = Prev.Id;
        Next.fCompl0 = Prev.fCompl ^ fOrGate;
        Next.iFan1   = This.Id;
        Next.fCompl1 = This.fCompl ^ fOrGate;
        Next.Id      = Vec_WrdSize(vAnds);
        Next.fCompl  = fOrGate;
149
        Next.Delay   = 1 + Abc_MaxInt( This.Delay, Prev.Delay );
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        // add new
        If_AndInsertSorted( vCube, Next );
        Vec_WrdPush( vAnds, If_AndToWrd(Next) );
    }
    return If_WrdToAnd( Vec_WrdPop(vCube) );
}



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

  Synopsis    [Returns the well-balanced structure of AIG nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wrd_t * If_CutDelaySopAnds( If_Man_t * p, If_Cut_t * pCut, Vec_Int_t * vCover, int fCompl )
{
    If_Obj_t * pLeaf;
    If_And_t Leaf;
    int i, k, Entry, Literal;
175
    Vec_WrdClear( p->vAnds );
176 177 178
    if ( Vec_IntSize(vCover) == 0 ) // const 0
    {
        assert( fCompl == 0 );
179
        return p->vAnds; 
180 181 182 183
    }
    if ( Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0 ) // const 1
    {
        assert( fCompl == 0 );
184 185
        Vec_WrdPush( p->vAnds, 0 );
        return p->vAnds;
186 187 188 189 190 191
    }
    If_CutForEachLeaf( p, pCut, pLeaf, k )
    {
        If_AndClear( &Leaf );
        Leaf.Id     = k;
        Leaf.Delay  = (int)If_ObjCutBest(pLeaf)->Delay; 
192
        Vec_WrdPush( p->vAnds, If_AndToWrd(Leaf) );
193 194
    }
    // iterate through the cubes
195 196
    Vec_WrdClear( p->vOrGate );
    Vec_WrdClear( p->vAndGate );
197 198
    Vec_IntForEachEntry( vCover, Entry, i )
    { 
199
        Vec_WrdClear( p->vAndGate );
200 201 202 203 204 205 206 207 208
        If_CutForEachLeaf( p, pCut, pLeaf, k )
        {
            Literal = 3 & (Entry >> (k << 1));
            if ( Literal == 1 ) // neg literal
            {
                If_AndClear( &Leaf );
                Leaf.fCompl = 1;
                Leaf.Id     = k;
                Leaf.Delay  = (int)If_ObjCutBest(pLeaf)->Delay; 
209
                If_AndInsertSorted( p->vAndGate, Leaf );
210 211 212 213 214 215
            }
            else if ( Literal == 2 ) // pos literal
            {
                If_AndClear( &Leaf );
                Leaf.Id     = k;
                Leaf.Delay  = (int)If_ObjCutBest(pLeaf)->Delay; 
216
                If_AndInsertSorted( p->vAndGate, Leaf );
217 218 219 220
            }
            else if ( Literal != 0 ) 
                assert( 0 );
        }
221 222
        Leaf = If_CutDelaySopCube( p->vAndGate, p->vAnds, 0 );
        If_AndInsertSorted( p->vOrGate, Leaf );
223
    }
224 225
    Leaf = If_CutDelaySopCube( p->vOrGate, p->vAnds, 1 );
    if ( Vec_WrdSize(p->vAnds) == (int)pCut->nLeaves )
226 227 228 229
    {
//        Extra_PrintBinary( stdout, If_CutTruth(pCut), 32 ); printf( "\n" );
        assert( Leaf.Id < pCut->nLeaves );
        Leaf.iFan0 = Leaf.iFan1 = Leaf.Id;
230 231
        Leaf.Id    = Vec_WrdSize(p->vAnds);
        Vec_WrdPush( p->vAnds, If_AndToWrd(Leaf) );
232 233 234
    }
    if ( fCompl )
    {
235
        Leaf = If_WrdToAnd( Vec_WrdPop(p->vAnds) );
236
        Leaf.fCompl ^= 1;
237
        Vec_WrdPush( p->vAnds, If_AndToWrd(Leaf) );
238
    }
239
    return p->vAnds;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

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

  Synopsis    [Computes balanced AND decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wrd_t * If_CutDelaySopArray( If_Man_t * p, If_Cut_t * pCut )
{
255
    clock_t clk;
256 257
    Vec_Wrd_t * vAnds;
    int RetValue;
258 259
    if ( p->vCover == NULL )
        p->vCover   = Vec_IntAlloc(0);
260
    if ( p->vAnds == NULL )
261
        p->vAnds    = Vec_WrdAlloc(100);
262
    if ( p->vAndGate == NULL )
263
        p->vAndGate = Vec_WrdAlloc(100);
264
    if ( p->vOrGate == NULL )
265 266 267 268
        p->vOrGate  = Vec_WrdAlloc(100);
    RetValue = Kit_TruthIsop( If_CutTruth(pCut), If_CutLeaveNum(pCut), p->vCover, 1 );
    if ( RetValue == -1 )
        return NULL;
269
    assert( RetValue == 0 || RetValue == 1 );
270 271

    clk = clock();
272
    vAnds = If_CutDelaySopAnds( p, pCut, p->vCover, RetValue ^ pCut->fCompl );
273
    s_timeOld += clock() - clk;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
/*
    if ( pCut->nLeaves <= 5 )
    {
        if ( *If_CutTruth(pCut) != (unsigned)If_AndVerifyArray(vAnds, pCut->nLeaves) )
        {
            unsigned Truth0 = *If_CutTruth(pCut);
            unsigned Truth1 = (unsigned)If_AndVerifyArray(vAnds, pCut->nLeaves);

            printf( "\n" );
            Extra_PrintBinary( stdout, &Truth0, 32 ); printf( "\n" );
            Extra_PrintBinary( stdout, &Truth1, 32 ); printf( "\n" );

            printf( "Verification failed for %d vars.\n", pCut->nLeaves );
        }
//        else
//            printf( "Verification passed for %d vars.\n", pCut->nLeaves );
    }
    else if ( pCut->nLeaves == 6 )
    {
        if ( *((word *)If_CutTruth(pCut)) != If_AndVerifyArray(vAnds, pCut->nLeaves) )
            printf( "Verification failed for %d vars.\n", pCut->nLeaves );
//        else
//            printf( "Verification passed for %d vars.\n", pCut->nLeaves );
    }
*/
    return vAnds;
}


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

  Synopsis    [Derives the maximum depth from the leaf to the root.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_CutDelayLeafDepth_rec( Vec_Wrd_t * vAnds, If_And_t And, int iLeaf )
{
    int Depth0, Depth1, Depth;
    if ( (int)And.Id == iLeaf )
        return 0;
    if ( And.iFan0 == And.iFan1 )
320
        return -IF_BIG_CHAR;
321 322
    Depth0 = If_CutDelayLeafDepth_rec( vAnds, If_WrdToAnd(Vec_WrdEntry(vAnds, And.iFan0)), iLeaf );
    Depth1 = If_CutDelayLeafDepth_rec( vAnds, If_WrdToAnd(Vec_WrdEntry(vAnds, And.iFan1)), iLeaf );
323
    Depth  = Abc_MaxInt( Depth0, Depth1 );
324
    Depth  = (Depth == -IF_BIG_CHAR) ? -IF_BIG_CHAR : Depth + 1;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    return Depth;
}

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

  Synopsis    [Derives the maximum depth from the leaf to the root.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_CutDelayLeafDepth( Vec_Wrd_t * vAnds, int iLeaf )
{
    If_And_t Leaf;
    if ( Vec_WrdSize(vAnds) == 0 ) // const 0
343
        return -IF_BIG_CHAR;
344
    if ( Vec_WrdSize(vAnds) == 1 && Vec_WrdEntry(vAnds, 0) == 0 ) // const 1
345
        return -IF_BIG_CHAR;
346 347 348 349 350
    Leaf = If_WrdToAnd(Vec_WrdEntryLast(vAnds));
    if ( Leaf.iFan0 == Leaf.iFan1 )
    {
        if ( (int)Leaf.iFan0 == iLeaf )
            return 0;
351
        return -IF_BIG_CHAR;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    }
    return If_CutDelayLeafDepth_rec( vAnds, Leaf, iLeaf );
}


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

  Synopsis    [Computes the SOP delay using balanced AND decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_CutDelaySopCost( If_Man_t * p, If_Cut_t * pCut )
{
    If_And_t Leaf;
    Vec_Wrd_t * vAnds;
372
    int i, Delay;
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    // mark cut as a user cut
    pCut->fUser = 1;
    vAnds = If_CutDelaySopArray( p, pCut );
    if ( vAnds == NULL )
    {
        assert( 0 );
        return ABC_INFINITY;
    }
    // get the cost
    If_AndClear( &Leaf );
    if ( Vec_WrdSize(vAnds) )
        Leaf = If_WrdToAnd( Vec_WrdEntryLast(vAnds) );
    if ( pCut->nLeaves > 2 && Vec_WrdSize(vAnds) > (int)pCut->nLeaves )
        pCut->Cost = Vec_WrdSize(vAnds) - pCut->nLeaves;
    else
        pCut->Cost = 1;
    // get the permutation
    for ( i = 0; i < (int)pCut->nLeaves; i++ )
391 392 393
    {
        Delay = If_CutDelayLeafDepth( vAnds, i );
        pCut->pPerm[i] = (char)(Delay == -IF_BIG_CHAR ? IF_BIG_CHAR : Delay);
394
//printf( "%d ", pCut->pPerm[i] );
395
    }
396
//printf( " (%d)\n", Leaf.Delay );
397
    // verify the delay
398
//    Delay = If_CutDelay( p, pObj, pCut );
399 400 401 402 403
//    assert( (int)Leaf.Delay == Delay );
    return Leaf.Delay;
}


404 405 406
/**Function*************************************************************

  Synopsis    [Alternative computation of delay.]
407

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word If_CutDelayCountFormula( Vec_Int_t * vNums )
{
    word Count = 0;
    int i, Entry;
    Vec_IntForEachEntry( vNums, Entry, i )
    {
        if ( Entry < 0 )
            continue;
        assert( Entry < 60 );
        Count += ((word)1) << Entry;
    }
    return Count;
}
int If_CutDelayUseFormula( Vec_Int_t * vNums )
{
    int i, k, fChanges = 1;
//    word Count = If_CutDelayCountFormula( vNums );
//    Vec_IntPrint( vNums );
    while ( fChanges )
    {
        fChanges = 0;
        for ( i = Vec_IntSize(vNums) - 1; i > 0; i-- )
            if ( vNums->pArray[i] == vNums->pArray[i-1] )
            {
                vNums->pArray[i-1]++;
                for ( k = i; k < Vec_IntSize(vNums) - 1; k++ )
                    vNums->pArray[k] = vNums->pArray[k+1];
                Vec_IntShrink( vNums, Vec_IntSize(vNums)-1 );
                fChanges = 1;
            }
    }
//    assert( Count == If_CutDelayCountFormula(vNums) );
//    Vec_IntPrint( vNums );
//    printf( "\n" );
    if ( Vec_IntSize(vNums) == 1 )
        return vNums->pArray[0];
    return Vec_IntEntryLast(vNums) + 1;
}
int If_CutDelaySopAnds2( If_Man_t * p, If_Cut_t * pCut, Vec_Int_t * vCover, int fCompl, int * pArea )
{
    Vec_Int_t * vOrGate2  = (Vec_Int_t *)p->vOrGate;
    Vec_Int_t * vAndGate2 = (Vec_Int_t *)p->vAndGate;
    int Arrivals[16];
    If_Obj_t * pLeaf;
    int i, k, Entry, Literal;
    *pArea = 0;
    if ( Vec_IntSize(vCover) == 0 ) // const 0
    {
        assert( fCompl == 0 );
        return 0; 
    }
    if ( Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0 ) // const 1
    {
        assert( fCompl == 0 );
        return 0; 
    }
    If_CutForEachLeaf( p, pCut, pLeaf, k )
        Arrivals[k] = (int)If_ObjCutBest(pLeaf)->Delay;
    // iterate through the cubes
    Vec_IntClear( vOrGate2 );
    Vec_IntForEachEntry( vCover, Entry, i )
    { 
        Vec_IntClear( vAndGate2 );
        for ( k = 0; k < (int)pCut->nLeaves; k++ )
        {
            Literal = 3 & (Entry >> (k << 1));
            if ( Literal == 1 ) // neg literal
                Vec_IntPushOrder( vAndGate2, Arrivals[k] );
            else if ( Literal == 2 ) // pos literal
                Vec_IntPushOrder( vAndGate2, Arrivals[k] );
            else if ( Literal != 0 ) 
                assert( 0 );
        }
        *pArea += Vec_IntSize(vAndGate2) - 1;
        Vec_IntPushOrder( vOrGate2, If_CutDelayUseFormula(vAndGate2) );
    }
    *pArea += Vec_IntSize(vOrGate2) - 1;
    return If_CutDelayUseFormula(vOrGate2);
}
int If_CutDelaySopArray2( If_Man_t * p, If_Cut_t * pCut, int * pArea )
{
496
    clock_t clk;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    int RetValue;
    if ( p->vCover == NULL )
        p->vCover = Vec_IntAlloc(0);
    if ( p->vAndGate == NULL )
        p->vAndGate = Vec_WrdAlloc(100);
    if ( p->vOrGate == NULL )
        p->vOrGate = Vec_WrdAlloc(100);
    RetValue = Kit_TruthIsop( If_CutTruth(pCut), If_CutLeaveNum(pCut), p->vCover, 1 );
    if ( RetValue == -1 )
        return -1;
    assert( RetValue == 0 || RetValue == 1 );

    clk = clock();
    RetValue = If_CutDelaySopAnds2( p, pCut, p->vCover, RetValue ^ pCut->fCompl, pArea );
//    RetValue = If_CutDelaySopAnds2_( p, pCut, p->vCover, RetValue ^ pCut->fCompl, pArea );
    s_timeNew += clock() - clk;
    return RetValue;
}
int If_CutDelaySopCost2( If_Man_t * p, If_Cut_t * pCut )
{
    If_Obj_t * pLeaf;
    int i, DelayMax, Area;
    // mark cut as a user cut
    pCut->fUser = 1;
    DelayMax = If_CutDelaySopArray2( p, pCut, &Area );
    if ( DelayMax == -1 )
    {
        assert( 0 );
        return ABC_INFINITY;
    }
    // get the cost
    if ( pCut->nLeaves > 2 )
        pCut->Cost = Area;
    else
        pCut->Cost = 1;
    // get the permutation
    If_CutForEachLeaf( p, pCut, pLeaf, i )
    {
        assert( DelayMax == 0 || DelayMax >= (int)If_ObjCutBest(pLeaf)->Delay );
        pCut->pPerm[i] = (char)(DelayMax - (int)If_ObjCutBest(pLeaf)->Delay);
//        printf( "%d ", pCut->pPerm[i] );
    }
//    printf( "(%d)     ", DelayMax );
    // verify the delay
//    Delay = If_CutDelay( p, pObj, pCut );
//    assert( (int)Leaf.Delay == Delay );
    return DelayMax;
}
545 546 547 548 549



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

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 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 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  Synopsis    [Computes the SOP delay using balanced AND decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int If_CutMaxCubeSize( Vec_Int_t * vCover, int nVars )
{
    int i, k, Entry, Literal, Count, CountMax = 0;
    Vec_IntForEachEntry( vCover, Entry, i )
    {
        Count = 0;
        for ( k = 0; k < nVars; k++ )
        {
            Literal = (3 & (Entry >> (k << 1)));
            if ( Literal == 1 || Literal == 2 )
                Count++;
        }
        CountMax = Abc_MaxInt( CountMax, Count );
    }
    return CountMax;
}
int If_CutDelaySop( If_Man_t * p, If_Cut_t * pCut )
{
    // delay is calculated using 1+log2(NumFanins)
    static double GateDelays[20] = { 1.00, 1.00, 2.00, 2.58, 3.00, 3.32, 3.58, 3.81, 4.00, 4.17, 4.32, 4.46, 4.58, 4.70, 4.81, 4.91, 5.00, 5.09, 5.17, 5.25 };
    If_Obj_t * pLeaf;
    int Delay, DelayMax;
    int i, nLitMax, RetValue;
    // mark cut as a user cut
    pCut->fUser = 1;
    if ( p->vCover == NULL )
        p->vCover = Vec_IntAlloc(0);
    RetValue = Kit_TruthIsop( If_CutTruth(pCut), If_CutLeaveNum(pCut), p->vCover, 1 );
    if ( RetValue == -1 )
        return ABC_INFINITY;
    assert( RetValue == 0 || RetValue == 1 );
    // mark the output as complemented
//    vAnds = If_CutDelaySopAnds( p, pCut, p->vCover, RetValue ^ pCut->fCompl );
    if ( Vec_IntSize(p->vCover) > p->pPars->nGateSize )
        return ABC_INFINITY;
    // set the area cost
    assert( If_CutLeaveNum(pCut) >= 0 && If_CutLeaveNum(pCut) <= 16 );
    // compute the gate delay
    nLitMax = If_CutMaxCubeSize( p->vCover, If_CutLeaveNum(pCut) );
    if ( Vec_IntSize(p->vCover) < 2 )
    {
        pCut->Cost = Vec_IntSize(p->vCover);
        Delay = (int)(GateDelays[If_CutLeaveNum(pCut)] + 0.5);
        DelayMax = 0;
        If_CutForEachLeaf( p, pCut, pLeaf, i )
            DelayMax = Abc_MaxInt( DelayMax, If_ObjCutBest(pLeaf)->Delay + (pCut->pPerm[i] = (char)Delay) );
    }
    else
    {
        pCut->Cost = Vec_IntSize(p->vCover) + 1;
        Delay = (int)(GateDelays[If_CutLeaveNum(pCut)] + GateDelays[nLitMax] + 0.5);
        DelayMax = 0;
        If_CutForEachLeaf( p, pCut, pLeaf, i )
            DelayMax = Abc_MaxInt( DelayMax, If_ObjCutBest(pLeaf)->Delay + (pCut->pPerm[i] = (char)Delay) );
    }
    return DelayMax;
}

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

Alan Mishchenko committed
619 620 621 622 623 624 625 626 627
  Synopsis    [Computes delay.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
628
float If_CutDelay( If_Man_t * p, If_Obj_t * pObj, If_Cut_t * pCut )
Alan Mishchenko committed
629 630 631 632 633 634
{
    static int pPinPerm[IF_MAX_LUTSIZE];
    static float pPinDelays[IF_MAX_LUTSIZE];
    If_Obj_t * pLeaf;
    float Delay, DelayCur;
    float * pLutDelays;
635
    int i, Shift, Pin2PinDelay, iLeaf;
Alan Mishchenko committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    assert( p->pPars->fSeqMap || pCut->nLeaves > 1 );
    Delay = -IF_FLOAT_LARGE;
    if ( p->pPars->pLutLib )
    {
        assert( !p->pPars->fLiftLeaves );
        pLutDelays = p->pPars->pLutLib->pLutDelays[pCut->nLeaves];
        if ( p->pPars->pLutLib->fVarPinDelays )
        {
            // compute the delay using sorted pins
            If_CutSortInputPins( p, pCut, pPinPerm, pPinDelays );
            for ( i = 0; i < (int)pCut->nLeaves; i++ )
            {
                DelayCur = pPinDelays[pPinPerm[i]] + pLutDelays[i];
                Delay = IF_MAX( Delay, DelayCur );
            }
        }
        else
        {
            If_CutForEachLeaf( p, pCut, pLeaf, i )
            {
656 657 658 659
                if ( p->pDriverCuts && p->pDriverCuts[pObj->Id] && (iLeaf = Vec_IntFind(p->pDriverCuts[pObj->Id], pLeaf->Id)) >= 0 )
                    DelayCur = If_ObjCutBest(pLeaf)->Delay + s_ExtraDel[pObj->fDriver][iLeaf];
                else
                    DelayCur = If_ObjCutBest(pLeaf)->Delay + pLutDelays[0];
Alan Mishchenko committed
660 661 662 663 664 665 666 667 668 669 670
                Delay = IF_MAX( Delay, DelayCur );
            }
        }
    }
    else
    {
        if ( pCut->fUser )
        {
            assert( !p->pPars->fLiftLeaves );
            If_CutForEachLeaf( p, pCut, pLeaf, i )
            {
671 672
                Pin2PinDelay = pCut->pPerm ? (pCut->pPerm[i] == IF_BIG_CHAR ? -IF_BIG_CHAR : pCut->pPerm[i]) : 1;
                DelayCur = If_ObjCutBest(pLeaf)->Delay + (float)Pin2PinDelay;
Alan Mishchenko committed
673 674 675 676 677 678 679 680 681 682
                Delay = IF_MAX( Delay, DelayCur );
            }
        }
        else
        {
            if ( p->pPars->fLiftLeaves )
            {
                If_CutForEachLeafSeq( p, pCut, pLeaf, Shift, i )
                {
                    DelayCur = If_ObjCutBest(pLeaf)->Delay - Shift * p->Period;
683
                    Delay = IF_MAX( Delay, DelayCur + 1.0 );
Alan Mishchenko committed
684 685 686 687 688 689
                }
            }
            else
            {
                If_CutForEachLeaf( p, pCut, pLeaf, i )
                {
690 691 692 693
                    if ( p->pDriverCuts && p->pDriverCuts[pObj->Id] && (iLeaf = Vec_IntFind(p->pDriverCuts[pObj->Id], pLeaf->Id)) >= 0 )
                        DelayCur = If_ObjCutBest(pLeaf)->Delay + ((pObj->fDriver && iLeaf == 2) ? 0.0 : 1.0);
                    else
                        DelayCur = If_ObjCutBest(pLeaf)->Delay + 1.0;
Alan Mishchenko committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
                    Delay = IF_MAX( Delay, DelayCur );
                }
            }
        }
    }
    return Delay;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
713
void If_CutPropagateRequired( If_Man_t * p, If_Obj_t * pObj, If_Cut_t * pCut, float ObjRequired )
Alan Mishchenko committed
714 715 716 717 718 719
{
    static int pPinPerm[IF_MAX_LUTSIZE];
    static float pPinDelays[IF_MAX_LUTSIZE];
    If_Obj_t * pLeaf;
    float * pLutDelays;
    float Required;
720
    int i, Pin2PinDelay, iLeaf;
Alan Mishchenko committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    assert( !p->pPars->fLiftLeaves );
    // compute the pins
    if ( p->pPars->pLutLib )
    {
        pLutDelays = p->pPars->pLutLib->pLutDelays[pCut->nLeaves];
        if ( p->pPars->pLutLib->fVarPinDelays )
        {
            // compute the delay using sorted pins
            If_CutSortInputPins( p, pCut, pPinPerm, pPinDelays );
            for ( i = 0; i < (int)pCut->nLeaves; i++ )
            {
                Required = ObjRequired - pLutDelays[i];
                pLeaf = If_ManObj( p, pCut->pLeaves[pPinPerm[i]] );
                pLeaf->Required = IF_MIN( pLeaf->Required, Required );
            }
        }
        else
        {
739
            Required = ObjRequired;
Alan Mishchenko committed
740
            If_CutForEachLeaf( p, pCut, pLeaf, i )
741 742 743 744 745 746
            {
                if ( p->pDriverCuts && p->pDriverCuts[pObj->Id] && (iLeaf = Vec_IntFind(p->pDriverCuts[pObj->Id], pLeaf->Id)) >= 0 )
                    pLeaf->Required = IF_MIN( pLeaf->Required, Required - s_ExtraDel[pObj->fDriver][iLeaf] );
                else
                    pLeaf->Required = IF_MIN( pLeaf->Required, Required - pLutDelays[0] );
            }
Alan Mishchenko committed
747 748 749 750 751 752 753 754
        }
    }
    else
    {
        if ( pCut->fUser )
        {
            If_CutForEachLeaf( p, pCut, pLeaf, i )
            {
755 756
                Pin2PinDelay = pCut->pPerm ? (pCut->pPerm[i] == IF_BIG_CHAR ? -IF_BIG_CHAR : pCut->pPerm[i]) : 1;
                Required = ObjRequired - (float)Pin2PinDelay;
Alan Mishchenko committed
757 758 759 760 761
                pLeaf->Required = IF_MIN( pLeaf->Required, Required );
            }
        }
        else
        {
762
            Required = ObjRequired;
Alan Mishchenko committed
763
            If_CutForEachLeaf( p, pCut, pLeaf, i )
764 765 766 767 768 769
            {
                if ( p->pDriverCuts && p->pDriverCuts[pObj->Id] && (iLeaf = Vec_IntFind(p->pDriverCuts[pObj->Id], pLeaf->Id)) >= 0 )
                    pLeaf->Required = IF_MIN( pLeaf->Required, Required - (float)((pObj->fDriver && iLeaf == 2) ? 0.0 : 1.0) );
                else
                    pLeaf->Required = IF_MIN( pLeaf->Required, Required - (float)1.0 );
            }
Alan Mishchenko committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
        }
    }
}

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

  Synopsis    [Sorts the pins in the decreasing order of delays.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_CutSortInputPins( If_Man_t * p, If_Cut_t * pCut, int * pPinPerm, float * pPinDelays )
{
    If_Obj_t * pLeaf;
    int i, j, best_i, temp;
    // start the trivial permutation and collect pin delays
    If_CutForEachLeaf( p, pCut, pLeaf, i )
    {
        pPinPerm[i] = i;
        pPinDelays[i] = If_ObjCutBest(pLeaf)->Delay;
    }
    // selection sort the pins in the decreasible order of delays
    // this order will match the increasing order of LUT input pins
    for ( i = 0; i < (int)pCut->nLeaves-1; i++ )
    {
        best_i = i;
        for ( j = i+1; j < (int)pCut->nLeaves; j++ )
            if ( pPinDelays[pPinPerm[j]] > pPinDelays[pPinPerm[best_i]] )
                best_i = j;
        if ( best_i == i )
            continue;
        temp = pPinPerm[i]; 
        pPinPerm[i] = pPinPerm[best_i]; 
        pPinPerm[best_i] = temp;
    }
Alan Mishchenko committed
809
/*
Alan Mishchenko committed
810 811 812 813 814 815 816
    // verify
    assert( pPinPerm[0] < (int)pCut->nLeaves );
    for ( i = 1; i < (int)pCut->nLeaves; i++ )
    {
        assert( pPinPerm[i] < (int)pCut->nLeaves );
        assert( pPinDelays[pPinPerm[i-1]] >= pPinDelays[pPinPerm[i]] );
    }
Alan Mishchenko committed
817
*/
Alan Mishchenko committed
818 819
}

Alan Mishchenko committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
/**Function*************************************************************

  Synopsis    [Sorts the pins in the decreasing order of delays.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void If_CutRotatePins( If_Man_t * p, If_Cut_t * pCut )
{
    If_Obj_t * pLeaf;
    float PinDelays[32];
//    int PinPerm[32];
    int i;
Alan Mishchenko committed
837
//    assert( p->pPars->pLutLib && p->pPars->pLutLib->fVarPinDelays && p->pPars->fTruth ); 
Alan Mishchenko committed
838 839 840 841 842 843
    If_CutForEachLeaf( p, pCut, pLeaf, i )
        PinDelays[i] = If_ObjCutBest(pLeaf)->Delay;
    If_CutTruthPermute( p->puTemp[0], If_CutTruth(pCut), If_CutLeaveNum(pCut), PinDelays, If_CutLeaves(pCut) );
//    If_CutSortInputPins( p, pCut, PinPerm, PinDelays );
}

Alan Mishchenko committed
844 845 846 847 848
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


849 850
ABC_NAMESPACE_IMPL_END