ifDec16.c 76.6 KB
Newer Older
1 2
/**CFile****************************************************************

3
  FileName    [ifDec16.c]
4 5 6 7 8 9 10 11 12 13 14 15 16

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapping based on priority cuts.]

  Synopsis    [Fast checking procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

17
  Revision    [$Id: ifDec16.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]
18 19 20 21

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

#include "if.h"
22
#include "bool/kit/kit.h"
Alan Mishchenko committed
23
#include "misc/vec/vecMem.h"
24 25 26 27 28 29 30

ABC_NAMESPACE_IMPL_START

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

31 32
#define CLU_VAR_MAX  16
#define CLU_WRD_MAX  (1 << ((CLU_VAR_MAX)-6))
33
#define CLU_MEM_MAX  1000  // 1 GB
34
#define CLU_UNUSED   0xff
35

36
// decomposition
37 38
typedef struct If_Grp_t_ If_Grp_t;
struct If_Grp_t_
39
{
40 41 42 43 44 45 46 47 48 49
    char       nVars;
    char       nMyu;
    char       pVars[CLU_VAR_MAX];
};

// hash table entry
typedef struct If_Hte_t_ If_Hte_t;
struct If_Hte_t_
{
    If_Hte_t * pNext;
50 51
    unsigned   Group;
    unsigned   Counter;
52
    word       pTruth[1];
53 54
};

55 56
// variable swapping code
static word PMasks[5][3] = {
57 58 59 60 61
    { ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) },
    { ABC_CONST(0xC3C3C3C3C3C3C3C3), ABC_CONST(0x0C0C0C0C0C0C0C0C), ABC_CONST(0x3030303030303030) },
    { ABC_CONST(0xF00FF00FF00FF00F), ABC_CONST(0x00F000F000F000F0), ABC_CONST(0x0F000F000F000F00) },
    { ABC_CONST(0xFF0000FFFF0000FF), ABC_CONST(0x0000FF000000FF00), ABC_CONST(0x00FF000000FF0000) },
    { ABC_CONST(0xFFFF00000000FFFF), ABC_CONST(0x00000000FFFF0000), ABC_CONST(0x0000FFFF00000000) }
62 63 64
};
// elementary truth tables
static word Truth6[6] = {
65 66 67 68 69 70
    ABC_CONST(0xAAAAAAAAAAAAAAAA),
    ABC_CONST(0xCCCCCCCCCCCCCCCC),
    ABC_CONST(0xF0F0F0F0F0F0F0F0),
    ABC_CONST(0xFF00FF00FF00FF00),
    ABC_CONST(0xFFFF0000FFFF0000),
    ABC_CONST(0xFFFFFFFF00000000)
71
};
72
static word TruthAll[CLU_VAR_MAX][CLU_WRD_MAX] = {{0}};
73 74 75 76

extern void Kit_DsdPrintFromTruth( unsigned * pTruth, int nVars );
extern void Extra_PrintBinary( FILE * pFile, unsigned Sign[], int nBits );

77
extern int If_CluSupportSize( word * t, int nVars );
78

Alan Mishchenko committed
79 80 81
int s_Count2 = 0;
int s_Count3 = 0;

82 83 84 85
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static inline unsigned If_CluGrp2Uns( If_Grp_t * pG )
{
    char * pChar = (char *)pG;
    unsigned Res = 0;
    int i;
    for ( i = 0; i < 8; i++ )
        Res |= ((pChar[i] & 15) << (i << 2));
    return Res;
}

static inline void If_CluUns2Grp( unsigned Group, If_Grp_t * pG )
{
    char * pChar = (char *)pG;
    int i;
    for ( i = 0; i < 8; i++ )
        pChar[i] = ((Group >> (i << 2)) & 15);
}

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
unsigned int If_CluPrimeCudd( unsigned int p )
{
    int i,pn;

    p--;
    do {
        p++;
        if (p&1) {
        pn = 1;
        i = 3;
        while ((unsigned) (i * i) <= p) {
        if (p % i == 0) {
            pn = 0;
            break;
        }
        i += 2;
        }
    } else {
        pn = 0;
    }
    } while (!pn);
    return(p);

} /* end of Cudd_Prime */

129
// hash table
130 131 132 133
static inline int If_CluWordNum( int nVars )
{
    return nVars <= 6 ? 1 : 1 << (nVars-6);
}
134 135
static inline int If_CluCountOnes( word t )
{
136 137 138 139 140 141
    t =    (t & ABC_CONST(0x5555555555555555)) + ((t>> 1) & ABC_CONST(0x5555555555555555));
    t =    (t & ABC_CONST(0x3333333333333333)) + ((t>> 2) & ABC_CONST(0x3333333333333333));
    t =    (t & ABC_CONST(0x0F0F0F0F0F0F0F0F)) + ((t>> 4) & ABC_CONST(0x0F0F0F0F0F0F0F0F));
    t =    (t & ABC_CONST(0x00FF00FF00FF00FF)) + ((t>> 8) & ABC_CONST(0x00FF00FF00FF00FF));
    t =    (t & ABC_CONST(0x0000FFFF0000FFFF)) + ((t>>16) & ABC_CONST(0x0000FFFF0000FFFF));
    return (t & ABC_CONST(0x00000000FFFFFFFF)) +  (t>>32);
142
}
143 144 145 146 147 148 149 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

void If_CluHashTableCheck( If_Man_t * p )
{
    int t = 1;
    If_Hte_t * pEntry;
    int i, RetValue, Status;
    for ( i = 0; i < p->nTableSize[t]; i++ )
    {
        for ( pEntry = ((If_Hte_t **)p->pHashTable[t])[i]; pEntry; pEntry = pEntry->pNext )
        {        
            Status = ((pEntry->Group & 15) > 0);
            RetValue = If_CutPerformCheck16( NULL, (unsigned *)pEntry->pTruth, 13, If_CluSupportSize(pEntry->pTruth, 13), "555" );
            if ( RetValue != Status ) 
            {
                Kit_DsdPrintFromTruth( (unsigned*)pEntry->pTruth, 13 ); printf( "\n" );
                RetValue = If_CutPerformCheck16( NULL, (unsigned *)pEntry->pTruth, 13, If_CluSupportSize(pEntry->pTruth, 13), "555" );
                printf( "Hash table problem!!!\n" );
            }
        }
    }
}
void If_CluHashPrintStats( If_Man_t * p, int t )
{
    If_Hte_t * pEntry;
    int i, Counter;
    for ( i = 0; i < p->nTableSize[t]; i++ )
    {
        Counter = 0;
        for ( pEntry = ((If_Hte_t **)p->pHashTable[t])[i]; pEntry; pEntry = pEntry->pNext )
            Counter++;
        if ( Counter == 0 )
            continue;
        if ( Counter < 10 )
            continue;
        printf( "%d=%d ", i, Counter );
    }
}
int If_CluHashFindMedian( If_Man_t * p, int t )
{
    If_Hte_t * pEntry;
    Vec_Int_t * vCounters;
    int i, Max = 0, Total = 0, Half = 0;
    vCounters = Vec_IntStart( 1000 );
    for ( i = 0; i < p->nTableSize[t]; i++ )
    {
        for ( pEntry = ((If_Hte_t **)p->pHashTable[t])[i]; pEntry; pEntry = pEntry->pNext )
        {
            if ( Max < (int)pEntry->Counter )
            {
                Max = pEntry->Counter;
                Vec_IntSetEntry( vCounters, pEntry->Counter, 0 );
            }
            Vec_IntAddToEntry( vCounters, pEntry->Counter, 1 );
            Total++;
        }
    }
    for ( i = Max; i > 0; i-- )
    {
        Half += Vec_IntEntry( vCounters, i );
        if ( Half > Total/2 )
            break;
    }
/*
    printf( "total = %d  ", Total );
    printf( "half = %d  ", Half );
    printf( "i = %d  ", i );
    printf( "Max = %d.\n", Max );
*/
    Vec_IntFree( vCounters );
    return Abc_MaxInt( i, 1 );
}
214 215 216 217 218
int If_CluHashKey( word * pTruth, int nWords, int Size )
{
    static unsigned BigPrimes[8] = {12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};
    unsigned Value = 0;
    int i;
219 220 221 222 223 224 225 226 227 228 229 230
    if ( nWords < 4 )
    {
        unsigned char * s = (unsigned char *)pTruth;
        for ( i = 0; i < 8 * nWords; i++ )
            Value ^= BigPrimes[i % 7] * s[i];
    }
    else
    {
        unsigned * s = (unsigned *)pTruth;
        for ( i = 0; i < 2 * nWords; i++ )
            Value ^= BigPrimes[i % 7] * s[i];
    }
231 232
    return Value % Size;
}
233
unsigned * If_CluHashLookup( If_Man_t * p, word * pTruth, int t )
234
{
235
    If_Hte_t * pEntry, * pPrev;
236 237 238 239 240 241
    int nWords, HashKey;
    if ( p == NULL )
        return NULL;
    nWords = If_CluWordNum(p->pPars->nLutSize);
    if ( p->pMemEntries == NULL )
        p->pMemEntries = Mem_FixedStart( sizeof(If_Hte_t) + sizeof(word) * (If_CluWordNum(p->pPars->nLutSize) - 1) );
242 243 244 245 246 247 248 249 250
    if ( p->pHashTable[t] == NULL )
    {
        // decide how large should be the table
        int nEntriesMax1 = 4 * If_CluPrimeCudd( Vec_PtrSize(p->vObjs) * p->pPars->nCutsMax );
        int nEntriesMax2 = (int)(((double)CLU_MEM_MAX * (1 << 20)) / If_CluWordNum(p->pPars->nLutSize) / 8);
//        int nEntriesMax2 = 10000;
        // create table
        p->nTableSize[t] = If_CluPrimeCudd( Abc_MinInt(nEntriesMax1, nEntriesMax2)/2 );
        p->pHashTable[t] = ABC_CALLOC( void *, p->nTableSize[t] );
251 252
    }
    // check if this entry exists
253 254
    HashKey = If_CluHashKey( pTruth, nWords, p->nTableSize[t] );
    for ( pEntry = ((If_Hte_t **)p->pHashTable[t])[HashKey]; pEntry; pEntry = pEntry->pNext )
255
        if ( memcmp(pEntry->pTruth, pTruth, sizeof(word) * nWords) == 0 )
256 257
        {
            pEntry->Counter++;
258
            return &pEntry->Group;
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
        }
    // resize the hash table
    if ( p->nTableEntries[t] >= 2 * p->nTableSize[t] )
    {
        // collect useful entries
        If_Hte_t * pPrev;
        Vec_Ptr_t * vUseful = Vec_PtrAlloc( p->nTableEntries[t] );
        int i, Median = If_CluHashFindMedian( p, t );
        for ( i = 0; i < p->nTableSize[t]; i++ )
        {
            for ( pEntry = ((If_Hte_t **)p->pHashTable[t])[i]; pEntry; )
            {
                if ( (int)pEntry->Counter > Median )
                {
                    Vec_PtrPush( vUseful, pEntry );
                    pEntry = pEntry->pNext;
                }
                else
                {
                    pPrev = pEntry->pNext;
                    Mem_FixedEntryRecycle( p->pMemEntries, (char *)pEntry );
                    pEntry = pPrev;
                }
            }
        }
        // add useful entries
        memset( p->pHashTable[t], 0, sizeof(void *) * p->nTableSize[t] );
        Vec_PtrForEachEntry( If_Hte_t *, vUseful, pEntry, i )
        {
            HashKey = If_CluHashKey( pEntry->pTruth, nWords, p->nTableSize[t] );
            pPrev = ((If_Hte_t **)p->pHashTable[t])[HashKey];
            if ( pPrev == NULL || pEntry->Counter >= pPrev->Counter )
            {
                pEntry->pNext = pPrev;
                ((If_Hte_t **)p->pHashTable[t])[HashKey] = pEntry;
            }
            else
            {
                while ( pPrev->pNext && pEntry->Counter < pPrev->pNext->Counter )
                    pPrev = pPrev->pNext;
                pEntry->pNext = pPrev->pNext;
                pPrev->pNext = pEntry;
            }
        }
        p->nTableEntries[t] = Vec_PtrSize( vUseful );
        Vec_PtrFree( vUseful );
    }
306
    // create entry
307
    p->nTableEntries[t]++;
308 309
    pEntry = (If_Hte_t *)Mem_FixedEntryFetch( p->pMemEntries );
    memcpy( pEntry->pTruth, pTruth, sizeof(word) * nWords );
310 311 312 313 314 315 316 317 318 319 320 321
    pEntry->Group = CLU_UNUSED;
    pEntry->Counter = 1;
    // insert at the beginning
//    pEntry->pNext = ((If_Hte_t **)p->pHashTable[t])[HashKey];
//    ((If_Hte_t **)p->pHashTable[t])[HashKey] = pEntry;
    // insert at the end
    pEntry->pNext = NULL;
    for ( pPrev = ((If_Hte_t **)p->pHashTable[t])[HashKey]; pPrev && pPrev->pNext; pPrev = pPrev->pNext );
    if ( pPrev == NULL )
        ((If_Hte_t **)p->pHashTable[t])[HashKey] = pEntry;
    else
        pPrev->pNext = pEntry;
322 323 324 325
    return &pEntry->Group;
}

// variable permutation for large functions
326 327 328 329 330 331 332 333 334 335
static inline void If_CluClear( word * pIn, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
        pIn[w] = 0;
}
static inline void If_CluFill( word * pIn, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
336
        pIn[w] = ~(word)0;
337
}
338 339 340 341 342 343
static inline void If_CluCopy( word * pOut, word * pIn, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
        pOut[w] = pIn[w];
}
344 345 346 347 348 349 350 351
static inline int If_CluEqual( word * pOut, word * pIn, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
        if ( pOut[w] != pIn[w] )
            return 0;
    return 1;
}
352 353 354 355 356 357 358 359 360 361 362
static inline void If_CluAnd( word * pRes, word * pIn1, word * pIn2, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
        pRes[w] = pIn1[w] & pIn2[w];
}
static inline void If_CluSharp( word * pRes, word * pIn1, word * pIn2, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
        pRes[w] = pIn1[w] & ~pIn2[w];
363
} 
364 365 366 367 368 369 370 371
static inline void If_CluOr( word * pRes, word * pIn1, word * pIn2, int nVars )
{
    int w, nWords = If_CluWordNum( nVars );
    for ( w = 0; w < nWords; w++ )
        pRes[w] = pIn1[w] | pIn2[w];
}
static inline word If_CluAdjust( word t, int nVars )
{
372 373 374 375
    assert( nVars >= 0 && nVars <= 6 );
    if ( nVars == 6 )
        return t;
    t &= (((word)1) << (1 << nVars)) - 1;
376 377 378 379 380 381 382 383 384 385 386 387 388 389
    if ( nVars == 0 )
        t |= t << (1<<nVars++);
    if ( nVars == 1 )
        t |= t << (1<<nVars++);
    if ( nVars == 2 )
        t |= t << (1<<nVars++);
    if ( nVars == 3 )
        t |= t << (1<<nVars++);
    if ( nVars == 4 )
        t |= t << (1<<nVars++);
    if ( nVars == 5 )
        t |= t << (1<<nVars++);
    return t;
}
390 391 392 393 394 395 396 397 398 399 400 401
static inline void If_CluAdjustBig( word * pF, int nVarsCur, int nVarsMax )
{
    int v, nWords;
    if ( nVarsCur == nVarsMax )
        return;
    assert( nVarsCur < nVarsMax );
    for ( v = Abc_MaxInt( nVarsCur, 6 ); v < nVarsMax; v++ )
    {
        nWords = If_CluWordNum( v );
        If_CluCopy( pF + nWords, pF, v );
    }
}
402 403 404 405 406 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
static inline void If_CluSwapAdjacent( word * pOut, word * pIn, int iVar, int nVars )
{
    int i, k, nWords = If_CluWordNum( nVars );
    assert( iVar < nVars - 1 );
    if ( iVar < 5 )
    {
        int Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            pOut[i] = (pIn[i] & PMasks[iVar][0]) | ((pIn[i] & PMasks[iVar][1]) << Shift) | ((pIn[i] & PMasks[iVar][2]) >> Shift);
    }
    else if ( iVar > 5 )
    {
        int Step = (1 << (iVar - 6));
        for ( k = 0; k < nWords; k += 4*Step )
        {
            for ( i = 0; i < Step; i++ )
                pOut[i] = pIn[i];
            for ( i = 0; i < Step; i++ )
                pOut[Step+i] = pIn[2*Step+i];
            for ( i = 0; i < Step; i++ )
                pOut[2*Step+i] = pIn[Step+i];
            for ( i = 0; i < Step; i++ )
                pOut[3*Step+i] = pIn[3*Step+i];
            pIn  += 4*Step;
            pOut += 4*Step;
        }
    }
    else // if ( iVar == 5 )
    {
        for ( i = 0; i < nWords; i += 2 )
        {
433 434
            pOut[i]   = (pIn[i]   & ABC_CONST(0x00000000FFFFFFFF)) | ((pIn[i+1] & ABC_CONST(0x00000000FFFFFFFF)) << 32);
            pOut[i+1] = (pIn[i+1] & ABC_CONST(0xFFFFFFFF00000000)) | ((pIn[i]   & ABC_CONST(0xFFFFFFFF00000000)) >> 32);
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 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 531 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 570 571 572 573 574 575 576 577 578 579
void If_CluChangePhase( word * pF, int nVars, int iVar )
{
    int nWords = If_CluWordNum( nVars );
    assert( iVar < nVars );
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            pF[i] = ((pF[i] & ~Truth6[iVar]) << Shift) | ((pF[i] & Truth6[iVar]) >> Shift);
    }
    else
    {
        word Temp;
        int i, k, Step = (1 << (iVar - 6));
        for ( k = 0; k < nWords; k += 2*Step )
        {
            for ( i = 0; i < Step; i++ )
            {
                Temp = pF[i];
                pF[i] = pF[Step+i];
                pF[Step+i] = Temp;
            }
            pF += 2*Step;
        }
    }
}
void If_CluCountOnesInCofs( word * pTruth, int nVars, int * pStore )
{
    int nWords = If_CluWordNum( nVars );
    int i, k, nOnes = 0, Limit = Abc_MinInt( nVars, 6 );
    memset( pStore, 0, sizeof(int) * 2 * nVars );
    // compute positive cofactors
    for ( k = 0; k < nWords; k++ )
        for ( i = 0; i < Limit; i++ )
            pStore[2*i+1] += If_CluCountOnes( pTruth[k] & Truth6[i] );
    if ( nVars > 6 )
    for ( k = 0; k < nWords; k++ )
        for ( i = 6; i < nVars; i++ )
            if ( k & (1 << (i-6)) )
                pStore[2*i+1] += If_CluCountOnes( pTruth[k] );
    // compute negative cofactors
    for ( k = 0; k < nWords; k++ )
        nOnes += If_CluCountOnes( pTruth[k] );
    for ( i = 0; i < nVars; i++ )
        pStore[2*i] = nOnes - pStore[2*i+1];
}
unsigned If_CluSemiCanonicize( word * pTruth, int nVars, int * pCanonPerm )
{
    word pFunc[CLU_WRD_MAX], * pIn = pTruth, * pOut = pFunc, * pTemp;
    int pStore[CLU_VAR_MAX*2];
    unsigned uCanonPhase = 0;
    int i, Temp, fChange, Counter = 0;
//Kit_DsdPrintFromTruth( (unsigned*)pTruth, nVars ); printf( "\n" );

    // collect signatures 
    If_CluCountOnesInCofs( pTruth, nVars, pStore );
    // canonicize phase
    for ( i = 0; i < nVars; i++ )
    {
        if ( pStore[2*i+0] <= pStore[2*i+1] )
            continue;
        uCanonPhase |= (1 << i);
        Temp = pStore[2*i+0];
        pStore[2*i+0] = pStore[2*i+1];
        pStore[2*i+1] = Temp;
        If_CluChangePhase( pIn, nVars, i );
    }
    // compute permutation
    for ( i = 0; i < nVars; i++ )
        pCanonPerm[i] = i;
    do {
        fChange = 0;
        for ( i = 0; i < nVars-1; i++ )
        {
            if ( pStore[2*i] <= pStore[2*(i+1)] )
                continue;
            Counter++;
            fChange = 1;

            Temp = pCanonPerm[i];
            pCanonPerm[i] = pCanonPerm[i+1];
            pCanonPerm[i+1] = Temp;

            Temp = pStore[2*i];
            pStore[2*i] = pStore[2*(i+1)];
            pStore[2*(i+1)] = Temp;

            Temp = pStore[2*i+1];
            pStore[2*i+1] = pStore[2*(i+1)+1];
            pStore[2*(i+1)+1] = Temp;

            If_CluSwapAdjacent( pOut, pIn, i, nVars );
            pTemp = pIn; pIn = pOut; pOut = pTemp;
        }
    } while ( fChange );
    // swap if it was moved an odd number of times
    if ( Counter & 1 )
        If_CluCopy( pOut, pIn, nVars );
    return uCanonPhase;
}
void If_CluSemiCanonicizeVerify( word * pTruth, word * pTruth0, int nVars, int * pCanonPerm, unsigned uCanonPhase )
{
    word pFunc[CLU_WRD_MAX], pGunc[CLU_WRD_MAX], * pIn = pTruth, * pOut = pFunc, * pTemp;
    int i, Temp, fChange, Counter = 0;
    If_CluCopy( pGunc, pTruth, nVars );
    // undo permutation
    do {
        fChange = 0;
        for ( i = 0; i < nVars-1; i++ )
        {
            if ( pCanonPerm[i] < pCanonPerm[i+1] )
                continue;

            Counter++;
            fChange = 1;

            Temp = pCanonPerm[i];
            pCanonPerm[i] = pCanonPerm[i+1];
            pCanonPerm[i+1] = Temp;

            If_CluSwapAdjacent( pOut, pIn, i, nVars );
            pTemp = pIn; pIn = pOut; pOut = pTemp;
        }
    } while ( fChange );
    if ( Counter & 1 )
        If_CluCopy( pOut, pIn, nVars );
    // undo phase
    for ( i = 0; i < nVars; i++ )
        if ( (uCanonPhase >> i) & 1 )
            If_CluChangePhase( pTruth, nVars, i );
    // compare
    if ( !If_CluEqual(pTruth0, pTruth, nVars) )
    {
        Kit_DsdPrintFromTruth( (unsigned*)pTruth0, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pGunc, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pTruth, nVars ); printf( "\n" );
        printf( "SemiCanonical verification FAILED!\n" );
    }
}


580 581 582 583
void If_CluPrintGroup( If_Grp_t * g )
{
    int i;
    printf( "Vars = %d   ", g->nVars );
584
    printf( "Myu = %d   {", g->nMyu );
585
    for ( i = 0; i < g->nVars; i++ )
586 587
        printf( " %c", 'a' + g->pVars[i] );
    printf( " }\n" );
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
}

void If_CluPrintConfig( int nVars, If_Grp_t * g, If_Grp_t * r, word BStruth, word * pFStruth )
{
    assert( r->nVars == nVars - g->nVars + 1 + (g->nMyu > 2) );
    If_CluPrintGroup( g );
    if ( g->nVars < 6 )
        BStruth = If_CluAdjust( BStruth, g->nVars );
    Kit_DsdPrintFromTruth( (unsigned *)&BStruth, g->nVars );
    printf( "\n" );
    If_CluPrintGroup( r );
    if ( r->nVars < 6 )
        pFStruth[0] = If_CluAdjust( pFStruth[0], r->nVars );
    Kit_DsdPrintFromTruth( (unsigned *)pFStruth, r->nVars );
    printf( "\n" );
}


void If_CluInitTruthTables()
{
    int i, k;
    assert( CLU_VAR_MAX <= 16 );
    for ( i = 0; i < 6; i++ )
        for ( k = 0; k < CLU_WRD_MAX; k++ )
            TruthAll[i][k] = Truth6[i];
    for ( i = 6; i < CLU_VAR_MAX; i++ )
        for ( k = 0; k < CLU_WRD_MAX; k++ )
615
            TruthAll[i][k] = ((k >> (i-6)) & 1) ? ~(word)0 : 0;
616

617 618
//    Extra_PrintHex( stdout, TruthAll[6], 8 ); printf( "\n" );
//    Extra_PrintHex( stdout, TruthAll[7], 8 ); printf( "\n" );
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
}


// verification
static void If_CluComposeLut( int nVars, If_Grp_t * g, word * t, word f[6][CLU_WRD_MAX], word * r )
{
    word c[CLU_WRD_MAX];
    int m, v;
    If_CluClear( r, nVars ); 
    for ( m = 0; m < (1<<g->nVars); m++ )
    {
        if ( !((t[m >> 6] >> (m & 63)) & 1) )
            continue;
        If_CluFill( c, nVars );
        for ( v = 0; v < g->nVars; v++ )
            if ( (m >> v) & 1 )
                If_CluAnd( c, c, f[v], nVars );
            else
                If_CluSharp( c, c, f[v], nVars );
        If_CluOr( r, r, c, nVars );
    }
}
void If_CluVerify( word * pF, int nVars, If_Grp_t * g, If_Grp_t * r, word BStruth, word * pFStruth )
{
    word pTTFans[6][CLU_WRD_MAX], pTTWire[CLU_WRD_MAX], pTTRes[CLU_WRD_MAX];
    int i;
    assert( g->nVars <= 6 && r->nVars <= 6 );

    if ( TruthAll[0][0] == 0 )
        If_CluInitTruthTables();

    for ( i = 0; i < g->nVars; i++ )
651
        If_CluCopy( pTTFans[i], TruthAll[(int)g->pVars[i]], nVars );
652 653 654 655 656 657
    If_CluComposeLut( nVars, g, &BStruth, pTTFans, pTTWire );

    for ( i = 0; i < r->nVars; i++ )
        if ( r->pVars[i] == nVars )
            If_CluCopy( pTTFans[i], pTTWire, nVars );
        else
658
            If_CluCopy( pTTFans[i], TruthAll[(int)r->pVars[i]], nVars );
659 660 661 662 663 664 665 666 667 668 669 670 671 672
    If_CluComposeLut( nVars, r, pFStruth, pTTFans, pTTRes );

    if ( !If_CluEqual(pTTRes, pF, nVars) )
    {
        printf( "\n" );
        If_CluPrintConfig( nVars, g, r, BStruth, pFStruth );
        Kit_DsdPrintFromTruth( (unsigned*)pTTRes, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pF, nVars ); printf( "\n" );
//        Extra_PrintHex( stdout, (unsigned *)pF, nVars ); printf( "\n" );
        printf( "Verification FAILED!\n" );
    }
//    else
//        printf( "Verification succeed!\n" );
}
673 674 675 676 677 678 679
void If_CluVerify3( word * pF, int nVars, If_Grp_t * g, If_Grp_t * g2, If_Grp_t * r, word BStruth, word BStruth2, word FStruth )
{
    word pTTFans[6][CLU_WRD_MAX], pTTWire[CLU_WRD_MAX], pTTWire2[CLU_WRD_MAX], pTTRes[CLU_WRD_MAX];
    int i;
    assert( g->nVars >= 2 && g2->nVars >= 2 && r->nVars >= 2 );
    assert( g->nVars <= 6 && g2->nVars <= 6 && r->nVars <= 6 );

680
    if ( TruthAll[0][0] == 0 )
681 682 683
        If_CluInitTruthTables();

    for ( i = 0; i < g->nVars; i++ )
684
        If_CluCopy( pTTFans[i], TruthAll[(int)g->pVars[i]], nVars );
685 686
    If_CluComposeLut( nVars, g, &BStruth, pTTFans, pTTWire );

687
    for ( i = 0; i < g2->nVars; i++ )
688
        If_CluCopy( pTTFans[i], TruthAll[(int)g2->pVars[i]], nVars );
689
    If_CluComposeLut( nVars, g2, &BStruth2, pTTFans, pTTWire2 );
690 691 692 693 694 695 696

    for ( i = 0; i < r->nVars; i++ )
        if ( r->pVars[i] == nVars )
            If_CluCopy( pTTFans[i], pTTWire, nVars );
        else if ( r->pVars[i] == nVars + 1 )
            If_CluCopy( pTTFans[i], pTTWire2, nVars );
        else
697
            If_CluCopy( pTTFans[i], TruthAll[(int)r->pVars[i]], nVars );
698 699 700 701
    If_CluComposeLut( nVars, r, &FStruth, pTTFans, pTTRes );

    if ( !If_CluEqual(pTTRes, pF, nVars) )
    {
702
        printf( "%d\n", nVars );
703
//        If_CluPrintConfig( nVars, g, r, BStruth, pFStruth );
704 705 706 707 708 709
//        Extra_PrintHex( stdout, (unsigned *)pF, nVars ); printf( "\n" );

        Kit_DsdPrintFromTruth( (unsigned*)&BStruth, g->nVars );   printf( "    " ); If_CluPrintGroup(g);  // printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)&BStruth2, g2->nVars ); printf( "    " ); If_CluPrintGroup(g2); // printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)&FStruth, r->nVars );   printf( "    " ); If_CluPrintGroup(r);  // printf( "\n" );

710 711 712 713 714 715 716 717 718 719
        Kit_DsdPrintFromTruth( (unsigned*)pTTWire, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pTTWire2, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pTTRes, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pF, nVars ); printf( "\n" );
//        Extra_PrintHex( stdout, (unsigned *)pF, nVars ); printf( "\n" );
        printf( "Verification FAILED!\n" );
    }
//    else
//        printf( "Verification succeed!\n" );
}
720 721


722 723 724 725 726 727 728 729

void If_CluSwapVars( word * pTruth, int nVars, int * V2P, int * P2V, int iVar, int jVar )
{
    word low2High, high2Low, temp;
    int nWords = If_CluWordNum(nVars);
    int shift, step, iStep, jStep;
    int w = 0, i = 0, j = 0;
    static word PPMasks[6][6] = {
730 731 732 733 734 735
        { ABC_CONST(0x2222222222222222), ABC_CONST(0x0A0A0A0A0A0A0A0A), ABC_CONST(0x00AA00AA00AA00AA), ABC_CONST(0x0000AAAA0000AAAA), ABC_CONST(0x00000000AAAAAAAA), ABC_CONST(0xAAAAAAAAAAAAAAAA) },
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0C0C0C0C0C0C0C0C), ABC_CONST(0x00CC00CC00CC00CC), ABC_CONST(0x0000CCCC0000CCCC), ABC_CONST(0x00000000CCCCCCCC), ABC_CONST(0xCCCCCCCCCCCCCCCC) },
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x00F000F000F000F0), ABC_CONST(0x0000F0F00000F0F0), ABC_CONST(0x00000000F0F0F0F0), ABC_CONST(0xF0F0F0F0F0F0F0F0) },
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000FF000000FF00), ABC_CONST(0x00000000FF00FF00), ABC_CONST(0xFF00FF00FF00FF00) },
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x00000000FFFF0000), ABC_CONST(0xFFFF0000FFFF0000) },
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0xFFFFFFFF00000000) }
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 763 764 765 766 767 768 769 770 771 772 773 774 775
    };
    if( iVar == jVar )
        return;
    if( jVar < iVar )
    {
        int varTemp = jVar;
        jVar = iVar;
        iVar = varTemp;
    }
    if ( iVar <= 5 && jVar <= 5 )
    {
        shift = (1 <<  jVar) - (1 << iVar);
        for ( w = 0; w < nWords; w++ )
        {
            low2High = (pTruth[w] & PPMasks[iVar][jVar - 1] ) << shift;
            pTruth[w] &= ~PPMasks[iVar][jVar - 1];
            high2Low = (pTruth[w] & (PPMasks[iVar][jVar - 1] << shift )) >> shift;
            pTruth[w] &= ~ (PPMasks[iVar][jVar - 1] << shift);
            pTruth[w] = pTruth[w] | low2High | high2Low;
        }
    }
    else if( iVar <= 5 && jVar > 5 )
    {
        step = If_CluWordNum(jVar + 1)/2;
        shift = 1 << iVar;
        for ( w = 0; w < nWords; w += 2*step )
        {
            for (j = 0; j < step; j++)
            {
                low2High = (pTruth[w + j] & PPMasks[iVar][5]) >> shift;
                pTruth[w + j] &= ~PPMasks[iVar][5];
                high2Low = (pTruth[w + step + j] & (PPMasks[iVar][5] >> shift)) << shift;
                pTruth[w + step + j] &= ~(PPMasks[iVar][5] >> shift);
                pTruth[w + j] |= high2Low;
                pTruth[w + step + j] |= low2High;            
            }
        }
    }
    else
    {
776 777 778 779 780 781 782 783 784 785 786 787 788
        iStep = If_CluWordNum(iVar + 1)/2;
        jStep = If_CluWordNum(jVar + 1)/2;
        for (w = 0; w < nWords; w += 2*jStep)
        {
            for (i = 0; i < jStep; i += 2*iStep)
            {
                for (j = 0; j < iStep; j++)
                {
                    temp = pTruth[w + iStep + i + j];
                    pTruth[w + iStep + i + j] = pTruth[w + jStep + i + j];
                    pTruth[w + jStep + i + j] = temp;
                }
            }
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
        }
    }    
    if ( V2P && P2V )
    {
        V2P[P2V[iVar]] = jVar;
        V2P[P2V[jVar]] = iVar;
        P2V[iVar] ^= P2V[jVar];
        P2V[jVar] ^= P2V[iVar];
        P2V[iVar] ^= P2V[jVar];
    }
}
void If_CluReverseOrder( word * pTruth, int nVars, int * V2P, int * P2V, int iVarStart )
{
    int i, j, k;
    for ( k = 0; k < (nVars-iVarStart)/2 ; k++ )
    {
        i = iVarStart + k;
        j = nVars - 1 - k;
        If_CluSwapVars( pTruth, nVars, V2P, P2V, i, j );
    }
}

// moves one var (v) to the given position (p)
void If_CluMoveVar2( word * pF, int nVars, int * Var2Pla, int * Pla2Var, int v, int p )
{
    If_CluSwapVars( pF, nVars, Var2Pla, Pla2Var, Var2Pla[v], p );
}

817
// moves one var (v) to the given position (p)
818
void If_CluMoveVar( word * pF, int nVars, int * Var2Pla, int * Pla2Var, int v, int p )
819
{
820
    word pG[CLU_WRD_MAX], * pIn = pF, * pOut = pG, * pTemp;
821 822
    int iPlace0, iPlace1, Count = 0;
    assert( v >= 0 && v < nVars );
823
    while ( Var2Pla[v] < p )
824
    {
825 826 827 828 829 830 831 832 833 834
        iPlace0 = Var2Pla[v];
        iPlace1 = Var2Pla[v]+1;
        If_CluSwapAdjacent( pOut, pIn, iPlace0, nVars );
        pTemp = pIn; pIn = pOut, pOut = pTemp;
        Var2Pla[Pla2Var[iPlace0]]++;
        Var2Pla[Pla2Var[iPlace1]]--;
        Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
        Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
        Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
        Count++;
835
    }
836
    while ( Var2Pla[v] > p )
837
    {
838 839 840 841 842 843 844 845 846 847
        iPlace0 = Var2Pla[v]-1;
        iPlace1 = Var2Pla[v];
        If_CluSwapAdjacent( pOut, pIn, iPlace0, nVars );
        pTemp = pIn; pIn = pOut, pOut = pTemp;
        Var2Pla[Pla2Var[iPlace0]]++;
        Var2Pla[Pla2Var[iPlace1]]--;
        Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
        Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
        Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
        Count++;
848 849 850 851 852 853 854
    }
    if ( Count & 1 )
        If_CluCopy( pF, pIn, nVars );
    assert( Pla2Var[p] == v );
}

// moves vars to be the most signiticant ones (Group[0] is MSB)
855
void If_CluMoveGroupToMsb( word * pF, int nVars, int * V2P, int * P2V, If_Grp_t * g )
856 857
{
    int v;
858 859
    for ( v = 0; v < g->nVars; v++ )
        If_CluMoveVar( pF, nVars, V2P, P2V, g->pVars[g->nVars - 1 - v], nVars - 1 - v );
860 861
}

862

863
// reverses the variable order
864
void If_CluReverseOrder_old( word * pF, int nVars, int * V2P, int * P2V, int iVarStart )
865
{
866
    word pG[CLU_WRD_MAX];
867
    int v;
868

869 870
    If_CluCopy( pG, pF, nVars );

871 872 873 874
//    for ( v = 0; v < nVars; v++ )
//        printf( "%c ", 'a' + P2V[v] );
//    printf( "  ---  " );

875
    for ( v = iVarStart; v < nVars; v++ )
876 877 878 879 880
        If_CluMoveVar( pF, nVars, V2P, P2V, P2V[iVarStart], nVars - 1 - (v - iVarStart) );

//    for ( v = 0; v < nVars; v++ )
//        printf( "%c ", 'a' + P2V[v] );
//    printf( "\n" );
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

//    if ( iVarStart > 0 )
//        return;

    If_CluReverseOrder( pG, nVars, NULL, NULL, iVarStart );
    if ( If_CluEqual( pG, pF, nVars ) )
    {
//        printf( "+" );
    }
    else
    {
/*
        printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pF, nVars ); printf( "\n" );
        Kit_DsdPrintFromTruth( (unsigned*)pG, nVars ); 
        printf( "\n" );
*/
        printf( "%d ", nVars );
    }
900
}
901

902 903 904 905 906 907 908 909 910
// return the number of cofactors w.r.t. the topmost vars (nBSsize)
int If_CluCountCofs( word * pF, int nVars, int nBSsize, int iShift, word pCofs[3][CLU_WRD_MAX/4] )
{
    word iCofs[128], iCof, Result = 0;
    word * pCofA, * pCofB;
    int nMints = (1 << nBSsize);
    int i, c, w, nCofs;
    assert( nBSsize >= 2 && nBSsize <= 7 && nBSsize < nVars );
    if ( nVars - nBSsize < 6 )
911
    {
912 913
        int nShift = (1 << (nVars - nBSsize));
        word Mask  = ((((word)1) << nShift) - 1);
914 915
        for ( nCofs = i = 0; i < nMints; i++ )
        {
916
            iCof = (pF[(iShift + i * nShift) / 64] >> ((iShift + i * nShift) & 63)) & Mask;
917
            for ( c = 0; c < nCofs; c++ )
918
                if ( iCof == iCofs[c] )
919 920
                    break;
            if ( c == nCofs )
921 922 923
                iCofs[nCofs++] = iCof;
            if ( pCofs && iCof != iCofs[0] )
                Result |= (((word)1) << i);
924
            if ( nCofs == 5 )
925
                break;
926
        }
927
        if ( nCofs <= 2 && pCofs )
928
        {
929 930 931 932
            assert( nBSsize <= 6 );
            pCofs[0][0] = iCofs[0];
            pCofs[1][0] = (nCofs == 2) ? iCofs[1] : iCofs[0];
            pCofs[2][0] = Result;
933 934 935 936
        }
    }
    else
    {
937 938
        int nWords = If_CluWordNum( nVars - nBSsize );
        assert( nWords * nMints == If_CluWordNum(nVars) );
939 940
        for ( nCofs = i = 0; i < nMints; i++ )
        {
941
            pCofA = pF + i * nWords;
942
            for ( c = 0; c < nCofs; c++ )
943 944 945 946 947 948
            {
                pCofB = pF + iCofs[c] * nWords;
                for ( w = 0; w < nWords; w++ )
                    if ( pCofA[w] != pCofB[w] )
                        break;
                if ( w == nWords )
949
                    break;
950
            }
951
            if ( c == nCofs )
952 953 954 955 956 957 958 959 960 961 962
                iCofs[nCofs++] = i;
            if ( pCofs )
            {
                assert( nBSsize <= 6 );
                pCofB = pF + iCofs[0] * nWords;
                for ( w = 0; w < nWords; w++ )
                    if ( pCofA[w] != pCofB[w] )
                        break;
                if ( w != nWords )
                    Result |= (((word)1) << i);
            }
963 964 965
            if ( nCofs == 5 )
                break;
        }
966 967 968 969 970 971
        if ( nCofs <= 2 && pCofs )
        {
            If_CluCopy( pCofs[0], pF + iCofs[0] * nWords, nVars - nBSsize );
            If_CluCopy( pCofs[1], pF + ((nCofs == 2) ? iCofs[1] : iCofs[0]) * nWords, nVars - nBSsize );
            pCofs[2][0] = Result;
        }
972
    }
973
    assert( nCofs >= 1 && nCofs <= 5 );
974 975 976
    return nCofs;
}

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
// return the number of cofactors w.r.t. the topmost vars (nBSsize)
int If_CluCountCofs4( word * pF, int nVars, int nBSsize, word pCofs[6][CLU_WRD_MAX/4] )
{
    word iCofs[128], iCof, Result0 = 0, Result1 = 0;
    int nMints = (1 << nBSsize);
    int i, c, nCofs;
    assert( pCofs );
    assert( nBSsize >= 2 && nBSsize <= 6 && nBSsize < nVars );
    if ( nVars - nBSsize < 6 )
    {
        int nShift = (1 << (nVars - nBSsize));
        word Mask  = ((((word)1) << nShift) - 1);
        for ( nCofs = i = 0; i < nMints; i++ )
        {
            iCof = (pF[(i * nShift) / 64] >> ((i * nShift) & 63)) & Mask;
            for ( c = 0; c < nCofs; c++ )
                if ( iCof == iCofs[c] )
                    break;
            if ( c == nCofs )
                iCofs[nCofs++] = iCof;
            if ( c == 1 || c == 3 )
                Result0 |= (((word)1) << i);
            if ( c == 2 || c == 3 )
                Result1 |= (((word)1) << i);
        }
        assert( nCofs >= 3 && nCofs <= 4 );
        pCofs[0][0] = iCofs[0];
        pCofs[1][0] = iCofs[1];
        pCofs[2][0] = iCofs[2];
        pCofs[3][0] = (nCofs == 4) ? iCofs[3] : iCofs[2];
        pCofs[4][0] = Result0;
        pCofs[5][0] = Result1;
    }
    else
        assert( 0 );
    return nCofs;
}

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
void If_CluCofactors( word * pF, int nVars, int iVar, word * pCof0, word * pCof1 )
{
    int nWords = If_CluWordNum( nVars );
    assert( iVar < nVars );
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
        {
            pCof0[i] = (pF[i] & ~Truth6[iVar]) | ((pF[i] & ~Truth6[iVar]) << Shift);
            pCof1[i] = (pF[i] &  Truth6[iVar]) | ((pF[i] &  Truth6[iVar]) >> Shift);
        }
    }
    else
    {
        int i, k, Step = (1 << (iVar - 6));
        for ( k = 0; k < nWords; k += 2*Step )
        {
            for ( i = 0; i < Step; i++ )
            {
                pCof0[i] = pCof0[Step+i] = pF[i];
                pCof1[i] = pCof1[Step+i] = pF[Step+i];
            }
            pF    += 2*Step;
            pCof0 += 2*Step;
            pCof1 += 2*Step;
        }
    }
}

1045 1046
// returns 1 if we have special case of cofactors; otherwise, returns 0
int If_CluDetectSpecialCaseCofs( word * pF, int nVars, int iVar )
1047
{
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    word Cof0, Cof1;
    int State[6] = {0};
    int i, nWords = If_CluWordNum( nVars );
    assert( iVar < nVars );
    if ( iVar < 6 )
    {
        int Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
        {
            Cof0 =  (pF[i] & ~Truth6[iVar]);
            Cof1 = ((pF[i] &  Truth6[iVar]) >> Shift);

            if ( Cof0 == 0 )
                State[0]++;
            else if ( Cof0 == ~Truth6[iVar] )
                State[1]++;
            else if ( Cof1 == 0 )
                State[2]++;
            else if ( Cof1 == ~Truth6[iVar] )
                State[3]++;
            else if ( Cof0 == ~Cof1 )
                State[4]++;
            else if ( Cof0 == Cof1 )
                State[5]++;
        }
    }
    else
    {
        int k, Step = (1 << (iVar - 6));
        for ( k = 0; k < nWords; k += 2*Step )
        {
            for ( i = 0; i < Step; i++ )
            {
                Cof0 = pF[i];
                Cof1 = pF[Step+i];

                if ( Cof0 == 0 )
                    State[0]++;
1086
                else if ( Cof0 == ~(word)0 )
1087 1088 1089
                    State[1]++;
                else if ( Cof1 == 0 )
                    State[2]++;
1090
                else if ( Cof1 == ~(word)0 )
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
                    State[3]++;
                else if ( Cof0 == ~Cof1 )
                    State[4]++;
                else if ( Cof0 == Cof1 )
                    State[5]++;
            }
            pF    += 2*Step;
        }
        nWords /= 2;
    }
    assert( State[5] != nWords );
    for ( i = 0; i < 5; i++ )
    {
        assert( State[i] <= nWords );
        if ( State[i] == nWords )
            return i;
    }
    return -1;
}
1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
// returns 1 if we have special case of cofactors; otherwise, returns 0
If_Grp_t If_CluDecUsingCofs( word * pTruth, int nVars, int nLutLeaf )
{
    If_Grp_t G = {0};
    word pF2[CLU_WRD_MAX], * pF = pF2;
    int Var2Pla[CLU_VAR_MAX+2], Pla2Var[CLU_VAR_MAX+2];
    int V2P[CLU_VAR_MAX+2], P2V[CLU_VAR_MAX+2];
    int nVarsNeeded = nVars - nLutLeaf;
    int v, i, k, iVar, State;
//Kit_DsdPrintFromTruth( (unsigned*)pTruth, nVars ); printf( "\n" );
    // create local copy
    If_CluCopy( pF, pTruth, nVars );
    for ( k = 0; k < nVars; k++ )
        Var2Pla[k] = Pla2Var[k] = k;
    // find decomposable vars 
    for ( i = 0; i < nVarsNeeded; i++ )
    {
        for ( v = nVars - 1; v >= 0; v-- )
        {
            State = If_CluDetectSpecialCaseCofs( pF, nVars, v );
            if ( State == -1 )
                continue;
            // update the variable place
            iVar = Pla2Var[v];
            while ( Var2Pla[iVar] < nVars - 1 )
            {
                int iPlace0 = Var2Pla[iVar];
                int iPlace1 = Var2Pla[iVar]+1;
                Var2Pla[Pla2Var[iPlace0]]++;
                Var2Pla[Pla2Var[iPlace1]]--;
                Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
                Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
                Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
            }
            // move this variable to the top
            for ( k = 0; k < nVars; k++ )
                V2P[k] = P2V[k] = k;
//Kit_DsdPrintFromTruth( (unsigned*)pF, nVars ); printf( "\n" );
            If_CluMoveVar( pF, nVars, V2P, P2V, v, nVars - 1 );
//Kit_DsdPrintFromTruth( (unsigned*)pF, nVars ); printf( "\n" );
            // choose cofactor to follow
            iVar = nVars - 1;
            if ( State == 0 || State == 1 ) // need cof1
            {
                if ( iVar < 6 )
                    pF[0] = (pF[0] &  Truth6[iVar]) | ((pF[0] &  Truth6[iVar]) >> (1 << iVar));
                else
                    pF += If_CluWordNum( nVars ) / 2;
            }
            else // need cof0
            {
                if ( iVar < 6 )
                    pF[0] = (pF[0] & ~Truth6[iVar]) | ((pF[0] & ~Truth6[iVar]) << (1 << iVar));
            }
            // update the variable count
            nVars--;
            break;
        }
        if ( v == -1 )
            return G;
    }
    // create the resulting group
    G.nVars = nLutLeaf;
    G.nMyu = 2;
    for ( v = 0; v < G.nVars; v++ )
        G.pVars[v] = Pla2Var[v];
    return G;
}
1179 1180 1181



1182 1183 1184 1185 1186 1187 1188
// deriving decomposition
word If_CluDeriveDisjoint( word * pF, int nVars, int * V2P, int * P2V, If_Grp_t * g, If_Grp_t * r )
{
    word pCofs[3][CLU_WRD_MAX/4];
    int i, RetValue, nFSset = nVars - g->nVars;
    RetValue = If_CluCountCofs( pF, nVars, g->nVars, 0, pCofs );
//    assert( RetValue == 2 );
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    if ( nFSset < 6 )
        pF[0] = (pCofs[1][0] << (1 << nFSset)) | pCofs[0][0];
    else
    {
        If_CluCopy( pF, pCofs[0], nFSset );
        If_CluCopy( pF + If_CluWordNum(nFSset), pCofs[1], nFSset );
    }
    // create the resulting group
    if ( r )
    {
        r->nVars = nFSset + 1;
        r->nMyu = 0;
        for ( i = 0; i < nFSset; i++ )
            r->pVars[i] = P2V[i];
        r->pVars[nFSset] = nVars;
    }
    return pCofs[2][0];
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
void If_CluDeriveDisjoint4( word * pF, int nVars, int * V2P, int * P2V, If_Grp_t * g, If_Grp_t * r, word * pTruth0, word * pTruth1 )
{
    word pCofs[6][CLU_WRD_MAX/4];
    word Cof0, Cof1;
    int i, RetValue, nFSset = nVars - g->nVars;

    assert( g->nVars <= 6 && nFSset <= 4 );

    RetValue = If_CluCountCofs4( pF, nVars, g->nVars, pCofs );
    if ( RetValue != 3 && RetValue != 4 )
        printf( "If_CluDeriveDisjoint4(): Error!!!\n" );

    Cof0  = (pCofs[1][0] << (1 << nFSset)) | pCofs[0][0];
    Cof1  = (pCofs[3][0] << (1 << nFSset)) | pCofs[2][0];
    pF[0] = (Cof1 << (1 << (nFSset+1))) | Cof0;
    pF[0] = If_CluAdjust( pF[0], nFSset + 2 );

    // create the resulting group
    r->nVars = nFSset + 2;
    r->nMyu = 0;
    for ( i = 0; i < nFSset; i++ )
        r->pVars[i] = P2V[i];
    r->pVars[nFSset] = nVars;
    r->pVars[nFSset+1] = nVars+1;

    *pTruth0 = If_CluAdjust( pCofs[4][0], g->nVars );
    *pTruth1 = If_CluAdjust( pCofs[5][0], g->nVars );
}

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
word If_CluDeriveNonDisjoint( word * pF, int nVars, int * V2P, int * P2V, If_Grp_t * g, If_Grp_t * r )
{
    word pCofs[2][CLU_WRD_MAX];
    word Truth0, Truth1, Truth;
    int i, nFSset = nVars - g->nVars, nFSset1 = nFSset + 1;
    If_CluCofactors( pF, nVars, nVars - 1, pCofs[0], pCofs[1] );

//    Extra_PrintHex( stdout, (unsigned *)pCofs[0], nVars ); printf( "\n" );
//    Extra_PrintHex( stdout, (unsigned *)pCofs[1], nVars ); printf( "\n" );

    g->nVars--;
    Truth0 = If_CluDeriveDisjoint( pCofs[0], nVars - 1, V2P, P2V, g, NULL );
    Truth1 = If_CluDeriveDisjoint( pCofs[1], nVars - 1, V2P, P2V, g, NULL );
    Truth  = (Truth1 << (1 << g->nVars)) | Truth0;
    g->nVars++;
    if ( nFSset1 < 6 )
        pF[0] = (pCofs[1][0] << (1 << nFSset1)) | pCofs[0][0];
    else
    {
        If_CluCopy( pF, pCofs[0], nFSset1 );
        If_CluCopy( pF + If_CluWordNum(nFSset1), pCofs[1], nFSset1 );
    }
1259

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
//    Extra_PrintHex( stdout, (unsigned *)&Truth0, 6 ); printf( "\n" );
//    Extra_PrintHex( stdout, (unsigned *)&Truth1, 6 ); printf( "\n" );
//    Extra_PrintHex( stdout, (unsigned *)&pCofs[0][0], 6 ); printf( "\n" );
//    Extra_PrintHex( stdout, (unsigned *)&pCofs[1][0], 6 ); printf( "\n" );
//    Extra_PrintHex( stdout, (unsigned *)&Truth, 6 ); printf( "\n" );
//    Extra_PrintHex( stdout, (unsigned *)&pF[0], 6 ); printf( "\n" );

    // create the resulting group
    r->nVars = nFSset + 2;
    r->nMyu = 0;
    for ( i = 0; i < nFSset; i++ )
        r->pVars[i] = P2V[i];
    r->pVars[nFSset] = nVars;
    r->pVars[nFSset+1] = g->pVars[g->nVars - 1];
    return Truth;
}
1276

1277
// check non-disjoint decomposition
1278
int If_CluCheckNonDisjointGroup( word * pF, int nVars, int * V2P, int * P2V, If_Grp_t * g )
1279
{
1280
    int v, i, nCofsBest2;
1281
    if ( (g->nMyu == 3 || g->nMyu == 4) )
1282
    {
1283
        word pCofs[2][CLU_WRD_MAX];
1284
        // try cofactoring w.r.t. each variable
1285
        for ( v = 0; v < g->nVars; v++ )
1286
        {
1287
            If_CluCofactors( pF, nVars, V2P[(int)g->pVars[v]], pCofs[0], pCofs[1] );
1288
            nCofsBest2 = If_CluCountCofs( pCofs[0], nVars, g->nVars, 0, NULL );
1289 1290
            if ( nCofsBest2 > 2 )
                continue;
1291
            nCofsBest2 = If_CluCountCofs( pCofs[1], nVars, g->nVars, 0, NULL );
1292 1293
            if ( nCofsBest2 > 2 )
                continue;
1294 1295 1296 1297
            // found good shared variable - move to the end
            If_CluMoveVar( pF, nVars, V2P, P2V, g->pVars[v], nVars-1 );
            for ( i = 0; i < g->nVars; i++ )
                g->pVars[i] = P2V[nVars-g->nVars+i];
1298 1299
            return 1;
        }
1300
    }
1301 1302 1303
    return 0;
}

1304

1305
// finds a good var group (cof count < 6; vars are MSBs)
1306
If_Grp_t If_CluFindGroup( word * pF, int nVars, int iVarStart, int iVarStop, int * V2P, int * P2V, int nBSsize, int fDisjoint )
1307
{
1308 1309
    int fVerbose = 0;
    int nRounds = 2;//nBSsize;
1310
    If_Grp_t G = {0}, * g = &G;//, BestG = {0};
1311
    int i, r, v, nCofs, VarBest, nCofsBest2;
1312
    assert( nVars > nBSsize && nVars >= nBSsize + iVarStart && nVars <= CLU_VAR_MAX );
1313
    assert( nBSsize >= 2 && nBSsize <= 6 );
1314
    assert( !iVarStart || !iVarStop );
1315
    // start with the default group
1316 1317
    g->nVars = nBSsize;
    g->nMyu = If_CluCountCofs( pF, nVars, nBSsize, 0, NULL );
1318
    for ( i = 0; i < nBSsize; i++ )
1319
        g->pVars[i] = P2V[nVars-nBSsize+i];
1320
    // check if good enough
1321 1322
    if ( g->nMyu == 2 )
        return G;
1323
    if ( !fDisjoint && If_CluCheckNonDisjointGroup( pF, nVars, V2P, P2V, g ) )
1324 1325
    {
//        BestG = G;
1326
        return G;
1327
    }
1328 1329 1330 1331 1332
    if ( nVars == nBSsize + iVarStart )
    {
        g->nVars = 0;
        return G;
    }
1333

1334 1335 1336 1337 1338
    if ( fVerbose )
    {
        printf( "Iter %2d  ", -1 );
        If_CluPrintGroup( g );
    }
1339

1340
    // try to find better group
1341
    for ( r = 0; r < nRounds; r++ )
1342
    {
1343
        if ( nBSsize < nVars-1 )
1344
        {
1345 1346 1347 1348
            // find the best var to add
            VarBest = P2V[nVars-1-nBSsize];
            nCofsBest2 = If_CluCountCofs( pF, nVars, nBSsize+1, 0, NULL );
            for ( v = nVars-2-nBSsize; v >= iVarStart; v-- )
1349
            {
1350 1351
//                If_CluMoveVar( pF, nVars, V2P, P2V, P2V[v], nVars-1-nBSsize );
                If_CluMoveVar2( pF, nVars, V2P, P2V, P2V[v], nVars-1-nBSsize );
1352 1353 1354 1355 1356 1357
                nCofs = If_CluCountCofs( pF, nVars, nBSsize+1, 0, NULL );
                if ( nCofsBest2 >= nCofs )
                {
                    nCofsBest2 = nCofs;
                    VarBest = P2V[nVars-1-nBSsize];
                }
1358
            }
1359
            // go back
1360 1361
//            If_CluMoveVar( pF, nVars, V2P, P2V, VarBest, nVars-1-nBSsize );
            If_CluMoveVar2( pF, nVars, V2P, P2V, VarBest, nVars-1-nBSsize );
1362 1363 1364
            // update best bound set
            nCofs = If_CluCountCofs( pF, nVars, nBSsize+1, 0, NULL );
            assert( nCofs == nCofsBest2 );
1365 1366 1367
        }

        // find the best var to remove
1368
        VarBest = P2V[nVars-1-nBSsize];
1369
        nCofsBest2 = If_CluCountCofs( pF, nVars, nBSsize, 0, NULL );
1370
        for ( v = nVars-nBSsize; v < nVars-iVarStop; v++ )
1371
        {
1372 1373
//            If_CluMoveVar( pF, nVars, V2P, P2V, P2V[v], nVars-1-nBSsize );
            If_CluMoveVar2( pF, nVars, V2P, P2V, P2V[v], nVars-1-nBSsize );
1374 1375
            nCofs = If_CluCountCofs( pF, nVars, nBSsize, 0, NULL );
            if ( nCofsBest2 >= nCofs )
1376 1377
            {
                nCofsBest2 = nCofs;
1378
                VarBest = P2V[nVars-1-nBSsize];
1379 1380
            }
        }
1381

1382
        // go back
1383 1384
//        If_CluMoveVar( pF, nVars, V2P, P2V, VarBest, nVars-1-nBSsize );
        If_CluMoveVar2( pF, nVars, V2P, P2V, VarBest, nVars-1-nBSsize );
1385
        // update best bound set
1386
        nCofs = If_CluCountCofs( pF, nVars, nBSsize, 0, NULL );
1387
        assert( nCofs == nCofsBest2 );
1388
        if ( g->nMyu >= nCofs )
1389
        {
1390 1391
            g->nVars = nBSsize;
            g->nMyu = nCofs;
1392
            for ( i = 0; i < nBSsize; i++ )
1393
                g->pVars[i] = P2V[nVars-nBSsize+i];
1394
        }
1395

1396 1397 1398 1399 1400
        if ( fVerbose )
        {
            printf( "Iter %2d  ", r );
            If_CluPrintGroup( g );
        }
1401 1402

        // check if good enough
1403 1404
        if ( g->nMyu == 2 )
            return G;
1405
        if ( !fDisjoint && If_CluCheckNonDisjointGroup( pF, nVars, V2P, P2V, g ) )
1406 1407
        {
//            BestG = G;
1408
            return G;
1409
        }
1410
    }
1411 1412

    assert( r == nRounds );
1413 1414
    g->nVars = 0;
    return G;
1415
//    return BestG;
1416 1417
}

1418 1419

// double check that the given group has a decomposition
1420
void If_CluCheckGroup( word * pTruth, int nVars, If_Grp_t * g )
1421
{
1422 1423
    word pF[CLU_WRD_MAX];
    int v, nCofs, V2P[CLU_VAR_MAX], P2V[CLU_VAR_MAX];
1424
    assert( g->nVars >= 2 && g->nVars <= 6 ); // vars
1425
    assert( g->nMyu >= 2 && g->nMyu <= 4 ); // cofs
1426 1427 1428 1429 1430 1431
    // create permutation
    for ( v = 0; v < nVars; v++ )
        V2P[v] = P2V[v] = v;
    // create truth table
    If_CluCopy( pF, pTruth, nVars );
    // move group up
1432
    If_CluMoveGroupToMsb( pF, nVars, V2P, P2V, g );
1433
    // check the number of cofactors
1434 1435
    nCofs = If_CluCountCofs( pF, nVars, g->nVars, 0, NULL );
    if ( nCofs != g->nMyu )
1436 1437 1438 1439
        printf( "Group check 0 has failed.\n" );
    // check compatible
    if ( nCofs > 2 )
    {
1440
        nCofs = If_CluCountCofs( pF, nVars-1, g->nVars-1, 0, NULL );
1441 1442
        if ( nCofs > 2 )
            printf( "Group check 1 has failed.\n" );
1443
        nCofs = If_CluCountCofs( pF, nVars-1, g->nVars-1, (1 << (nVars-1)), NULL );
1444 1445 1446 1447 1448 1449
        if ( nCofs > 2 )
            printf( "Group check 2 has failed.\n" );
    }
}


1450 1451 1452 1453 1454 1455 1456
// double check that the permutation derived is correct
void If_CluCheckPerm( word * pTruth, word * pF, int nVars, int * V2P, int * P2V )
{
    int i;
    for ( i = 0; i < nVars; i++ )
        If_CluMoveVar( pF, nVars, V2P, P2V, i, i );

1457
    if ( !If_CluEqual( pTruth, pF, nVars ) )
1458
        printf( "Permutation FAILED.\n" );
1459 1460
//    else
//        printf( "Permutation successful\n" );
1461 1462 1463
}


1464 1465


1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
static inline int If_CluSuppIsMinBase( int Supp )
{
    return (Supp & (Supp+1)) == 0;
}
static inline int If_CluHasVar( word * t, int nVars, int iVar )
{
    int nWords = If_CluWordNum( nVars );
    assert( iVar < nVars );
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            if ( (t[i] & ~Truth6[iVar]) != ((t[i] & Truth6[iVar]) >> Shift) )
                return 1;
        return 0;
    }
    else
    {
        int i, k, Step = (1 << (iVar - 6));
        for ( k = 0; k < nWords; k += 2*Step )
        {
            for ( i = 0; i < Step; i++ )
                if ( t[i] != t[Step+i] )
                    return 1;
            t += 2*Step;
        }
        return 0;
    }
}
static inline int If_CluSupport( word * t, int nVars )
{
    int v, Supp = 0;
    for ( v = 0; v < nVars; v++ )
        if ( If_CluHasVar( t, nVars, v ) )
            Supp |= (1 << v);
    return Supp;
}
1503 1504 1505 1506 1507 1508 1509 1510
int If_CluSupportSize( word * t, int nVars )
{
    int v, SuppSize = 0;
    for ( v = 0; v < nVars; v++ )
        if ( If_CluHasVar( t, nVars, v ) )
            SuppSize++;
    return SuppSize;
}
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
static inline void If_CluTruthShrink( word * pF, int nVars, int nVarsAll, unsigned Phase )
{
    word pG[CLU_WRD_MAX], * pIn = pF, * pOut = pG, * pTemp;
    int i, k, Var = 0, Counter = 0;
    assert( nVarsAll <= 16 );
    for ( i = 0; i < nVarsAll; i++ )
        if ( Phase & (1 << i) )
        {
            for ( k = i-1; k >= Var; k-- )
            {
                If_CluSwapAdjacent( pOut, pIn, k, nVarsAll );
                pTemp = pIn; pIn = pOut, pOut = pTemp;
                Counter++;
            }
            Var++;
        }
    assert( Var == nVars );
    // swap if it was moved an odd number of times
    if ( Counter & 1 )
        If_CluCopy( pOut, pIn, nVarsAll );
}
int If_CluMinimumBase( word * t, int * pSupp, int nVarsAll, int * pnVars )
{
    int v, iVar = 0, uSupp = 0;
    assert( nVarsAll <= 16 );
    for ( v = 0; v < nVarsAll; v++ )
        if ( If_CluHasVar( t, nVarsAll, v ) )
        {
            uSupp |= (1 << v);
            if ( pSupp )
                pSupp[iVar] = pSupp[v];
            iVar++;
        }
    if ( pnVars )
        *pnVars = iVar;
    if ( If_CluSuppIsMinBase( uSupp ) )
        return 0;
    If_CluTruthShrink( t, iVar, nVarsAll, uSupp );
    return 1;
}
1551

1552
// returns the best group found
1553
If_Grp_t If_CluCheck( If_Man_t * p, word * pTruth0, int nVars, int iVarStart, int iVarStop, int nLutLeaf, int nLutRoot, 
1554
                     If_Grp_t * pR, word * pFunc0, word * pFunc1, word * pLeftOver, int fHashing )
1555
{
1556
//    int fEnableHashing = 0;
1557 1558
    If_Grp_t G1 = {0}, R = {0};
    unsigned * pHashed = NULL;
1559 1560 1561
    word Truth, pTruth[CLU_WRD_MAX], pF[CLU_WRD_MAX];//, pG[CLU_WRD_MAX];
    int V2P[CLU_VAR_MAX+2], P2V[CLU_VAR_MAX+2], pCanonPerm[CLU_VAR_MAX];
    int i, nSupp, uCanonPhase;
1562
    int nLutSize = p ? p->pPars->nLutSize : nVars;
1563
    assert( nVars <= CLU_VAR_MAX );
1564
    assert( nVars <= nLutLeaf + nLutRoot - 1 );
1565

1566 1567 1568 1569 1570 1571 1572
    if ( pR )
    {
        pR->nVars = 0;
        *pFunc0 = 0;
        *pFunc1 = 0;
    }

1573
    // canonicize truth table
1574
    If_CluCopy( pTruth, pTruth0, nLutSize );
1575 1576 1577 1578

    if ( 0 )
    {
        uCanonPhase = If_CluSemiCanonicize( pTruth, nVars, pCanonPerm );
1579
        If_CluAdjustBig( pTruth, nVars, nLutSize );
1580 1581 1582
    }

//    If_CluSemiCanonicizeVerify( pTruth, pTruth0, nVars, pCanonPerm, uCanonPhase );
1583
//    If_CluCopy( pTruth, pTruth0, nLutSize );
1584

1585 1586 1587 1588 1589 1590
 /*
    {
        int pCanonPerm[32];
        short pStore[32];
        unsigned uCanonPhase;
        If_CluCopy( pF, pTruth, nVars );
1591
        uCanonPhase = Kit_TruthSemiCanonicize( pF, pG, nVars, pCanonPerm );
1592 1593 1594 1595
        G1.nVars = 1;
        return G1;
    }
*/
1596 1597
    // check minnimum base
    If_CluCopy( pF, pTruth, nVars );
1598 1599 1600
    for ( i = 0; i < nVars; i++ )
        V2P[i] = P2V[i] = i;
    // check support
1601
    nSupp = If_CluSupport( pF, nVars );
1602
//Extra_PrintBinary( stdout, &nSupp, 16 );  printf( "\n" );
1603
    if ( !nSupp || !If_CluSuppIsMinBase(nSupp) )
1604 1605
    {
//        assert( 0 );     
1606
        return G1;
1607
    }
1608

1609
    // check hash table
1610
    if ( p && fHashing )
1611
    {
1612 1613 1614
        pHashed = If_CluHashLookup( p, pTruth, 0 );
        if ( pHashed && *pHashed != CLU_UNUSED )
            If_CluUns2Grp( *pHashed, &G1 );
1615
    }
1616

1617 1618 1619 1620
    // update the variable order so that the first var was the last one
    if ( iVarStop )
        If_CluMoveVar( pF, nVars, V2P, P2V, 0, nVars-1 );

1621
    if ( G1.nVars == 0 ) 
1622
    {
Alan Mishchenko committed
1623 1624
        s_Count2++;

1625
        // detect easy cofs
1626 1627
        if ( iVarStart == 0 )
            G1 = If_CluDecUsingCofs( pTruth, nVars, nLutLeaf );
1628 1629
        if ( G1.nVars == 0 )
        {
1630
            // perform testing
1631
            G1 = If_CluFindGroup( pF, nVars, iVarStart, iVarStop, V2P, P2V, nLutLeaf, nLutLeaf + nLutRoot == nVars + 1 );
1632
    //        If_CluCheckPerm( pTruth, pF, nVars, V2P, P2V );
1633 1634
            if ( G1.nVars == 0 )
            {
1635 1636 1637 1638
                // perform testing with a smaller set
                if ( nVars < nLutLeaf + nLutRoot - 2 )
                {
                    nLutLeaf--;
1639
                    G1 = If_CluFindGroup( pF, nVars, iVarStart, iVarStop, V2P, P2V, nLutLeaf, nLutLeaf + nLutRoot == nVars + 1 );
1640 1641
                    nLutLeaf++;
                }
1642
                // perform testing with a smaller set
1643
                if ( nLutLeaf > 4 && nVars < nLutLeaf + nLutRoot - 3 )
1644 1645 1646
                {
                    nLutLeaf--;
                    nLutLeaf--;
1647
                    G1 = If_CluFindGroup( pF, nVars, iVarStart, iVarStop, V2P, P2V, nLutLeaf, nLutLeaf + nLutRoot == nVars + 1 );
1648 1649 1650
                    nLutLeaf++;
                    nLutLeaf++;
                }
1651 1652
                if ( G1.nVars == 0 )
                {
1653 1654
                    // perform testing with a different order
                    If_CluReverseOrder( pF, nVars, V2P, P2V, iVarStart );
1655
                    G1 = If_CluFindGroup( pF, nVars, iVarStart, iVarStop, V2P, P2V, nLutLeaf, nLutLeaf + nLutRoot == nVars + 1 );
1656 1657 1658 1659

                    // check permutation
    //                If_CluCheckPerm( pTruth, pF, nVars, V2P, P2V );
                    if ( G1.nVars == 0 )
1660
                    {
1661 1662 1663 1664 1665 1666
                        // remember free set, just in case
//                        for ( i = 0; i < nVars - nLutLeaf; i++ )
///                           G1.pVars[nLutLeaf+i] = P2V[i];
                        // if <XY>, this will not be used
                        // if <XYZ>, this will not be hashed

1667 1668 1669 1670 1671 1672 1673 1674 1675
    /*
                        if ( nVars == 6 )
                        {
                            Extra_PrintHex( stdout, (unsigned *)pF, nVars );  printf( "    " );
                            Kit_DsdPrintFromTruth( (unsigned*)pF, nVars );  printf( "\n" );
                            if ( !If_CutPerformCheck07( (unsigned *)pF, 6, 6, NULL ) )
                                printf( "no\n" );
                        } 
    */
1676 1677 1678
                        if ( pHashed )
                            *pHashed = If_CluGrp2Uns( &G1 );
                        return G1;
1679
                    }
1680 1681 1682 1683
                }
            }
        }
    }
1684

1685
    // derive
1686
    if ( pR )
1687
    {
1688 1689
        int iNewPos;

1690 1691
        If_CluMoveGroupToMsb( pF, nVars, V2P, P2V, &G1 );
        if ( G1.nMyu == 2 )
1692
        {
1693
            Truth = If_CluDeriveDisjoint( pF, nVars, V2P, P2V, &G1, &R );
1694 1695
            iNewPos = R.nVars - 1;
        }
1696
        else
1697
        {
1698
            Truth = If_CluDeriveNonDisjoint( pF, nVars, V2P, P2V, &G1, &R );
1699 1700 1701
            iNewPos = R.nVars - 2;
        }
        assert( R.pVars[iNewPos] == nVars );
1702

1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
        // adjust the functions
        Truth = If_CluAdjust( Truth, G1.nVars );
        if ( R.nVars < 6 )
            pF[0] = If_CluAdjust( pF[0], R.nVars );

//        Kit_DsdPrintFromTruth( (unsigned*)&Truth, G1.nVars ); printf( "  ...1\n" );
//        Kit_DsdPrintFromTruth( (unsigned*)pF, R.nVars );      printf( "  ...1\n" );

        // update the variable order of R so that the new var was the first one
//        if ( iVarStart == 0 )
        {
            int k, V2P2[CLU_VAR_MAX+2], P2V2[CLU_VAR_MAX+2];
            assert( iNewPos >= iVarStart );
            for ( k = 0; k < R.nVars; k++ )
                V2P2[k] = P2V2[k] = k;
            If_CluMoveVar( pF, R.nVars, V2P2, P2V2, iNewPos, iVarStart );
            for ( k = iNewPos; k > iVarStart; k-- )
                R.pVars[k] = R.pVars[k-1];
            R.pVars[iVarStart] = nVars;
        }

//        Kit_DsdPrintFromTruth( (unsigned*)pF, R.nVars ); printf( "  ...2\n" );
1725 1726 1727 1728 1729 1730 1731 1732 1733

        if ( pLeftOver )
        {
            if ( R.nVars < 6 )
                *pLeftOver = If_CluAdjust( pF[0], R.nVars );
            else
                If_CluCopy( pLeftOver, pF, R.nVars );
            If_CluAdjustBig( pLeftOver, R.nVars, nLutSize );
        }
1734

1735 1736 1737 1738 1739
        // perform checking
        if ( 0 )
        {
            If_CluCheckGroup( pTruth, nVars, &G1 );
            If_CluVerify( pTruth, nVars, &G1, &R, Truth, pF );
1740 1741 1742
        } 

        // save functions
1743
        *pR = R;
1744 1745 1746 1747
        if ( pFunc0 )
            *pFunc0 = pF[0];
        if ( pFunc1 )
            *pFunc1 = Truth;
1748
    }
1749 1750 1751 1752

    if ( pHashed )
        *pHashed = If_CluGrp2Uns( &G1 );
    return G1;
1753 1754
}

1755
/*
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
static inline word Abc_Tt6Cofactor0( word t, int iVar )
{
    assert( iVar >= 0 && iVar < 6 );
    return (t &~Truth6[iVar]) | ((t &~Truth6[iVar]) << (1<<iVar));
}
static inline word Abc_Tt6Cofactor1( word t, int iVar )
{
    assert( iVar >= 0 && iVar < 6 );
    return (t & Truth6[iVar]) | ((t & Truth6[iVar]) >> (1<<iVar));
}
1766
*/
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
int If_CluCheckDecInAny( word t, int nVars )
{
    int v, u, Cof2[2], Cof4[4];
    for ( v = 0; v < nVars; v++ )
    {
        Cof2[0] = Abc_Tt6Cofactor0( t, v );
        Cof2[1] = Abc_Tt6Cofactor1( t, v );
        for ( u = v+1; u < nVars; u++ )
        {
            Cof4[0] = Abc_Tt6Cofactor0( Cof2[0], u );
            Cof4[1] = Abc_Tt6Cofactor1( Cof2[0], u );
            Cof4[2] = Abc_Tt6Cofactor0( Cof2[1], u );
            Cof4[3] = Abc_Tt6Cofactor1( Cof2[1], u );
            if ( Cof4[0] == Cof4[1] && Cof4[0] == Cof4[2] )
                return 1;
            if ( Cof4[0] == Cof4[2] && Cof4[0] == Cof4[3] )
                return 1;
            if ( Cof4[0] == Cof4[1] && Cof4[0] == Cof4[3] )
                return 1;
            if ( Cof4[1] == Cof4[2] && Cof4[1] == Cof4[3] )
                return 1;
        }
    }
    return 0;
}
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
int If_CluCheckDecIn( word t, int nVars )
{
    int v, u, Cof2[2], Cof4[4];
//    for ( v = 0; v < nVars; v++ )
    for ( v = 0; v < 1; v++ ) // restrict to the first (decomposed) input
    {
        Cof2[0] = Abc_Tt6Cofactor0( t, v );
        Cof2[1] = Abc_Tt6Cofactor1( t, v );
        for ( u = v+1; u < nVars; u++ )
        {
            Cof4[0] = Abc_Tt6Cofactor0( Cof2[0], u );
            Cof4[1] = Abc_Tt6Cofactor1( Cof2[0], u );
            Cof4[2] = Abc_Tt6Cofactor0( Cof2[1], u );
            Cof4[3] = Abc_Tt6Cofactor1( Cof2[1], u );
            if ( Cof4[0] == Cof4[1] && Cof4[0] == Cof4[2] )
                return 1;
            if ( Cof4[0] == Cof4[2] && Cof4[0] == Cof4[3] )
                return 1;
            if ( Cof4[0] == Cof4[1] && Cof4[0] == Cof4[3] )
                return 1;
            if ( Cof4[1] == Cof4[2] && Cof4[1] == Cof4[3] )
                return 1;
        }
    }
    return 0;
}
int If_CluCheckDecInU( word t, int nVars )
{
    int v, u, Cof2[2], Cof4[4];
//    for ( v = 0; v < nVars; v++ )
    for ( v = 0; v < 1; v++ ) // restrict to the first (decomposed) input
    {
        Cof2[0] = Abc_Tt6Cofactor0( t, v );
        Cof2[1] = Abc_Tt6Cofactor1( t, v );
        for ( u = v+1; u < nVars; u++ )
        {
1828 1829 1830 1831 1832
            Cof4[0] = Abc_Tt6Cofactor0( Cof2[0], u ); // 00
            Cof4[1] = Abc_Tt6Cofactor1( Cof2[0], u ); // 01
            Cof4[2] = Abc_Tt6Cofactor0( Cof2[1], u ); // 10
            Cof4[3] = Abc_Tt6Cofactor1( Cof2[1], u ); // 11 
            if ( Cof4[0] == Cof4[1] && Cof4[0] == Cof4[2] ) //  F * a
1833
                return 1;
1834
            if ( Cof4[0] == Cof4[2] && Cof4[0] == Cof4[3] ) // !F * a
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
                return 1;
        }
    }
    return 0;
}
int If_CluCheckDecOut( word t, int nVars )
{
    int v;
    for ( v = 0; v < nVars; v++ )
        if ( 
             (t & Truth6[v]) == 0   ||  //  F * !a
             (~t & Truth6[v]) == 0  ||  // !F * !a
             (t & ~Truth6[v]) == 0  ||  //  F *  a
             (~t & ~Truth6[v]) == 0     // !F *  a   
           )
            return 1;
    return 0;
}
int If_CluCheckDecOutU( word t, int nVars )
{
    int v;
    for ( v = 0; v < nVars; v++ )
        if ( 
             (t & ~Truth6[v]) == 0  ||  //  F *  a
             (~t & ~Truth6[v]) == 0     // !F *  a   
           )
            return 1;
    return 0;
}

int If_CutPerformCheck45( If_Man_t * p, unsigned * pTruth, int nVars, int nLeaves, char * pStr )
{    
    // 5LUT -> 4LUT
    If_Grp_t G, R;
    word Func0, Func1;
1870
    G = If_CluCheck( p, (word *)pTruth, nLeaves, 0, 0, 5, 4, &R, &Func0, &Func1, NULL, 0 );
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
    if ( G.nVars == 0 )
        return 0;
    Func0 = If_CluAdjust( Func0, R.nVars );
    Func1 = If_CluAdjust( Func1, G.nVars );
#if 0
    Kit_DsdPrintFromTruth( pTruth, nVars ); printf( "\n" );
    Kit_DsdPrintFromTruth( (unsigned*)&Func0, R.nVars ); printf( "\n" );
    Kit_DsdPrintFromTruth( (unsigned*)&Func1, G.nVars ); printf( "\n" );
    If_CluPrintGroup( &R );
    If_CluPrintGroup( &G );
#endif
    if ( G.nVars < 5 || (p->pPars->fEnableCheck75 && If_CluCheckDecOut(Func1, 5)) || (p->pPars->fEnableCheck75u && If_CluCheckDecOutU(Func1, 5)) )
        return 1;
    return 0;
}
int If_CutPerformCheck54( If_Man_t * p, unsigned * pTruth, int nVars, int nLeaves, char * pStr )
{    
    // 4LUT -> 5LUT
    If_Grp_t G, R;
    word Func0, Func1;
1891
    G = If_CluCheck( p, (word *)pTruth, nLeaves, 0, 0, 4, 5, &R, &Func0, &Func1, NULL, 0 );
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
    if ( G.nVars == 0 )
        return 0;
    Func0 = If_CluAdjust( Func0, R.nVars );
    Func1 = If_CluAdjust( Func1, G.nVars );
#if 0
    Kit_DsdPrintFromTruth( pTruth, nVars ); printf( "\n" );
    Kit_DsdPrintFromTruth( (unsigned*)&Func0, R.nVars ); printf( "\n" );
    Kit_DsdPrintFromTruth( (unsigned*)&Func1, G.nVars ); printf( "\n" );
    If_CluPrintGroup( &R );
    If_CluPrintGroup( &G );
#endif
    if ( R.nVars < 5 || (p->pPars->fEnableCheck75 && If_CluCheckDecIn(Func0, 5)) || (p->pPars->fEnableCheck75u && If_CluCheckDecInU(Func0, 5)) )
        return 1;
    return 0;
}

1908 1909 1910 1911
// returns the best group found
If_Grp_t If_CluCheck3( If_Man_t * p, word * pTruth0, int nVars, int nLutLeaf, int nLutLeaf2, int nLutRoot, 
                      If_Grp_t * pR, If_Grp_t * pG2, word * pFunc0, word * pFunc1, word * pFunc2 )
{
1912
    int fEnableHashing = 0;
1913
    static int Counter = 0;
1914
    unsigned * pHashed = NULL;
1915 1916 1917 1918
    word pLeftOver[CLU_WRD_MAX], Func0, Func1, Func2;
    If_Grp_t G1 = {0}, G2 = {0}, R = {0}, R2 = {0};
    int i;
    Counter++;
1919 1920 1921 1922 1923 1924 1925
//    if ( Counter == 37590 )
//    {
//        int ns = 0;
//    }

    // check hash table
    if ( p && fEnableHashing )
1926
    {
1927 1928 1929 1930 1931 1932
        pHashed = If_CluHashLookup( p, pTruth0, 1 );
        if ( pHashed && *pHashed != CLU_UNUSED )
        {
            If_CluUns2Grp( *pHashed, &G1 );
            return G1;
        }
1933
    }
Alan Mishchenko committed
1934
    s_Count3++;
1935 1936

    // check two-node decomposition
1937
    G1 = If_CluCheck( p, pTruth0, nVars, 0, 0, nLutLeaf, nLutRoot + nLutLeaf2 - 1, &R2, &Func0, &Func1, pLeftOver, 0 );
1938 1939
    // decomposition does not exist
    if ( G1.nVars == 0 )
1940 1941
    {
        // check for decomposition with two outputs
1942
        if ( (G1.nMyu == 3 || G1.nMyu == 4) && nLutLeaf == nLutLeaf2 && nVars - nLutLeaf + 2 <= nLutRoot )
1943
        {
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
            int V2P[CLU_VAR_MAX+2], P2V[CLU_VAR_MAX+2];
            word Func0, Func1, Func2;
            int iVar0, iVar1;

            G1.nVars = nLutLeaf;
            If_CluCopy( pLeftOver, pTruth0, nVars );
            for ( i = 0; i < nVars; i++ )
                V2P[i] = P2V[i] = i;

            If_CluMoveGroupToMsb( pLeftOver, nVars, V2P, P2V, &G1 );
            If_CluDeriveDisjoint4( pLeftOver, nVars, V2P, P2V, &G1, &R, &Func1, &Func2 );

            // move the two vars to the front
            for ( i = 0; i < R.nVars; i++ )
                V2P[i] = P2V[i] = i;
            If_CluMoveVar( pLeftOver, R.nVars, V2P, P2V, R.nVars-2, 0 );
            If_CluMoveVar( pLeftOver, R.nVars, V2P, P2V, R.nVars-1, 1 );
            iVar0 = R.pVars[R.nVars-2];
            iVar1 = R.pVars[R.nVars-1];
            for ( i = R.nVars-1; i > 1; i-- )
                R.pVars[i] = R.pVars[i-2];
            R.pVars[0] = iVar0;
            R.pVars[1] = iVar1;

            Func0 = pLeftOver[0];
            If_CluVerify3( pTruth0, nVars, &G1, &G1, &R, Func1, Func2, Func0 );
            if ( pFunc1 && pFunc2 )
1971
            {
1972 1973 1974 1975 1976 1977 1978 1979 1980
                *pFunc0 = Func0;
                *pFunc1 = Func1;
                *pFunc2 = Func2;
                *pG2 = G1;
                *pR = R;
            }

            if ( pHashed )
                *pHashed = If_CluGrp2Uns( &G1 );
1981 1982
//                Kit_DsdPrintFromTruth( (unsigned*)pTruth0, nVars );  printf( "\n" );
//                If_CluPrintGroup( &G1 );
1983
            return G1;
1984
        }
1985

1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
/*
//        if ( nVars == 6 )
        {
//            Extra_PrintHex( stdout, (unsigned *)pTruth0, nVars );  printf( "    " );
            Kit_DsdPrintFromTruth( (unsigned*)pTruth0, nVars );  printf( "\n" );
            if ( p != NULL )
            If_CluCheck3( NULL, pTruth0, nVars, nLutLeaf, nLutLeaf2, nLutRoot, pR, pG2, pFunc0, pFunc1, pFunc2 );
        } 
*/
        if ( pHashed )
            *pHashed = If_CluGrp2Uns( &G1 );
1997
        return G1;
1998
    }
1999
    // decomposition exists and sufficient
2000
    if ( R2.nVars <= nLutRoot )
2001 2002 2003 2004 2005 2006
    {
        if ( pG2 )     *pG2 = G2;
        if ( pR )      *pR  = R2;
        if ( pFunc0 )  *pFunc0 = Func0;
        if ( pFunc1 )  *pFunc1 = Func1;
        if ( pFunc2 )  *pFunc2 = 0;
2007 2008
        if ( pHashed )
            *pHashed = If_CluGrp2Uns( &G1 );
2009 2010 2011 2012
        return G1;
    }

    // try second decomposition
2013 2014 2015 2016 2017 2018 2019
    {
        int Test = 0;
        if ( Test )
        {
            Kit_DsdPrintFromTruth( (unsigned*)&pLeftOver, R2.nVars ); printf( "\n" );
        }
    }
2020 2021

    // the new variable is at the bottom - skip it (iVarStart = 1)
2022
    if ( p->pPars->nStructType == 0 ) // allowed
2023
        G2 = If_CluCheck( p, pLeftOver, R2.nVars, 0, 0, nLutLeaf2, nLutRoot, &R, &Func0, &Func2, NULL, 0 );
2024
    else if ( p->pPars->nStructType == 1 ) // not allowed
2025
        G2 = If_CluCheck( p, pLeftOver, R2.nVars, 1, 0, nLutLeaf2, nLutRoot, &R, &Func0, &Func2, NULL, 0 );
2026
    else if ( p->pPars->nStructType == 2 ) // required
2027 2028 2029
        G2 = If_CluCheck( p, pLeftOver, R2.nVars, 0, 1, nLutLeaf2, nLutRoot, &R, &Func0, &Func2, NULL, 0 );
    else assert( 0 );

2030
    if ( G2.nVars == 0 )
2031 2032 2033
    {
        if ( pHashed )
            *pHashed = If_CluGrp2Uns( &G2 );
2034
        return G2;
2035
    }
2036 2037 2038 2039
    // remap variables
    for ( i = 0; i < G2.nVars; i++ )
    {
        assert( G2.pVars[i] < R2.nVars );
2040
        G2.pVars[i] = R2.pVars[ (int)G2.pVars[i] ];
2041 2042 2043 2044 2045 2046 2047
    }
    // remap variables
    for ( i = 0; i < R.nVars; i++ )
    {
        if ( R.pVars[i] == R2.nVars )
            R.pVars[i] = nVars + 1;
        else
2048
            R.pVars[i] = R2.pVars[ (int)R.pVars[i] ];
2049 2050 2051 2052 2053 2054 2055 2056
    }

    // decomposition exist
    if ( pG2 )     *pG2 = G2;
    if ( pR )      *pR  = R;
    if ( pFunc0 )  *pFunc0 = Func0;
    if ( pFunc1 )  *pFunc1 = Func1;
    if ( pFunc2 )  *pFunc2 = Func2;
2057 2058
    if ( pHashed )
        *pHashed = If_CluGrp2Uns( &G1 );
2059 2060 2061

    // verify
//    If_CluVerify3( pTruth0, nVars, &G1, &G2, &R, Func1, Func2, Func0 );
2062 2063
    return G1;
}
2064

2065
// returns the best group found
2066
int If_CluCheckExt( void * pMan, word * pTruth, int nVars, int nLutLeaf, int nLutRoot, 
2067
                   char * pLut0, char * pLut1, word * pFunc0, word * pFunc1 )
2068
{
2069
    If_Man_t * p = (If_Man_t *)pMan;
2070
    If_Grp_t G, R;
2071
    G = If_CluCheck( p, pTruth, nVars, 0, 0, nLutLeaf, nLutRoot, &R, pFunc0, pFunc1, NULL, 0 );
2072 2073 2074 2075 2076 2077
    memcpy( pLut0, &R, sizeof(If_Grp_t) );
    memcpy( pLut1, &G, sizeof(If_Grp_t) );
//    memcpy( pLut2, &G2, sizeof(If_Grp_t) );
    return (G.nVars > 0);
}

2078
// returns the best group found
2079
int If_CluCheckExt3( void * pMan, word * pTruth, int nVars, int nLutLeaf, int nLutLeaf2, int nLutRoot, 
2080 2081
                    char * pLut0, char * pLut1, char * pLut2, word * pFunc0, word * pFunc1, word * pFunc2 )
{
2082
    If_Man_t * p = (If_Man_t *)pMan;
2083 2084 2085 2086 2087 2088 2089 2090 2091
    If_Grp_t G, G2, R;
    G = If_CluCheck3( p, pTruth, nVars, nLutLeaf, nLutLeaf2, nLutRoot, &R, &G2, pFunc0, pFunc1, pFunc2 );
    memcpy( pLut0, &R, sizeof(If_Grp_t) );
    memcpy( pLut1, &G, sizeof(If_Grp_t) );
    memcpy( pLut2, &G2, sizeof(If_Grp_t) );
    return (G.nVars > 0);
}


2092
// computes delay of the decomposition
2093
float If_CluDelayMax( If_Grp_t * g, float * pDelays )
2094 2095 2096
{
    float Delay = 0.0;
    int i;
2097
    for ( i = 0; i < g->nVars; i++ )
2098
        Delay = Abc_MaxFloat( Delay, pDelays[(int)g->pVars[i]] );
2099
    return Delay;
2100 2101
}

2102 2103 2104
// returns delay of the decomposition;  sets area of the cut as its cost
float If_CutDelayLutStruct( If_Man_t * p, If_Cut_t * pCut, char * pStr, float WireDelay )
{
2105 2106
    float Delays[CLU_VAR_MAX+2];
    int fUsed[CLU_VAR_MAX+2] = {0};
2107
    If_Obj_t * pLeaf;
2108
    If_Grp_t G1 = {0}, G2 = {0}, G3 = {0};
2109 2110 2111
    int nLeaves = If_CutLeaveNum(pCut);
    int i, nLutLeaf, nLutRoot;
    // mark the cut as user cut
2112
//    pCut->fUser = 1;
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
    // quit if parameters are wrong
    if ( strlen(pStr) != 2 )
    {
        printf( "Wrong LUT struct (%s)\n", pStr );
        return ABC_INFINITY;
    }
    nLutLeaf = pStr[0] - '0';
    if ( nLutLeaf < 3 || nLutLeaf > 6 )
    {
        printf( "Leaf size (%d) should belong to {3,4,5,6}.\n", nLutLeaf );
        return ABC_INFINITY;
    }
    nLutRoot = pStr[1] - '0';
    if ( nLutRoot < 3 || nLutRoot > 6 )
    {
2128
        printf( "Root size (%d) should belong to {3,4,5,6}.\n", nLutRoot );
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
        return ABC_INFINITY;
    }
    if ( nLeaves > nLutLeaf + nLutRoot - 1 )
    {
        printf( "The cut size (%d) is too large for the LUT structure %d%d.\n", If_CutLeaveNum(pCut), nLutLeaf, nLutRoot );
        return ABC_INFINITY;
    }

    // remember the delays
    If_CutForEachLeaf( p, pCut, pLeaf, i )
2139
        Delays[i] = If_ObjCutBest(pLeaf)->Delay;
2140 2141 2142 2143

    // consider easy case
    if ( nLeaves <= Abc_MaxInt( nLutLeaf, nLutRoot ) )
    {
2144
        char * pPerm = If_CutPerm( pCut );
2145 2146 2147
        assert( nLeaves <= 6 );
        for ( i = 0; i < nLeaves; i++ )
        {
2148
            pPerm[i] = 1;
2149
            G1.pVars[i] = i;
2150
        }
2151 2152
        G1.nVars = nLeaves;
        return 1.0 + If_CluDelayMax( &G1, Delays );
2153 2154 2155
    }

    // derive the first group
2156
    G1 = If_CluCheck( p, If_CutTruthW(p, pCut), nLeaves, 0, 0, nLutLeaf, nLutRoot, NULL, NULL, NULL, NULL, 1 );
2157
    if ( G1.nVars == 0 )
2158
        return ABC_INFINITY;
2159

2160
    // compute the delay
Alan Mishchenko committed
2161
    Delays[nLeaves] = If_CluDelayMax( &G1, Delays ) + ((WireDelay == 0.0)?1.0:WireDelay);
2162
    if ( G2.nVars )
Alan Mishchenko committed
2163
        Delays[nLeaves+1] = If_CluDelayMax( &G2, Delays ) + ((WireDelay == 0.0)?1.0:WireDelay);
2164 2165

    // mark used groups
2166
    for ( i = 0; i < G1.nVars; i++ )
2167
        fUsed[(int)G1.pVars[i]] = 1;
2168
    for ( i = 0; i < G2.nVars; i++ )
2169
        fUsed[(int)G2.pVars[i]] = 1;
2170
    // mark unused groups
2171 2172
    assert( G1.nMyu >= 2 && G1.nMyu <= 4 );
    if ( G1.nMyu > 2 )
2173
        fUsed[(int)G1.pVars[G1.nVars-1]] = 0;
2174
    assert( !G2.nVars || (G2.nMyu >= 2 && G2.nMyu <= 4) );
2175
    if ( G2.nMyu > 2 )
2176
        fUsed[(int)G2.pVars[G2.nVars-1]] = 0;
2177 2178

    // create remaining group
2179
    assert( G3.nVars == 0 );
2180 2181
    for ( i = 0; i < nLeaves; i++ )
        if ( !fUsed[i] )
2182 2183
            G3.pVars[(int)G3.nVars++] = i;
    G3.pVars[(int)G3.nVars++] = nLeaves;
2184
    if ( G2.nVars )
2185
        G3.pVars[(int)G3.nVars++] = nLeaves+1;
2186 2187
    assert( G1.nVars + G2.nVars + G3.nVars == nLeaves + 
        (G1.nVars > 0) + (G2.nVars > 0) + (G1.nMyu > 2) + (G2.nMyu > 2) );
2188 2189
    // what if both non-disjoint vars are the same???

2190 2191
    pCut->Cost = 2 + (G2.nVars > 0);
    return 1.0 + If_CluDelayMax( &G3, Delays );
2192 2193
}

Alan Mishchenko committed
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
//#define IF_TRY_NEW

#ifdef IF_TRY_NEW
static Vec_Mem_t * s_vTtMem = NULL;
static Vec_Mem_t * s_vTtMem2 = NULL;
int If_TtMemCutNum()  { return Vec_MemEntryNum(s_vTtMem); }
int If_TtMemCutNum2() { return Vec_MemEntryNum(s_vTtMem2); }
//        printf( "Unique TTs = %d.  Unique classes = %d.    ", If_TtMemCutNum(), If_TtMemCutNum2() );
//        printf( "Check2 = %d.  Check3 = %d.\n", s_Count2, s_Count3 );
#endif
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215

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

  Synopsis    [Performs additional check.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2216
int If_CutPerformCheck16( If_Man_t * p, unsigned * pTruth0, int nVars, int nLeaves, char * pStr )
2217
{
2218
    unsigned pTruth[IF_MAX_FUNC_LUTSIZE > 5 ? 1 << (IF_MAX_FUNC_LUTSIZE - 5) : 1];
2219
    If_Grp_t G1 = {0};//, G3 = {0};
2220
    int i, nLutLeaf, nLutLeaf2, nLutRoot, Length;
2221 2222 2223 2224
    // stretch the truth table
    assert( nVars >= 6 );
    memcpy( pTruth, pTruth0, sizeof(word) * Abc_TtWordNum(nVars) );
    Abc_TtStretch6( (word *)pTruth, nLeaves, p->pPars->nLutSize );
Alan Mishchenko committed
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256

#ifdef IF_TRY_NEW
    {
        word pCopy[1024];
        int nWords = Abc_TruthWordNum(nVars);
        int iNum;
        if ( s_vTtMem == NULL )
        {
            s_vTtMem = Vec_MemAlloc( Abc_Truth6WordNum(nVars), 12 ); // 32 KB/page for 6-var functions
            Vec_MemHashAlloc( s_vTtMem, 10000 );
        }
        if ( s_vTtMem2 == NULL )
        {
            s_vTtMem2 = Vec_MemAlloc( Abc_Truth6WordNum(nVars), 12 ); // 32 KB/page for 6-var functions
            Vec_MemHashAlloc( s_vTtMem2, 10000 );
        }
        memcpy( pCopy, pTruth, sizeof(word) * Abc_Truth6WordNum(nVars) );
        if ( pCopy[0] & 1 )
            for ( i = 0; i < nWords; i++ )
                pCopy[i] = ~pCopy[i];
        iNum = Vec_MemHashInsert( s_vTtMem, pCopy );
        if ( iNum == Vec_MemEntryNum(s_vTtMem) - 1 )
        {
            int pCanonPerm[16];
            char pCanonPermC[16];
            Abc_TtCanonicize( pCopy, nVars, pCanonPermC );
//            If_CluSemiCanonicize( pCopy, nVars, pCanonPerm );
            Vec_MemHashInsert( s_vTtMem2, pCopy );
        }
    }
#endif

2257 2258 2259
    // if cutmin is disabled, minimize the function
    if ( !p->pPars->fCutMin )
        nLeaves = Abc_TtMinBase( (word *)pTruth, NULL, nLeaves, nVars );
2260

2261
    // quit if parameters are wrong
2262 2263
    Length = strlen(pStr);
    if ( Length != 2 && Length != 3 )
2264 2265 2266 2267
    {
        printf( "Wrong LUT struct (%s)\n", pStr );
        return 0;
    }
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
    for ( i = 0; i < Length; i++ )
        if ( pStr[i] - '0' < 3 || pStr[i] - '0' > 6 )
        {
            printf( "The LUT size (%d) should belong to {3,4,5,6}.\n", pStr[i] - '0' );
            return 0;
        }

    nLutLeaf  =                   pStr[0] - '0';
    nLutLeaf2 = ( Length == 3 ) ? pStr[1] - '0' : 0;
    nLutRoot  =                   pStr[Length-1] - '0';
    if ( nLeaves > nLutLeaf - 1 + (nLutLeaf2 ? nLutLeaf2 - 1 : 0) + nLutRoot )
2279
    {
2280
        printf( "The cut size (%d) is too large for the LUT structure %s.\n", nLeaves, pStr );
2281 2282 2283
        return 0;
    }
    // consider easy case
2284
    if ( nLeaves <= Abc_MaxInt( nLutLeaf2, Abc_MaxInt(nLutLeaf, nLutRoot) ) )
2285 2286 2287
        return 1;

    // derive the first group
2288
    if ( Length == 2 )
2289
        G1 = If_CluCheck( p, (word *)pTruth, nLeaves, 0, 0, nLutLeaf, nLutRoot, NULL, NULL, NULL, NULL, 1 );
2290 2291
    else
        G1 = If_CluCheck3( p, (word *)pTruth, nLeaves, nLutLeaf, nLutLeaf2, nLutRoot, NULL, NULL, NULL, NULL, NULL );
2292 2293 2294 2295

//    if ( G1.nVars > 0 )
//        If_CluPrintGroup( &G1 );

2296
    return (int)(G1.nVars > 0);
2297 2298 2299
}


2300
// testing procedure
2301
void If_CluTest()
2302 2303
{
//    word t = 0xff00f0f0ccccaaaa;
2304 2305 2306 2307 2308
//    word t = 0xfedcba9876543210;
//    word t = 0xec64000000000000;
//    word t = 0x0100200000000001;
//    word t2[4] = { 0x0000800080008000, 0x8000000000008000, 0x8000000080008000, 0x0000000080008000 };
//    word t = 0x07770FFF07770FFF;
2309
//    word t = 0x002000D000D00020;
2310
//    word t = 0x000F000E000F000F;
2311
//    word t = 0xF7FFF7F7F7F7F7F7;
2312
//    word t = 0x0234AFDE23400BCE;
2313 2314 2315
//    word t = 0x0080008880A088FF;
//    word s = t;
//    word t = 0xFFBBBBFFF0B0B0F0;
2316
    word t = ABC_CONST(0x6DD9926D962D6996);
2317
    int nVars     = 6;
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
    int nLutLeaf  = 4;
    int nLutLeaf2 = 4;
    int nLutRoot  = 4;
/*
    word t2[2] = { 0x7f807f807f80807f, 0x7f807f807f807f80 };
    int nVars     = 7;
    int nLutLeaf  = 3;
    int nLutLeaf2 = 3;
    int nLutRoot  = 3;
*/

2329
    If_Grp_t G;
2330 2331 2332 2333
    If_Grp_t G2, R;
    word Func0, Func1, Func2;


2334
    return;
2335

2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
/*
    int pCanonPerm[CLU_VAR_MAX];
    int uCanonPhase;

    Kit_DsdPrintFromTruth( (unsigned*)&s, nVars ); printf( "\n" );
    uCanonPhase = If_CluSemiCanonicize( &s, nVars, pCanonPerm );
    Kit_DsdPrintFromTruth( (unsigned*)&s, nVars ); printf( "\n" );

    If_CluSemiCanonicizeVerify( &s, &t, nVars, pCanonPerm, uCanonPhase );
*/
2346

2347 2348
    Kit_DsdPrintFromTruth( (unsigned*)&t, nVars ); printf( "\n" );
    G = If_CluCheck3( NULL, &t, nVars, nLutLeaf, nLutLeaf2, nLutRoot, &R, &G2, &Func0, &Func1, &Func2 );
2349 2350 2351 2352
    If_CluPrintGroup( &G );
    If_CluPrintGroup( &G2 );
    If_CluPrintGroup( &R );

2353
//    If_CluVerify3( &t, nVars, &G, &G2, &R, Func1, Func2, Func0 );
2354

2355 2356
    return;

2357
//    If_CutPerformCheck07( NULL, (unsigned *)&t, 6, 6, NULL );
2358 2359
//    return;

2360 2361 2362
//    Kit_DsdPrintFromTruth( (unsigned*)&t, nVars ); printf( "\n" );
//    G = If_CluCheck( NULL, &t, nVars, 0, nLutLeaf, nLutRoot, NULL, NULL, NULL, NULL, 0 );
//    If_CluPrintGroup( &G );
2363 2364
}

2365 2366


2367 2368 2369 2370 2371 2372 2373 2374

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


ABC_NAMESPACE_IMPL_END