exor.c 34.8 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**CFile****************************************************************

  FileName    [exor.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Exclusive sum-of-product minimization.]

  Synopsis    [Main procedure.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: exor.c,v 1.0 2005/06/20 00:00:00 alanmi Exp $]

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

////////////////////////////////////////////////////////////////////////
///                                                                  ///
///                  Implementation of EXORCISM - 4                  ///
///              An Exclusive Sum-of-Product Minimizer               ///
///               Alan Mishchenko  <alanmi@ee.pdx.edu>               ///
///                                                                  ///
////////////////////////////////////////////////////////////////////////
///                                                                  ///
///                         Main Module                              ///
///                ESOP Minimization Task Coordinator                ///
///                                                                  ///
///          1) interprets command line                              ///  
///          2) calls the approapriate reading procedure             ///
///          3) calls the minimization module                        ///
///                                                                  ///
///  Ver. 1.0. Started - July 18, 2000. Last update - July 20, 2000  ///
///  Ver. 1.1. Started - July 24, 2000. Last update - July 29, 2000  ///
///  Ver. 1.4. Started -  Aug 10, 2000. Last update -  Aug 26, 2000  ///
///  Ver. 1.6. Started -  Sep 11, 2000. Last update -  Sep 15, 2000  ///
///  Ver. 1.7. Started -  Sep 20, 2000. Last update -  Sep 23, 2000  ///
///                                                                  ///
////////////////////////////////////////////////////////////////////////
///   This software was tested with the BDD package "CUDD", v.2.3.0  ///
///                          by Fabio Somenzi                        ///
///                  http://vlsi.colorado.edu/~fabio/                ///
////////////////////////////////////////////////////////////////////////

#include "exor.h"
49
#include "base/abc/abc.h"
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

ABC_NAMESPACE_IMPL_START

///////////////////////////////////////////////////////////////////////
///                      GLOBAL VARIABLES                            ///
////////////////////////////////////////////////////////////////////////

// information about the cube cover
cinfo g_CoverInfo;

extern int s_fDecreaseLiterals;

////////////////////////////////////////////////////////////////////////
///                       EXTERNAL FUNCTIONS                         ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                        FUNCTION main()                           ///
////////////////////////////////////////////////////////////////////////

70 71 72 73 74 75 76 77 78 79 80 81

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

  Synopsis    [Number of negative literals.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
82
/*
83 84 85 86 87 88 89 90 91 92 93 94
static int QCost[16][16] = 
{
    {  1}, // 0
    {  1,   2}, // 1
    {  5,   5,   6}, // 2
    { 14,  14,  16,  18}, // 3
    { 20,  20,  20,  22,  24}, // 4
    { 32,  32,  32,  34,  36,  38}, // 5
    { 44,  44,  44,  44,  46,  48,  50}, // 6
    { 56,  56,  56,  56,  58,  60,  62,  64}, // 7
    { 0 }
};
95
*/
96
int GetQCost( int nVars, int nNegs )
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
    int Extra;
    assert( nVars >= nNegs );
    if ( nVars == 0 )  
        return 1;
    if ( nVars == 1 )  
    {
        if ( nNegs == 0 )  return 1;
        if ( nNegs == 1 )  return 2;
    }
    if ( nVars == 2 )
    {
        if ( nNegs <= 1 )  return 5;
        if ( nNegs == 2 )  return 6;
    }
    if ( nVars == 3 )
    {
        if ( nNegs <= 1 )  return 14;
        if ( nNegs == 2 )  return 16;
        if ( nNegs == 3 )  return 18;
    }
    Extra = nNegs - nVars/2;
    return 20 + 12 * (nVars - 4) + (Extra > 0 ? 2 * Extra : 0);

}
void GetQCostTest()
{
    int i, k, Limit = 10;
    for ( i = 0; i < Limit; i++ )
    {
        for ( k = 0; k <= i; k++ )
            printf( "%4d ", GetQCost(i, k) );
        printf( "\n" );
    }
131 132 133
}
int ComputeQCost( Vec_Int_t * vCube )
{
134 135 136 137
    int i, Entry, nLitsN = 0;
    Vec_IntForEachEntry( vCube, Entry, i )
        nLitsN += Abc_LitIsCompl(Entry);
    return GetQCost( Vec_IntSize(vCube), nLitsN );
138 139 140 141 142 143 144 145 146 147 148 149 150 151
}
int ComputeQCostBits( Cube * p )
{
    extern varvalue GetVar( Cube* pC, int Var );
    int v, nLits = 0, nLitsN = 0;
    for ( v = 0; v < g_CoverInfo.nVarsIn; v++ )
    {
        int Value = GetVar( p, v );
        if ( Value == VAR_NEG )
            nLitsN++;
        else if ( Value == VAR_POS )
            nLits++;
    }
    nLits += nLitsN;
152
    return GetQCost( nLits, nLitsN );
153
}
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
int ToffoliGateCount( int controls, int lines )
{
    switch ( controls )
    {
    case 0u:
    case 1u:
        return 0;
        break;
    case 2u:
        return 1;
        break;
    case 3u:
        return 4;
        break;
    case 4u:
        return ( ( ( lines + 1 ) / 2 ) >= controls ) ? 8 : 10;
        break;
    default:
        return ( ( ( lines + 1 ) / 2 ) >= controls ) ? 4 * ( controls - 2 ) : 8 * ( controls - 3 );
    }
}
int ComputeQCostTcount( Vec_Int_t * vCube )
{
    return 7 * ToffoliGateCount( Vec_IntSize( vCube ), g_CoverInfo.nVarsIn + 1 );
}
int ComputeQCostTcountBits( Cube * p )
{
    extern varvalue GetVar( Cube* pC, int Var );
    int v, nLits = 0;
    for ( v = 0; v < g_CoverInfo.nVarsIn; v++ )
        if ( GetVar( p, v ) != VAR_ABS )
            nLits++;
    return 7 * ToffoliGateCount( nLits, g_CoverInfo.nVarsIn + 1 );

    /* maybe just: 7 * ToffoliGateCount( p->a, g_CoverInfo.nVarsIn + 1 ); */
}

191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int ReduceEsopCover()
{
    ///////////////////////////////////////////////////////////////
    // SIMPLIFICATION
    ////////////////////////////////////////////////////////////////////

    int nIterWithoutImprovement = 0;
    int nIterCount = 0;
    int GainTotal;
    int z;

    do
    {
//START:
        if ( g_CoverInfo.Verbosity == 2 )
            printf( "\nITERATION #%d\n\n", ++nIterCount );
        else if ( g_CoverInfo.Verbosity == 1 )
            printf( "." );

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        if ( nIterWithoutImprovement > (int)(g_CoverInfo.Quality>0) )
        {
            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|0 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|0 );

            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|0 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|0 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;

//      if ( g_CoverInfo.Quality >= 2 && nIterWithoutImprovement == 2 )
//          s_fDecreaseLiterals = 1;
    }
    while ( nIterWithoutImprovement < 1 + g_CoverInfo.Quality );


    // improve the literal count
    s_fDecreaseLiterals = 1;
    for ( z = 0; z < 1; z++ )
    {
        if ( g_CoverInfo.Verbosity == 2 )
            printf( "\nITERATION #%d\n\n", ++nIterCount );
        else if ( g_CoverInfo.Verbosity == 1 )
            printf( "." );

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

//      if ( GainTotal )
//      {
//          nIterWithoutImprovement = 0;
//          goto START;
//      }           
    }


/*  ////////////////////////////////////////////////////////////////////
    // Print statistics
    printf( "\nShallow simplification time is ";
    cout << (float)(clk2 - clk1)/(float)(CLOCKS_PER_SEC) << " sec\n" );
    printf( "Deep simplification time is ";
311
    cout << (float)(Abc_Clock() - clk2)/(float)(CLOCKS_PER_SEC) << " sec\n" );
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    printf( "Cover after iterative simplification = " << s_nCubesInUse << endl;
    printf( "Reduced by initial cube writing      = " << g_CoverInfo.nCubesBefore-nCubesAfterWriting << endl;
    printf( "Reduced by shallow simplification    = " << nCubesAfterWriting-nCubesAfterShallow << endl;
    printf( "Reduced by deep simplification       = " << nCubesAfterWriting-s_nCubesInUse << endl;

//  printf( "\nThe total number of cubes created = " << g_CoverInfo.cIDs << endl;
//  printf( "Total number of places in a queque = " << s_nPosAlloc << endl;
//  printf( "Minimum free places in queque-2 = " << s_nPosMax[0] << endl;
//  printf( "Minimum free places in queque-3 = " << s_nPosMax[1] << endl;
//  printf( "Minimum free places in queque-4 = " << s_nPosMax[2] << endl;
*/  ////////////////////////////////////////////////////////////////////

    // write the number of cubes into cover information 
    assert ( g_CoverInfo.nCubesInUse + g_CoverInfo.nCubesFree == g_CoverInfo.nCubesAlloc );

//  printf( "\nThe output cover is\n" );
//  PrintCoverDebug( cout );

    return 0;
}

//////////////////////////////////////////////////////////////////
// quite a good script
//////////////////////////////////////////////////////////////////
/*
337
    long clk1 = Abc_Clock();
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    int nIterWithoutImprovement = 0;
    do
    {
        PrintQuequeStats();
        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink( DIST2, 0|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 0|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        if ( nIterWithoutImprovement > 2 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 0|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|0 );
        }

        if ( nIterWithoutImprovement > 6 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|0 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
    while ( nIterWithoutImprovement < 12 );

    nCubesAfterShallow = s_nCubesInUse;

*/

/*
    // alu4 - 439
379
    long clk1 = Abc_Clock();
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    int nIterWithoutImprovement = 0;
    do
    {
        PrintQuequeStats();
        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink( DIST2, 1|0|0 );
        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        if ( nIterWithoutImprovement > 2 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 0|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|0 );
        }

        if ( nIterWithoutImprovement > 6 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 0|0|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|0 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
    while ( nIterWithoutImprovement < 12 );
*/

/*
// alu4 - 412 cubes, 700 sec

421
    long clk1 = Abc_Clock();
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
    int nIterWithoutImprovement = 0;
    int nIterCount = 0;
    do
    {
        printf( "\nITERATION #" << ++nIterCount << endl << endl;

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        if ( nIterWithoutImprovement > 3 )
        {
            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
        }

        if ( nIterWithoutImprovement > 7 )
        {
            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|0 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
    while ( nIterWithoutImprovement < 12 );
*/

/*
// pretty good script
// alu4 = 424   in 250 sec

476
    long clk1 = Abc_Clock();
477 478 479 480 481 482 483 484 485 486 487 488 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 529 530
    int nIterWithoutImprovement = 0;
    int nIterCount = 0;
    do
    {
        printf( "\nITERATION #" << ++nIterCount << "   |";
        for ( int k = 0; k < nIterWithoutImprovement; k++ )
            printf( "*";
        for ( ; k < 11; k++ )
            printf( "_";
        printf( "|" << endl << endl;

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        if ( nIterWithoutImprovement > 2 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
        }

        if ( nIterWithoutImprovement > 4 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
    while ( nIterWithoutImprovement < 7 );
*/

/*
alu4 = 435   70 secs

531
    long clk1 = Abc_Clock();
532 533 534 535 536 537 538 539 540 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 568 569
    int nIterWithoutImprovement = 0;
    int nIterCount = 0;

    do
    {
        printf( "\nITERATION #" << ++nIterCount << endl << endl;

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );


        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
    while ( nIterWithoutImprovement < 4 );
*/

/*
  // the best previous
  
570
    long clk1 = Abc_Clock();
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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    int nIterWithoutImprovement = 0;
    int nIterCount = 0;
    int GainTotal;
    do
    {
        if ( g_CoverInfo.Verbosity == 2 )
        printf( "\nITERATION #" << ++nIterCount << endl << endl;
        else if ( g_CoverInfo.Verbosity == 1 )
        cout << '.';

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
        GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );

        if ( nIterWithoutImprovement > 1 )
        {
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|0 );

            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|0 );
            GainTotal += IterativelyApplyExorLink( DIST3, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST2, 1|2|4 );
            GainTotal += IterativelyApplyExorLink( DIST4, 1|2|0 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
//  while ( nIterWithoutImprovement < 20 );
//  while ( nIterWithoutImprovement < 7 );
    while ( nIterWithoutImprovement < 1 + g_CoverInfo.Quality );

*/

/*
// the last tried

635
    long clk1 = Abc_Clock();
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 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
    int nIterWithoutImprovement = 0;
    int nIterCount = 0;
    int GainTotal;
    do
    {
        if ( g_CoverInfo.Verbosity == 2 )
        printf( "\nITERATION #" << ++nIterCount << endl << endl;
        else if ( g_CoverInfo.Verbosity == 1 )
        cout << '.';

        GainTotal  = 0;
        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        GainTotal += IterativelyApplyExorLink2( 1|2|0 );
        GainTotal += IterativelyApplyExorLink3( 1|2|0 );

        if ( nIterWithoutImprovement > (int)(g_CoverInfo.Quality>0) )
        {
            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|0 );
            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|0 );

            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|0 );
            GainTotal += IterativelyApplyExorLink2( 1|2|0 );
            GainTotal += IterativelyApplyExorLink3( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|4 );
            GainTotal += IterativelyApplyExorLink2( 1|2|4 );
            GainTotal += IterativelyApplyExorLink4( 1|2|0 );
        }

        if ( GainTotal )
            nIterWithoutImprovement = 0;
        else
            nIterWithoutImprovement++;
    }
    while ( nIterWithoutImprovement < 1 + g_CoverInfo.Quality );
*/

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void AddCubesToStartingCover( Vec_Wec_t * vEsop )
{
    Vec_Int_t * vCube;
    Cube * pNew;
    int * s_Level2Var;
    int * s_LevelValues;
    int c, i, k, Lit, Out;

    s_Level2Var = ABC_ALLOC( int, g_CoverInfo.nVarsIn );
    s_LevelValues = ABC_ALLOC( int, g_CoverInfo.nVarsIn );

    for ( i = 0; i < g_CoverInfo.nVarsIn; i++ )
        s_Level2Var[i] = i;

    g_CoverInfo.nLiteralsBefore = 0;
717
    g_CoverInfo.QCostBefore = 0;
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
    Vec_WecForEachLevel( vEsop, vCube, c )
    {
        // get the output of this cube
        Out = -Vec_IntPop(vCube) - 1;

        // fill in the cube with blanks
        for ( i = 0; i < g_CoverInfo.nVarsIn; i++ )
            s_LevelValues[i] = VAR_ABS;
        Vec_IntForEachEntry( vCube, Lit, k )
        {
            if ( Abc_LitIsCompl(Lit) )
                s_LevelValues[Abc_Lit2Var(Lit)] = VAR_NEG;
            else
                s_LevelValues[Abc_Lit2Var(Lit)] = VAR_POS;
        }

        // get the new cube
        pNew = GetFreeCube();
        // consider the need to clear the cube
        if ( pNew->pCubeDataIn[0] ) // this is a recycled cube
        {
            for ( i = 0; i < g_CoverInfo.nWordsIn; i++ )
                pNew->pCubeDataIn[i] = 0;
            for ( i = 0; i < g_CoverInfo.nWordsOut; i++ )
                pNew->pCubeDataOut[i] = 0;
        }

        InsertVarsWithoutClearing( pNew, s_Level2Var, g_CoverInfo.nVarsIn, s_LevelValues, Out );
        // set literal counts
        pNew->a = Vec_IntSize(vCube);
        pNew->z = 1;
749
        pNew->q = ComputeQCost(vCube);
750 751 752 753 754 755 756 757 758 759
        // set the ID
        pNew->ID = g_CoverInfo.cIDs++;
        // skip through zero-ID
        if ( g_CoverInfo.cIDs == 256 )
            g_CoverInfo.cIDs = 1;

        // add this cube to storage
        CheckForCloseCubes( pNew, 1 );

        g_CoverInfo.nLiteralsBefore += Vec_IntSize(vCube);
760
        g_CoverInfo.QCostBefore += ComputeQCost(vCube);
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    }
    ABC_FREE( s_Level2Var );
    ABC_FREE( s_LevelValues );

    assert ( g_CoverInfo.nCubesInUse + g_CoverInfo.nCubesFree == g_CoverInfo.nCubesAlloc );
}

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

  Synopsis    [Performs heuristic minimization of ESOPs.]

  Description [Returns 1 on success, 0 on failure.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Exorcism( Vec_Wec_t * vEsop, int nIns, int nOuts, char * pFileNameOut )
{
781
    abctime clk1;
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    int RemainderBits;
    int TotalWords;
    int MemTemp, MemTotal;

    ///////////////////////////////////////////////////////////////////////
    // STEPS of HEURISTIC ESOP MINIMIZATION
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
    // STEP 1: determine the size of the starting cover
    ///////////////////////////////////////////////////////////////////////
    assert( nIns > 0 );
    // inputs
    RemainderBits = (nIns*2)%(sizeof(unsigned)*8);
    TotalWords    = (nIns*2)/(sizeof(unsigned)*8) + (RemainderBits > 0);
    g_CoverInfo.nVarsIn  = nIns;
    g_CoverInfo.nWordsIn = TotalWords;
    // outputs
    RemainderBits = (nOuts)%(sizeof(unsigned)*8);
    TotalWords    = (nOuts)/(sizeof(unsigned)*8) + (RemainderBits > 0);
    g_CoverInfo.nVarsOut  = nOuts;
    g_CoverInfo.nWordsOut = TotalWords;
    g_CoverInfo.cIDs = 1;

    // cubes
806
    clk1 = Abc_Clock();
807 808
//  g_CoverInfo.nCubesBefore = CountTermsInPseudoKroneckerCover( g_Func.dd, nOuts );
    g_CoverInfo.nCubesBefore = Vec_WecSize(vEsop);
809
    g_CoverInfo.TimeStart = Abc_Clock() - clk1;
810 811 812 813 814 815 816

    if ( g_CoverInfo.Verbosity )
    {
    printf( "Starting cover generation time is %.2f sec\n", TICKS_TO_SECONDS(g_CoverInfo.TimeStart) );
    printf( "The number of cubes in the starting cover is %d\n", g_CoverInfo.nCubesBefore );
    }

817
    if ( g_CoverInfo.nCubesBefore > g_CoverInfo.nCubesMax )
818
    {
819
        printf( "\nThe size of the starting cover is more than %d cubes. Quitting...\n", g_CoverInfo.nCubesMax );
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 854 855 856 857 858 859 860 861 862 863 864 865
        return 0;
    }

    ///////////////////////////////////////////////////////////////////////
    // STEP 2: prepare internal data structures
    ///////////////////////////////////////////////////////////////////////
    g_CoverInfo.nCubesAlloc = g_CoverInfo.nCubesBefore + ADDITIONAL_CUBES; 

    // allocate cube cover
    MemTotal = 0;
    MemTemp = AllocateCover( g_CoverInfo.nCubesAlloc, g_CoverInfo.nWordsIn, g_CoverInfo.nWordsOut );
    if ( MemTemp == 0 )
    {
        printf( "Unexpected memory allocation problem. Quitting...\n" );
        return 0;
    }
    else 
        MemTotal += MemTemp;

    // allocate cube sets
    MemTemp = AllocateCubeSets( g_CoverInfo.nVarsIn, g_CoverInfo.nVarsOut );
    if ( MemTemp == 0 )
    {
        printf( "Unexpected memory allocation problem. Quitting...\n" );
        return 0;
    }
    else 
        MemTotal += MemTemp;

    // allocate adjacency queques
    MemTemp = AllocateQueques( g_CoverInfo.nCubesAlloc*g_CoverInfo.nCubesAlloc/CUBE_PAIR_FACTOR );
    if ( MemTemp == 0 )
    {
        printf( "Unexpected memory allocation problem. Quitting...\n" );
        return 0;
    }
    else 
        MemTotal += MemTemp;

    if ( g_CoverInfo.Verbosity )
    printf( "Dynamically allocated memory is %dK\n",  MemTotal/1000 );

    ///////////////////////////////////////////////////////////////////////
    // STEP 3: write the cube cover into the allocated storage
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
866
    clk1 = Abc_Clock();
867 868 869 870 871 872 873 874 875 876
    if ( g_CoverInfo.Verbosity )
    printf( "Generating the starting cover...\n" );
    AddCubesToStartingCover( vEsop );
    ///////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////
    // STEP 4: iteratively improve the cover 
    ///////////////////////////////////////////////////////////////////////
    if ( g_CoverInfo.Verbosity )
    printf( "Performing minimization...\n" );
877
    clk1 = Abc_Clock();
878
    ReduceEsopCover();
879 880
    g_CoverInfo.TimeMin = Abc_Clock() - clk1;
//  g_Func.TimeMin = (float)(Abc_Clock() - clk1)/(float)(CLOCKS_PER_SEC);
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    if ( g_CoverInfo.Verbosity )
    {
    printf( "\nMinimization time is %.2f sec\n", TICKS_TO_SECONDS(g_CoverInfo.TimeMin) );
    printf( "\nThe number of cubes after minimization is %d\n", g_CoverInfo.nCubesInUse );
    }

    ///////////////////////////////////////////////////////////////////////
    // STEP 5: save the cover into file 
    ///////////////////////////////////////////////////////////////////////
    // if option is MULTI_OUTPUT, the output is written into the output file;
    // if option is SINGLE_NODE, the output is added to the input file
    // and written into the output file; in this case, the minimized nodes is
    // also stored in the temporary file "temp.blif" for verification

    // create the file name and write the output
    {
        char Buffer[1000];
        sprintf( Buffer, "%s", pFileNameOut ? pFileNameOut : "temp.esop" );
        WriteResultIntoFile( Buffer );
900 901
        if ( g_CoverInfo.Verbosity )
            printf( "Minimized cover has been written into file <%s>\n", Buffer );
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    }

    ///////////////////////////////////////////////////////////////////////
    // STEP 6: delocate memory
    ///////////////////////////////////////////////////////////////////////
    DelocateCubeSets();
    DelocateCover();
    DelocateQueques();

    // return success
    return 1;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
927
int Abc_ExorcismMain( Vec_Wec_t * vEsop, int nIns, int nOuts, char * pFileNameOut, int Quality, int Verbosity, int nCubesMax, int fUseQCost )
928 929 930 931
{
    memset( &g_CoverInfo, 0, sizeof(cinfo) );
    g_CoverInfo.Quality = Quality;
    g_CoverInfo.Verbosity = Verbosity;
932
    g_CoverInfo.nCubesMax = nCubesMax;
933
    g_CoverInfo.fUseQCost = fUseQCost;
934 935
    if ( fUseQCost )
        s_fDecreaseLiterals = 1;
936
    if ( g_CoverInfo.Verbosity )
937 938 939
    {
        printf( "\nEXORCISM, Ver.4.7: Exclusive Sum-of-Product Minimizer\n" );
        printf( "by Alan Mishchenko, Portland State University, July-September 2000\n\n" );
940
        printf( "Incoming ESOP has %d inputs, %d outputs, and %d cubes.\n", nIns, nOuts, Vec_WecSize(vEsop) );
941
    }
942 943 944 945 946 947 948 949 950
    PrepareBitSetModule();
    if ( Exorcism( vEsop, nIns, nOuts, pFileNameOut ) == 0 )
    {
        printf( "Something went wrong when minimizing the cover\n" );
        return 0;
    }
    return 1;
}

951 952 953 954 955 956 957 958 959 960 961 962 963 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
Vec_Wec_t * Abc_ExorcismNtk2Esop( Abc_Ntk_t * pNtk )
{
    Vec_Wec_t * vEsop = NULL;
    Abc_Obj_t * pNode, * pFanin, * pDriver;
    char * pCube;
    int nIns, nOuts, nProducts, nFanins, i, k;

    nIns = Abc_NtkCiNum( pNtk );
    nOuts = Abc_NtkCoNum( pNtk );

    nProducts = 0;
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pNode) );
        if ( !Abc_ObjIsNode(pDriver) )
        {
            nProducts++;
            continue;
        }
        if ( Abc_NodeIsConst(pDriver) )
        {
            if ( Abc_NodeIsConst1(pDriver) )
                nProducts++;
            continue;
        }
        nProducts += Abc_SopGetCubeNum((char *)pDriver->pData);
    }

    Abc_NtkForEachCi( pNtk, pNode, i )
        pNode->pCopy = (Abc_Obj_t *)(ABC_PTRUINT_T)i;

    vEsop = Vec_WecAlloc( nProducts+1 );

    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pDriver = Abc_ObjFanin0Ntk( Abc_ObjFanin0(pNode) );
        if ( Abc_NodeIsConst(pDriver) ) continue;

        nFanins = Abc_ObjFaninNum(pDriver);
        Abc_SopForEachCube( (char *)pDriver->pData, nFanins, pCube )
        {
            Vec_Int_t *vCubeIn = Vec_WecPushLevel( vEsop );
            Vec_IntGrow( vCubeIn, nIns+2 );

            Abc_ObjForEachFanin( pDriver, pFanin, k )
            {
                pFanin = Abc_ObjFanin0Ntk(pFanin);
                assert( (int)(ABC_PTRUINT_T)pFanin->pCopy < nIns );
                if ( pCube[k] == '0' )
                {
                    Vec_IntPush( vCubeIn, 2*k + 1 );
                }
                else if ( pCube[k] == '1' )
                {
                    Vec_IntPush( vCubeIn, 2*k );
                }
            }
1008
            Vec_IntPush( vCubeIn, -( i + 1 ) );
1009 1010 1011 1012 1013
        }
    }

    return vEsop;
}
1014 1015 1016 1017 1018 1019 1020 1021 1022


///////////////////////////////////////////////////////////////////
////////////              End of File             /////////////////
///////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END