utilCex.c 17.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**CFile****************************************************************

  FileName    [utilCex.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Handling counter-examples.]

  Synopsis    [Handling counter-examples.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - Feburary 13, 2011.]

  Revision    [$Id: utilCex.c,v 1.00 2011/02/11 00:00:00 alanmi Exp $]

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

26
#include "misc/vec/vec.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include "utilCex.h"

ABC_NAMESPACE_IMPL_START

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

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////


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

  Synopsis    [Allocates a counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexAlloc( int nRegs, int nRealPis, int nFrames )
{
    Abc_Cex_t * pCex;
    int nWords = Abc_BitWordNum( nRegs + nRealPis * nFrames );
    pCex = (Abc_Cex_t *)ABC_ALLOC( char, sizeof(Abc_Cex_t) + sizeof(unsigned) * nWords );
    memset( pCex, 0, sizeof(Abc_Cex_t) + sizeof(unsigned) * nWords );
    pCex->nRegs  = nRegs;
    pCex->nPis   = nRealPis;
    pCex->nBits  = nRegs + nRealPis * nFrames;
    return pCex;
}
62 63 64 65 66 67 68 69 70 71 72
Abc_Cex_t * Abc_CexAllocFull( int nRegs, int nRealPis, int nFrames )
{
    Abc_Cex_t * pCex;
    int nWords = Abc_BitWordNum( nRegs + nRealPis * nFrames );
    pCex = (Abc_Cex_t *)ABC_ALLOC( char, sizeof(Abc_Cex_t) + sizeof(unsigned) * nWords );
    memset( pCex, 0xFF, sizeof(Abc_Cex_t) + sizeof(unsigned) * nWords );
    pCex->nRegs  = nRegs;
    pCex->nPis   = nRealPis;
    pCex->nBits  = nRegs + nRealPis * nFrames;
    return pCex;
}
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

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

  Synopsis    [Make the trivial counter-example for the trivially asserted output.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexMakeTriv( int nRegs, int nTruePis, int nTruePos, int iFrameOut )
{
    Abc_Cex_t * pCex;
    int iPo, iFrame;
    assert( nRegs > 0 );
    iPo    = iFrameOut % nTruePos;
    iFrame = iFrameOut / nTruePos;
    // allocate the counter example
    pCex = Abc_CexAlloc( nRegs, nTruePis, iFrame + 1 );
    pCex->iPo    = iPo;
    pCex->iFrame = iFrame;
    return pCex;
}

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

  Synopsis    [Derives the counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexCreate( int nRegs, int nPis, int * pArray, int iFrame, int iPo, int fSkipRegs )
{
    Abc_Cex_t * pCex;
    int i;
    pCex = Abc_CexAlloc( nRegs, nPis, iFrame+1 );
    pCex->iPo    = iPo;
    pCex->iFrame = iFrame;
    if ( pArray == NULL )
        return pCex;
    if ( fSkipRegs )
    {
        for ( i = nRegs; i < pCex->nBits; i++ )
            if ( pArray[i-nRegs] )
                Abc_InfoSetBit( pCex->pData, i );
    }
    else
    {
        for ( i = 0; i < pCex->nBits; i++ )
            if ( pArray[i] )
                Abc_InfoSetBit( pCex->pData, i );
    }
    return pCex;
}

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

  Synopsis    [Make the trivial counter-example for the trivially asserted output.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexDup( Abc_Cex_t * p, int nRegsNew )
{
    Abc_Cex_t * pCex;
    int i;
149 150
    if ( p == (Abc_Cex_t *)(ABC_PTRINT_T)1 )
        return p;
151 152
    if ( nRegsNew == -1 )
        nRegsNew = p->nRegs;
153 154 155 156 157 158 159 160 161 162 163
    pCex = Abc_CexAlloc( nRegsNew, p->nPis, p->iFrame+1 );
    pCex->iPo    = p->iPo;
    pCex->iFrame = p->iFrame;
    for ( i = p->nRegs; i < p->nBits; i++ )
        if ( Abc_InfoHasBit(p->pData, i) )
            Abc_InfoSetBit( pCex->pData, pCex->nRegs + i - p->nRegs );
    return pCex;
}

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

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  Synopsis    [Derives CEX from comb model.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexDeriveFromCombModel( int * pModel, int nPis, int nRegs, int iPo )
{
    Abc_Cex_t * pCex;
    int i;
    pCex = Abc_CexAlloc( nRegs, nPis, 1 );
    pCex->iPo = iPo;
    pCex->iFrame = 0;
    for ( i = 0; i < nPis; i++ )
        if ( pModel[i] )
            pCex->pData[i>>5] |= (1<<(i & 31));     
    return pCex;
}

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

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  Synopsis    [Derives CEX from comb model.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexMerge( Abc_Cex_t * pCex, Abc_Cex_t * pPart, int iFrBeg, int iFrEnd )
{
    Abc_Cex_t * pNew;
    int nFramesGain;
    int i, f, iBit;

    if ( iFrBeg < 0 )
        { printf( "Starting frame is less than 0.\n" ); return NULL; }
    if ( iFrEnd < 0 )
        { printf( "Stopping frame is less than 0.\n" ); return NULL; }
    if ( iFrBeg > pCex->iFrame )
        { printf( "Starting frame is more than the last frame of CEX (%d).\n", pCex->iFrame ); return NULL; }
    if ( iFrEnd > pCex->iFrame )
        { printf( "Stopping frame is more than the last frame of CEX (%d).\n", pCex->iFrame ); return NULL; }
211
    if ( iFrBeg > iFrEnd )
212 213 214
        { printf( "Starting frame (%d) should be less than stopping frame (%d).\n", iFrBeg, iFrEnd ); return NULL; }
    assert( iFrBeg >= 0 && iFrBeg <= pCex->iFrame );
    assert( iFrEnd >= 0 && iFrEnd <= pCex->iFrame );
215
    assert( iFrBeg <= iFrEnd );
216 217

    assert( pCex->nPis == pPart->nPis );
218
    assert( iFrEnd - iFrBeg + pPart->iPo >= pPart->iFrame );
219

220
    nFramesGain = iFrEnd - iFrBeg + pPart->iPo - pPart->iFrame;
221 222 223 224 225 226
    pNew = Abc_CexAlloc( pCex->nRegs, pCex->nPis, pCex->iFrame + 1 - nFramesGain );
    pNew->iPo    = pCex->iPo;
    pNew->iFrame = pCex->iFrame - nFramesGain;

    for ( iBit = 0; iBit < pCex->nRegs; iBit++ )
        if ( Abc_InfoHasBit(pCex->pData, iBit) )
227
            Abc_InfoSetBit( pNew->pData, iBit );
228
    for ( f = 0; f < iFrBeg; f++ )
229
        for ( i = 0; i < pCex->nPis; i++, iBit++ )
230
            if ( Abc_InfoHasBit(pCex->pData, pCex->nRegs + pCex->nPis * f + i) )
231
                Abc_InfoSetBit( pNew->pData, iBit );
232
    for ( f = 0; f < pPart->iFrame; f++ )
233
        for ( i = 0; i < pCex->nPis; i++, iBit++ )
234
            if ( Abc_InfoHasBit(pPart->pData, pPart->nRegs + pCex->nPis * f + i) )
235
                Abc_InfoSetBit( pNew->pData, iBit );
236
    for ( f = iFrEnd; f <= pCex->iFrame; f++ )
237 238 239
        for ( i = 0; i < pCex->nPis; i++, iBit++ )
            if ( Abc_InfoHasBit(pCex->pData, pCex->nRegs + pCex->nPis * f + i) )
                Abc_InfoSetBit( pNew->pData, iBit );
240
    assert( iBit == pNew->nBits );
241

242 243 244 245 246
    return pNew;
}

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

247 248 249 250 251 252 253 254 255
  Synopsis    [Prints out the counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
256 257 258 259 260 261 262 263
void Abc_CexPrintStats( Abc_Cex_t * p )
{
    int k, Counter = 0;
    if ( p == NULL )
    {
        printf( "The counter example is NULL.\n" );
        return;
    }
264 265 266 267 268
    if ( p == (Abc_Cex_t *)(ABC_PTRINT_T)1 )
    {
        printf( "The counter example is present but not available (pointer has value \"1\").\n" );
        return;
    }
269 270
    for ( k = 0; k < p->nBits; k++ )
        Counter += Abc_InfoHasBit(p->pData, k);
271 272 273
    printf( "CEX: Po =%4d  Frame =%4d  FF = %d  PI = %d  Bit =%8d  1s =%8d (%5.2f %%)\n", 
        p->iPo, p->iFrame, p->nRegs, p->nPis, p->nBits, 
        Counter, 100.0 * Counter / (p->nBits - p->nRegs) );
274
}
275
void Abc_CexPrintStatsInputs( Abc_Cex_t * p, int nRealPis )
276
{
277
    int k, Counter = 0, CounterPi = 0, CounterPpi = 0;
278 279 280 281 282
    if ( p == NULL )
    {
        printf( "The counter example is NULL.\n" );
        return;
    }
283 284 285 286 287
    if ( p == (Abc_Cex_t *)(ABC_PTRINT_T)1 )
    {
        printf( "The counter example is present but not available (pointer has value \"1\").\n" );
        return;
    }
288
    assert( nRealPis <= p->nPis );
289 290 291
    for ( k = 0; k < p->nBits; k++ )
    {
        Counter += Abc_InfoHasBit(p->pData, k);
292 293 294 295 296 297
        if ( nRealPis == p->nPis )
            continue;
        if ( (k - p->nRegs) % p->nPis < nRealPis )
            CounterPi += Abc_InfoHasBit(p->pData, k);
        else
            CounterPpi += Abc_InfoHasBit(p->pData, k);
298
    }
299
    printf( "CEX: Po =%4d  Fr =%4d  FF = %d  PI = %d  Bit =%7d  1 =%8d (%5.2f %%)", 
300
        p->iPo, p->iFrame, p->nRegs, p->nPis, p->nBits, 
301 302 303 304 305 306 307 308
        Counter,  100.0 * Counter    / ((p->iFrame + 1) * p->nPis ) );
    if ( nRealPis < p->nPis )
    {
        printf( " 1pi =%8d (%5.2f %%) 1ppi =%8d (%5.2f %%)", 
            CounterPi,  100.0 * CounterPi  / ((p->iFrame + 1) * nRealPis ), 
            CounterPpi, 100.0 * CounterPpi / ((p->iFrame + 1) * (p->nPis - nRealPis)) );
    }
    printf( "\n" );
309
}
310 311 312 313 314 315 316 317 318 319 320 321

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

  Synopsis    [Prints out the counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
322 323 324
void Abc_CexPrint( Abc_Cex_t * p )
{
    int i, f, k;
325 326 327 328 329
    if ( p == NULL )
    {
        printf( "The counter example is NULL.\n" );
        return;
    }
330 331 332 333 334
    if ( p == (Abc_Cex_t *)(ABC_PTRINT_T)1 )
    {
        printf( "The counter example is present but not available (pointer has value \"1\").\n" );
        return;
    }
335
    Abc_CexPrintStats( p );
336 337 338 339 340 341
    printf( "State    : " );
    for ( k = 0; k < p->nRegs; k++ )
        printf( "%d", Abc_InfoHasBit(p->pData, k) );
    printf( "\n" );
    for ( f = 0; f <= p->iFrame; f++ )
    {
342
        printf( "Frame %3d : ", f );
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        for ( i = 0; i < p->nPis; i++ )
            printf( "%d", Abc_InfoHasBit(p->pData, k++) );
        printf( "\n" );
    }
    assert( k == p->nBits );
}

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

  Synopsis    [Frees the counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
361 362 363 364
void Abc_CexFreeP( Abc_Cex_t ** p )
{
    if ( *p == NULL )
        return;
365 366 367 368
    if ( *p == (Abc_Cex_t *)(ABC_PTRINT_T)1 )
        *p = NULL;
    else
        ABC_FREE( *p );
369 370 371 372 373 374 375 376 377 378 379 380 381
}

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

  Synopsis    [Frees the counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
382 383
void Abc_CexFree( Abc_Cex_t * p )
{
384 385
    if ( p == (Abc_Cex_t *)(ABC_PTRINT_T)1 )
        return;
386 387 388
    ABC_FREE( p );
}

389

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
/**Function*************************************************************

  Synopsis    [Transform CEX after phase abstraction with nFrames frames.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexTransformPhase( Abc_Cex_t * p, int nPisOld, int nPosOld, int nRegsOld )
{
    Abc_Cex_t * pCex;
    int nFrames = p->nPis / nPisOld;
    int nPosNew = nPosOld * nFrames;
    assert( p->nPis % nPisOld == 0 );
    assert( p->iPo < nPosNew );
    pCex = Abc_CexDup( p, nRegsOld );
    pCex->nPis   = nPisOld;
410 411 412
    pCex->iPo    = -1;
    pCex->iFrame = (p->iFrame + 1) * nFrames - 1;
    pCex->nBits  = p->nBits;
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
    return pCex;
}

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

  Synopsis    [Transform CEX after phase temporal decomposition with nFrames frames.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexTransformTempor( Abc_Cex_t * p, int nPisOld, int nPosOld, int nRegsOld )
{
    Abc_Cex_t * pCex;
    int i, k, iBit = nRegsOld, nFrames = p->nPis / nPisOld - 1;
    assert( p->iFrame > 0 ); // otherwise tempor did not properly check for failures in the prefix
    assert( p->nPis % nPisOld == 0 );
    pCex = Abc_CexAlloc( nRegsOld, nPisOld, nFrames + p->iFrame + 1 );
    pCex->iPo    = p->iPo;
    pCex->iFrame = nFrames + p->iFrame;
    for ( i = 0; i < nFrames; i++ )
        for ( k = 0; k < nPisOld; k++, iBit++ )
            if ( Abc_InfoHasBit(p->pData, p->nRegs + (i+1)*nPisOld + k) )
                Abc_InfoSetBit( pCex->pData, iBit );
    for ( i = 0; i <= p->iFrame; i++ )
        for ( k = 0; k < nPisOld; k++, iBit++ )
            if ( Abc_InfoHasBit(p->pData, p->nRegs + i*p->nPis + k) )
                Abc_InfoSetBit( pCex->pData, iBit );
    assert( iBit == pCex->nBits );
    return pCex;
}

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

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
  Synopsis    [Transform CEX after "logic; undc; st; zero".]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexTransformUndc( Abc_Cex_t * p, char * pInit )
{
    Abc_Cex_t * pCex;
    int nFlops = strlen(pInit);
    int i, f, iBit, iAddPi = 0, nAddPis = 0;
    // count how many flops got a new PI
    for ( i = 0; i < nFlops; i++ )
466
        nAddPis += (int)(pInit[i] == 'x' || pInit[i] == 'X');
467 468 469 470 471 472
    // create new CEX
    pCex = Abc_CexAlloc( nFlops, p->nPis - nAddPis, p->iFrame + 1 );
    pCex->iPo    = p->iPo;
    pCex->iFrame = p->iFrame;
    for ( iBit = 0; iBit < nFlops; iBit++ )
    {
473
        if ( pInit[iBit] == '1' || ((pInit[iBit] == 'x' || pInit[iBit] == 'X') && Abc_InfoHasBit(p->pData, p->nRegs + p->nPis - nAddPis + iAddPi)) )
474
            Abc_InfoSetBit( pCex->pData, iBit );
475
        iAddPi += (int)(pInit[iBit] == 'x' || pInit[iBit] == 'X');
476 477 478 479 480 481 482 483 484 485 486 487 488
    }
    assert( iAddPi == nAddPis );
    // add timeframes
    for ( f = 0; f <= p->iFrame; f++ )
        for ( i = 0; i < pCex->nPis; i++, iBit++ )
            if ( Abc_InfoHasBit(p->pData, p->nRegs + p->nPis * f + i) )
                Abc_InfoSetBit( pCex->pData, iBit );
    assert( iBit == pCex->nBits );
    return pCex;
}

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

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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  Synopsis    [Derives permuted CEX using permutation of its inputs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexPermute( Abc_Cex_t * p, Vec_Int_t * vMapOld2New )
{
    Abc_Cex_t * pCex;
    int i, iNew;
    assert( Vec_IntSize(vMapOld2New) == p->nPis );
    pCex = Abc_CexAlloc( p->nRegs, p->nPis, p->iFrame+1 );
    pCex->iPo    = p->iPo;
    pCex->iFrame = p->iFrame;
    for ( i = p->nRegs; i < p->nBits; i++ )
        if ( Abc_InfoHasBit(p->pData, i) )
        {
            iNew = p->nRegs + p->nPis * ((i - p->nRegs) / p->nPis) + Vec_IntEntry( vMapOld2New, (i - p->nRegs) % p->nPis );
            Abc_InfoSetBit( pCex->pData, iNew );
        }
    return pCex;
}

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

  Synopsis    [Derives permuted CEX using two canonical permutations.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Cex_t * Abc_CexPermuteTwo( Abc_Cex_t * p, Vec_Int_t * vPermOld, Vec_Int_t * vPermNew )
{
    Abc_Cex_t * pCex;
529 530
    Vec_Int_t * vPerm;
    int i, eOld, eNew;
531 532
    assert( Vec_IntSize(vPermOld) == p->nPis );
    assert( Vec_IntSize(vPermNew) == p->nPis );
533 534 535
    vPerm = Vec_IntStartFull( p->nPis );
    Vec_IntForEachEntryTwo( vPermOld, vPermNew, eOld, eNew, i )
        Vec_IntWriteEntry( vPerm, eOld, eNew );
536 537 538 539 540
    pCex = Abc_CexPermute( p, vPerm );
    Vec_IntFree( vPerm );
    return pCex;
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
/**Function*************************************************************

  Synopsis    [Count the number of 1s in the CEX.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_CexOnes32( unsigned i )
{
    i = i - ((i >> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
    i = ((i + (i >> 4)) & 0x0F0F0F0F);
    return (i*(0x01010101))>>24;
}
int Abc_CexCountOnes( Abc_Cex_t * p )
{
    int nWords = Abc_BitWordNum( p->nBits );
    int i, Count = 0;
    for ( i = 0; i < nWords; i++ )
        Count += Abc_CexOnes32( p->pData[i] );
    return Count;
}

568 569 570 571 572 573 574
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END