utilTruth.h 121 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
/**CFile****************************************************************

  FileName    [utilTruth.h]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Truth table manipulation.]

  Synopsis    [Truth table manipulation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - October 28, 2012.]

  Revision    [$Id: utilTruth.h,v 1.00 2012/10/28 00:00:00 alanmi Exp $]

***********************************************************************/
 
#ifndef ABC__misc__util__utilTruth_h
#define ABC__misc__util__utilTruth_h

////////////////////////////////////////////////////////////////////////
///                          INCLUDES                                ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                         PARAMETERS                               ///
////////////////////////////////////////////////////////////////////////

ABC_NAMESPACE_HEADER_START

////////////////////////////////////////////////////////////////////////
///                         BASIC TYPES                              ///
////////////////////////////////////////////////////////////////////////

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static unsigned s_Truths5[6] = {
    0xAAAAAAAA,
    0xCCCCCCCC,
    0xF0F0F0F0,
    0xFF00FF00,
    0xFFFF0000
};

static unsigned s_Truths5Neg[6] = {
    0x55555555,
    0x33333333,
    0x0F0F0F0F,
    0x00FF00FF,
    0x0000FFFF
};

54
static word s_Truths6[6] = {
55 56 57 58 59 60
    ABC_CONST(0xAAAAAAAAAAAAAAAA),
    ABC_CONST(0xCCCCCCCCCCCCCCCC),
    ABC_CONST(0xF0F0F0F0F0F0F0F0),
    ABC_CONST(0xFF00FF00FF00FF00),
    ABC_CONST(0xFFFF0000FFFF0000),
    ABC_CONST(0xFFFFFFFF00000000)
61 62
};

63
static word s_Truths6Neg[6] = {
64 65 66 67 68 69
    ABC_CONST(0x5555555555555555),
    ABC_CONST(0x3333333333333333),
    ABC_CONST(0x0F0F0F0F0F0F0F0F),
    ABC_CONST(0x00FF00FF00FF00FF),
    ABC_CONST(0x0000FFFF0000FFFF),
    ABC_CONST(0x00000000FFFFFFFF)
70 71
};

Alan Mishchenko committed
72 73 74 75 76 77 78 79 80
static word s_TruthXors[6] = {
    ABC_CONST(0x0000000000000000),
    ABC_CONST(0x6666666666666666),
    ABC_CONST(0x6969696969696969),
    ABC_CONST(0x6996699669966996),
    ABC_CONST(0x6996966969969669),
    ABC_CONST(0x6996966996696996)
};

81
static word s_PMasks[5][3] = {
82 83 84 85 86
    { 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) }
87 88
};

Alan Mishchenko committed
89
static word s_PPMasks[5][6][3] = {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    { 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 0 0  
        { ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) }, // 0 1  
        { ABC_CONST(0xA5A5A5A5A5A5A5A5), ABC_CONST(0x0A0A0A0A0A0A0A0A), ABC_CONST(0x5050505050505050) }, // 0 2 
        { ABC_CONST(0xAA55AA55AA55AA55), ABC_CONST(0x00AA00AA00AA00AA), ABC_CONST(0x5500550055005500) }, // 0 3 
        { ABC_CONST(0xAAAA5555AAAA5555), ABC_CONST(0x0000AAAA0000AAAA), ABC_CONST(0x5555000055550000) }, // 0 4 
        { ABC_CONST(0xAAAAAAAA55555555), ABC_CONST(0x00000000AAAAAAAA), ABC_CONST(0x5555555500000000) }  // 0 5 
    },
    { 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 1 0  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 1 1  
        { ABC_CONST(0xC3C3C3C3C3C3C3C3), ABC_CONST(0x0C0C0C0C0C0C0C0C), ABC_CONST(0x3030303030303030) }, // 1 2 
        { ABC_CONST(0xCC33CC33CC33CC33), ABC_CONST(0x00CC00CC00CC00CC), ABC_CONST(0x3300330033003300) }, // 1 3 
        { ABC_CONST(0xCCCC3333CCCC3333), ABC_CONST(0x0000CCCC0000CCCC), ABC_CONST(0x3333000033330000) }, // 1 4 
        { ABC_CONST(0xCCCCCCCC33333333), ABC_CONST(0x00000000CCCCCCCC), ABC_CONST(0x3333333300000000) }  // 1 5 
    },
    { 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 2 0  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 2 1  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 2 2 
        { ABC_CONST(0xF00FF00FF00FF00F), ABC_CONST(0x00F000F000F000F0), ABC_CONST(0x0F000F000F000F00) }, // 2 3 
        { ABC_CONST(0xF0F00F0FF0F00F0F), ABC_CONST(0x0000F0F00000F0F0), ABC_CONST(0x0F0F00000F0F0000) }, // 2 4 
        { ABC_CONST(0xF0F0F0F00F0F0F0F), ABC_CONST(0x00000000F0F0F0F0), ABC_CONST(0x0F0F0F0F00000000) }  // 2 5 
    },
    { 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 3 0  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 3 1  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 3 2 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 3 3 
        { ABC_CONST(0xFF0000FFFF0000FF), ABC_CONST(0x0000FF000000FF00), ABC_CONST(0x00FF000000FF0000) }, // 3 4 
        { ABC_CONST(0xFF00FF0000FF00FF), ABC_CONST(0x00000000FF00FF00), ABC_CONST(0x00FF00FF00000000) }  // 3 5 
    },
    { 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 4 0  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 4 1  
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 4 2 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 4 3 
        { ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000), ABC_CONST(0x0000000000000000) }, // 4 4 
        { ABC_CONST(0xFFFF00000000FFFF), ABC_CONST(0x00000000FFFF0000), ABC_CONST(0x0000FFFF00000000) }  // 4 5 
    }
};

132 133 134 135 136 137 138 139 140 141 142 143 144
// the bit count for the first 256 integer numbers
static int Abc_TtBitCount8[256] = {
    0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
    1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
    1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
    2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
    1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
    2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
    2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
    3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
};
static inline int Abc_TtBitCount16( int i ) { return Abc_TtBitCount8[i & 0xFF] + Abc_TtBitCount8[i >> 8]; }

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
////////////////////////////////////////////////////////////////////////
///                      MACRO DEFINITIONS                           ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                    FUNCTION DECLARATIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
164
// read/write/flip i-th bit of a bit string table:
165 166 167
static inline int     Abc_TtGetBit( word * p, int i )         { return (int)(p[i>>6] >> (word)(i & 63)) & 1;        }
static inline void    Abc_TtSetBit( word * p, int i )         { p[i>>6] |= (word)(((word)1)<<(i & 63));             }
static inline void    Abc_TtXorBit( word * p, int i )         { p[i>>6] ^= (word)(((word)1)<<(i & 63));             }
Alan Mishchenko committed
168

169
// read/write k-th digit d of a quaternary number:
170 171 172
static inline int     Abc_TtGetQua( word * p, int k )         { return (int)(p[k>>5] >> (word)((k<<1) & 63)) & 3;   }
static inline void    Abc_TtSetQua( word * p, int k, int d )  { p[k>>5] |= (word)(((word)d)<<((k<<1) & 63));        }
static inline void    Abc_TtXorQua( word * p, int k, int d )  { p[k>>5] ^= (word)(((word)d)<<((k<<1) & 63));        }
173

Alan Mishchenko committed
174
// read/write k-th digit d of a hexadecimal number:
175 176 177
static inline int     Abc_TtGetHex( word * p, int k )         { return (int)(p[k>>4] >> (word)((k<<2) & 63)) & 15;  }
static inline void    Abc_TtSetHex( word * p, int k, int d )  { p[k>>4] |= (word)(((word)d)<<((k<<2) & 63));        }
static inline void    Abc_TtXorHex( word * p, int k, int d )  { p[k>>4] ^= (word)(((word)d)<<((k<<2) & 63));        }
Alan Mishchenko committed
178

179
// read/write k-th digit d of a 256-base number:
180 181 182
static inline int     Abc_TtGet256( word * p, int k )         { return (int)(p[k>>3] >> (word)((k<<3) & 63)) & 255; }
static inline void    Abc_TtSet256( word * p, int k, int d )  { p[k>>3] |= (word)(((word)d)<<((k<<3) & 63));        }
static inline void    Abc_TtXor256( word * p, int k, int d )  { p[k>>3] ^= (word)(((word)d)<<((k<<3) & 63));        }
183

Alan Mishchenko committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int  Abc_TtWordNum( int nVars )     { return nVars <= 6 ? 1 : 1 << (nVars-6); }
static inline int  Abc_TtByteNum( int nVars )     { return nVars <= 3 ? 1 : 1 << (nVars-3); }
static inline int  Abc_TtHexDigitNum( int nVars ) { return nVars <= 2 ? 1 : 1 << (nVars-2); }
198 199 200

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

Alan Mishchenko committed
201 202 203 204 205 206 207 208 209 210
  Synopsis    [Bit mask.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word Abc_Tt6Mask( int nBits )       { assert( nBits >= 0 && nBits <= 64 ); return (~(word)0) >> (64-nBits);        }
211 212 213 214 215 216 217 218 219 220 221 222
static inline void Abc_TtMask( word * pTruth, int nWords, int nBits )
{ 
    int w;
    assert( nBits >= 0 && nBits <= nWords * 64 );
    for ( w = 0; w < nWords; w++ )
        if ( nBits >= (w + 1) * 64 )
            pTruth[w] = ~(word)0;
        else if ( nBits > w * 64 )
            pTruth[w] = Abc_Tt6Mask( nBits - w * 64 );
        else
            pTruth[w] = 0;
}
Alan Mishchenko committed
223 224 225

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

226 227 228 229 230 231 232 233 234
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
235 236 237 238 239 240
static inline void Abc_TtVec( word * pOut, int nWords, word Entry )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = Entry;
}
241 242 243 244 245 246
static inline void Abc_TtConst( word * pOut, int nWords, int fConst1 )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = fConst1 ? ~(word)0 : 0;
}
Alan Mishchenko committed
247 248 249 250 251 252 253 254 255 256 257 258
static inline void Abc_TtClear( word * pOut, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = 0;
}
static inline void Abc_TtFill( word * pOut, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = ~(word)0;
}
259
static inline void Abc_TtUnit( word * pOut, int nWords, int fCompl )
Alan Mishchenko committed
260 261 262
{
    int w;
    for ( w = 0; w < nWords; w++ )
263
        pOut[w] = fCompl ? ~s_Truths6[0] : s_Truths6[0];
Alan Mishchenko committed
264
}
265 266 267 268 269 270
static inline void Abc_TtNot( word * pOut, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = ~pOut[w];
}
271 272 273 274 275 276 277 278 279 280
static inline void Abc_TtCopy( word * pOut, word * pIn, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
            pOut[w] = ~pIn[w];
    else
        for ( w = 0; w < nWords; w++ )
            pOut[w] = pIn[w];
}
281 282 283 284 285 286
static inline word * Abc_TtDup( word * pIn, int nWords, int fCompl )
{
    word * pOut = ABC_ALLOC( word, nWords );
    Abc_TtCopy( pOut, pIn, nWords, fCompl );
    return pOut;
}
287 288 289 290 291 292 293 294 295 296
static inline void Abc_TtAnd( word * pOut, word * pIn1, word * pIn2, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
            pOut[w] = ~(pIn1[w] & pIn2[w]);
    else
        for ( w = 0; w < nWords; w++ )
            pOut[w] = pIn1[w] & pIn2[w];
}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static inline void Abc_TtAndCompl( word * pOut, word * pIn1, int fCompl1, word * pIn2, int fCompl2, int nWords )
{
    int w;
    if ( fCompl1 )
    {
        if ( fCompl2 )
            for ( w = 0; w < nWords; w++ )
                pOut[w] = ~pIn1[w] & ~pIn2[w];
        else
            for ( w = 0; w < nWords; w++ )
                pOut[w] = ~pIn1[w] & pIn2[w];
    }
    else
    {
        if ( fCompl2 )
            for ( w = 0; w < nWords; w++ )
                pOut[w] = pIn1[w] & ~pIn2[w];
        else
            for ( w = 0; w < nWords; w++ )
                pOut[w] = pIn1[w] & pIn2[w];
    }
}
319 320 321 322 323 324 325 326 327 328
static inline void Abc_TtAndSharp( word * pOut, word * pIn1, word * pIn2, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
            pOut[w] = pIn1[w] & ~pIn2[w];
    else
        for ( w = 0; w < nWords; w++ )
            pOut[w] = pIn1[w] & pIn2[w];
}
Alan Mishchenko committed
329 330 331 332 333 334 335 336 337 338 339 340
static inline void Abc_TtSharp( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = pIn1[w] & ~pIn2[w];
}
static inline void Abc_TtOr( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = pIn1[w] | pIn2[w];
}
341 342 343 344 345 346
static inline void Abc_TtOrXor( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] |= pIn1[w] ^ pIn2[w];
}
Alan Mishchenko committed
347 348 349 350 351 352
static inline void Abc_TtAndXor( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] &= pIn1[w] ^ pIn2[w];
}
353 354 355 356 357 358
static inline void Abc_TtOrAnd( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] |= pIn1[w] & pIn2[w];
}
Alan Mishchenko committed
359 360 361 362 363 364
static inline void Abc_TtSharpOr( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = (pOut[w] & ~pIn1[w]) | pIn2[w];
}
Alan Mishchenko committed
365 366 367 368 369 370 371 372 373 374
static inline void Abc_TtXor( word * pOut, word * pIn1, word * pIn2, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
            pOut[w] = pIn1[w] ^ ~pIn2[w];
    else
        for ( w = 0; w < nWords; w++ )
            pOut[w] = pIn1[w] ^ pIn2[w];
}
Alan Mishchenko committed
375 376 377 378 379 380 381 382 383 384
static inline void Abc_TtXorMask( word * pOut, word * pIn1, word * pIn2, word * pMask, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
            pOut[w] = (pIn1[w] ^ pIn2[w]) & ~pMask[w];
    else
        for ( w = 0; w < nWords; w++ )
            pOut[w] = (pIn1[w] ^ pIn2[w]) & pMask[w];
}
Alan Mishchenko committed
385 386 387 388 389 390
static inline void Abc_TtMux( word * pOut, word * pCtrl, word * pIn1, word * pIn0, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = (pCtrl[w] & pIn1[w]) | (~pCtrl[w] & pIn0[w]);
}
391 392 393 394 395 396
static inline void Abc_TtMaj( word * pOut, word * pIn0, word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pOut[w] = (pIn0[w] & pIn1[w]) | (pIn0[w] & pIn2[w]) | (pIn1[w] & pIn2[w]);
}
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
static inline int Abc_TtIntersect( word * pIn1, word * pIn2, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
    {
        for ( w = 0; w < nWords; w++ )
            if ( ~pIn1[w] & pIn2[w] )
                return 1;
    }
    else
    {
        for ( w = 0; w < nWords; w++ )
            if ( pIn1[w] & pIn2[w] )
                return 1;
    }
    return 0;
}
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
static inline int Abc_TtIntersectCare( word * pIn1, word * pIn2, word * pCare, int nWords, int fCompl )
{
    int w;
    if ( fCompl )
    {
        for ( w = 0; w < nWords; w++ )
            if ( ~pIn1[w] & pIn2[w] & pCare[w] )
                return 1;
    }
    else
    {
        for ( w = 0; w < nWords; w++ )
            if ( pIn1[w] & pIn2[w] & pCare[w] )
                return 1;
    }
    return 0;
}static inline int Abc_TtIntersectOne( word * pOut, int fComp, word * pIn, int fComp0, int nWords )
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 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
{
    int w;
    if ( fComp0 )
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~pIn[w] & ~pOut[w] )
                    return 1;
        }
        else
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~pIn[w] & pOut[w] )
                    return 1;
        }
    }
    else
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( pIn[w] & ~pOut[w] )
                    return 1;
        }
        else
        {
            for ( w = 0; w < nWords; w++ )
                if ( pIn[w] & pOut[w] )
                    return 1;
        }
    }
    return 0;
}
static inline int Abc_TtIntersectTwo( word * pOut, int fComp, word * pIn0, int fComp0, word * pIn1, int fComp1, int nWords )
{
    int w;
    if ( fComp0 && fComp1 )
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~pIn0[w] & ~pIn1[w] & ~pOut[w] )
                    return 1;
        }
        else
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~pIn0[w] & ~pIn1[w] & pOut[w] )
                    return 1;
        }
    }
    else if ( fComp0 )
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~pIn0[w] & pIn1[w] & ~pOut[w] )
                    return 1;
        }
        else
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~pIn0[w] & pIn1[w] & pOut[w] )
                    return 1;
        }
    }
    else if ( fComp1 )
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( pIn0[w] & ~pIn1[w] & ~pOut[w] )
                    return 1;
        }
        else
        {
            for ( w = 0; w < nWords; w++ )
                if ( pIn0[w] & ~pIn1[w] & pOut[w] )
                    return 1;
        }
    }
    else 
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( pIn0[w] & pIn1[w] & ~pOut[w] )
                    return 1;
        }
        else
        {
            for ( w = 0; w < nWords; w++ )
                if ( pIn0[w] & pIn1[w] & pOut[w] )
                    return 1;
        }
    }
    return 0;
}
static inline int Abc_TtIntersectXor( word * pOut, int fComp, word * pIn0, word * pIn1, int fComp01, int nWords )
{
    int w;
    if ( fComp01 )
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~(pIn0[w] ^ pIn1[w]) & ~pOut[w] )
                    return 1;
        }
        else 
        {
            for ( w = 0; w < nWords; w++ )
                if ( ~(pIn0[w] ^ pIn1[w]) & pOut[w] )
                    return 1;
        }
    }
    else
    {
        if ( fComp )
        {
            for ( w = 0; w < nWords; w++ )
                if ( (pIn0[w] ^ pIn1[w]) & ~pOut[w] )
                    return 1;
        }
        else 
        {
            for ( w = 0; w < nWords; w++ )
                if ( (pIn0[w] ^ pIn1[w]) & pOut[w] )
                    return 1;
        }
    }
    return 0;
}
565 566 567 568 569 570 571
static inline int Abc_TtEqual( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] != pIn2[w] )
            return 0;
    return 1;
572
}
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static inline int Abc_TtEqualCare( word * pIn1, word * pIn2, word * pCare, int fComp, int nWords )
{
    int w;
    if ( fComp )
    {
        for ( w = 0; w < nWords; w++ )
            if ( (~pIn1[w] ^ pIn2[w]) & pCare[w] )
                return 0;
    }
    else
    {
        for ( w = 0; w < nWords; w++ )
            if ( (pIn1[w] ^ pIn2[w]) & pCare[w] )
                return 0;
    }
    return 1;
}
590 591 592 593 594 595 596 597
static inline int Abc_TtOpposite( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] != ~pIn2[w] )
            return 0;
    return 1;
}
598 599 600 601 602 603 604
static inline int Abc_TtImply( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( (pIn1[w] & pIn2[w]) != pIn1[w] )
            return 0;
    return 1;
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
}
static inline int Abc_TtCompare( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] != pIn2[w] )
            return (pIn1[w] < pIn2[w]) ? -1 : 1;
    return 0;
}
static inline int Abc_TtCompareRev( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = nWords - 1; w >= 0; w-- )
        if ( pIn1[w] != pIn2[w] )
            return (pIn1[w] < pIn2[w]) ? -1 : 1;
    return 0;
}
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
static inline int Abc_TtIsConst0( word * pIn1, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] )
            return 0;
    return 1;
}
static inline int Abc_TtIsConst1( word * pIn1, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( ~pIn1[w] )
            return 0;
    return 1;
}
Alan Mishchenko committed
638 639 640 641 642 643 644 645 646 647 648 649
static inline void Abc_TtConst0( word * pIn1, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pIn1[w] = 0;
}
static inline void Abc_TtConst1( word * pIn1, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        pIn1[w] = ~(word)0;
}
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
static inline void Abc_TtIthVar( word * pOut, int iVar, int nVars )
{
    int k, nWords = Abc_TtWordNum( nVars );
    if ( iVar < 6 )
    {
        for ( k = 0; k < nWords; k++ )
            pOut[k] = s_Truths6[iVar];
    }
    else
    {
        for ( k = 0; k < nWords; k++ )
            if ( k & (1 << (iVar-6)) )
                pOut[k] = ~(word)0;
            else
                pOut[k] = 0;
    }
}
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 717 718 719 720 721 722 723 724 725
static inline void Abc_TtTruth2( word * pOut, word * pIn0, word * pIn1, int Truth, int nWords )
{
    int w;
    assert( Truth >= 0 && Truth <= 0xF );
    switch ( Truth )
    {
        case 0x0 : for ( w = 0; w < nWords; w++ ) pOut[w] =  0;                  break; // 0000
        case 0x1 : for ( w = 0; w < nWords; w++ ) pOut[w] = ~pIn1[w] & ~pIn0[w]; break; // 0001
        case 0x2 : for ( w = 0; w < nWords; w++ ) pOut[w] = ~pIn1[w] &  pIn0[w]; break; // 0010
        case 0x3 : for ( w = 0; w < nWords; w++ ) pOut[w] = ~pIn1[w]           ; break; // 0011
        case 0x4 : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w] & ~pIn0[w]; break; // 0100
        case 0x5 : for ( w = 0; w < nWords; w++ ) pOut[w] =            ~pIn0[w]; break; // 0101
        case 0x6 : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w] ^  pIn0[w]; break; // 0110
        case 0x7 : for ( w = 0; w < nWords; w++ ) pOut[w] = ~pIn1[w] | ~pIn0[w]; break; // 0111
        case 0x8 : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w] &  pIn0[w]; break; // 1000
        case 0x9 : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w] ^ ~pIn0[w]; break; // 1001
        case 0xA : for ( w = 0; w < nWords; w++ ) pOut[w] =             pIn0[w]; break; // 1010
        case 0xB : for ( w = 0; w < nWords; w++ ) pOut[w] = ~pIn1[w] |  pIn0[w]; break; // 1011
        case 0xC : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w]           ; break; // 1100
        case 0xD : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w] | ~pIn0[w]; break; // 1101
        case 0xE : for ( w = 0; w < nWords; w++ ) pOut[w] =  pIn1[w] |  pIn0[w]; break; // 1110
        case 0xF : for ( w = 0; w < nWords; w++ ) pOut[w] = ~(word)0;            break; // 1111
        default  : assert( 0 );
    }
}
static inline void Abc_TtTruth4( word Entry, word ** pNodes, word * pOut, int nWords, int fCompl )
{
    unsigned First  = (unsigned)Entry;
    unsigned Second = (unsigned)(Entry >> 32);
    int i, k = 5;
    for ( i = 0; i < 4; i++ )
    {
        int Lit0, Lit1, Pair = (First >> (i*8)) & 0xFF;
        if ( Pair == 0 )
            break;
        Lit0 = Pair & 0xF;
        Lit1 = Pair >> 4;
        assert( Lit0 != Lit1 );
        if ( Lit0 < Lit1 )
            Abc_TtAndCompl( pNodes[k++], pNodes[Lit0 >> 1], Lit0 & 1, pNodes[Lit1 >> 1], Lit1 & 1, nWords );
        else
            Abc_TtXor( pNodes[k++], pNodes[Lit0 >> 1], pNodes[Lit1 >> 1], nWords, (Lit0 & 1) ^ (Lit1 & 1) );
    }
    for ( i = 0; i < 3; i++ )
    {
        int Lit0, Lit1, Pair = (Second >> (i*10)) & 0x3FF;
        if ( Pair == 0 )
            break;
        Lit0 = Pair & 0x1F;
        Lit1 = Pair >> 5;
        assert( Lit0 != Lit1 );
        if ( Lit0 < Lit1 )
            Abc_TtAndCompl( pNodes[k++], pNodes[Lit0 >> 1], Lit0 & 1, pNodes[Lit1 >> 1], Lit1 & 1, nWords );
        else
            Abc_TtXor( pNodes[k++], pNodes[Lit0 >> 1], pNodes[Lit1 >> 1], nWords, (Lit0 & 1) ^ (Lit1 & 1) );
    }
    assert( k > 5 );
    Abc_TtCopy( pOut, pNodes[k-1], nWords, (int)(Entry >> 62) ^ fCompl );
}
726

727 728
/**Function*************************************************************

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtIsAndCompl( word * pOut, int fCompl, word * pIn1, int fCompl1, word * pIn2, int fCompl2, word * pCare, int nWords )
{
    int w;
    if ( fCompl )
    {
        if ( fCompl1 )
        {
            if ( fCompl2 )
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (~pOut[w] & pCare[w]) != (~pIn1[w] & ~pIn2[w] & pCare[w]) )
                        return 0;
            }
            else
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (~pOut[w] & pCare[w]) != (~pIn1[w] & pIn2[w] & pCare[w]) )
                        return 0;
            }
        }
        else
        {
            if ( fCompl2 )
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (~pOut[w] & pCare[w]) != (pIn1[w] & ~pIn2[w] & pCare[w]) )
                        return 0;
            }
            else
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (~pOut[w] & pCare[w]) != (pIn1[w] & pIn2[w] & pCare[w]) )
                        return 0;
            }
        }
    }
    else
    {
        if ( fCompl1 )
        {
            if ( fCompl2 )
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (pOut[w] & pCare[w]) != (~pIn1[w] & ~pIn2[w] & pCare[w]) )
                        return 0;
            }
            else
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (pOut[w] & pCare[w]) != (~pIn1[w] & pIn2[w] & pCare[w]) )
                        return 0;
            }
        }
        else
        {
            if ( fCompl2 )
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (pOut[w] & pCare[w]) != (pIn1[w] & ~pIn2[w] & pCare[w]) )
                        return 0;
            }
            else
            {
                for ( w = 0; w < nWords; w++ )
                    if ( (pOut[w] & pCare[w]) != (pIn1[w] & pIn2[w] & pCare[w]) )
                        return 0;
            }
        }
    }
    return 1;
}

static inline int Abc_TtIsXorCompl( word * pOut, int fCompl, word * pIn1, word * pIn2, word * pCare, int nWords )
{
    int w;
    if ( fCompl )
    {
        for ( w = 0; w < nWords; w++ )
            if ( (~pOut[w] & pCare[w]) != ((pIn1[w] ^ pIn2[w]) & pCare[w]) )
                return 0;
    }
    else
    {
        for ( w = 0; w < nWords; w++ )
            if ( ( pOut[w] & pCare[w]) != ((pIn1[w] ^ pIn2[w]) & pCare[w]) )
                return 0;
    }
    return 1;
}


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

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 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
  Synopsis    [Compares Cof0 and Cof1.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtCompare1VarCofs( word * pTruth, int nWords, int iVar )
{
    if ( nWords == 1 )
    {
        word Cof0 = pTruth[0] & s_Truths6Neg[iVar];
        word Cof1 = (pTruth[0] >> (1 << iVar)) & s_Truths6Neg[iVar];
        if ( Cof0 != Cof1 )
            return Cof0 < Cof1 ? -1 : 1;
        return 0;
    }
    if ( iVar <= 5 )
    {
        word Cof0, Cof1;
        int w, shift = (1 << iVar);
        for ( w = 0; w < nWords; w++ )
        {
            Cof0 = pTruth[w] & s_Truths6Neg[iVar];
            Cof1 = (pTruth[w] >> shift) & s_Truths6Neg[iVar];
            if ( Cof0 != Cof1 )
                return Cof0 < Cof1 ? -1 : 1;
        }
        return 0;
    }
    // if ( iVar > 5 )
    {
        word * pLimit = pTruth + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        assert( nWords >= 2 );
        for ( ; pTruth < pLimit; pTruth += 2*iStep )
            for ( i = 0; i < iStep; i++ )
                if ( pTruth[i] != pTruth[i + iStep] )
                    return pTruth[i] < pTruth[i + iStep] ? -1 : 1;
        return 0;
    }    
}
static inline int Abc_TtCompare1VarCofsRev( word * pTruth, int nWords, int iVar )
{
    if ( nWords == 1 )
    {
        word Cof0 = pTruth[0] & s_Truths6Neg[iVar];
        word Cof1 = (pTruth[0] >> (1 << iVar)) & s_Truths6Neg[iVar];
        if ( Cof0 != Cof1 )
            return Cof0 < Cof1 ? -1 : 1;
        return 0;
    }
    if ( iVar <= 5 )
    {
        word Cof0, Cof1;
        int w, shift = (1 << iVar);
        for ( w = nWords - 1; w >= 0; w-- )
        {
            Cof0 = pTruth[w] & s_Truths6Neg[iVar];
            Cof1 = (pTruth[w] >> shift) & s_Truths6Neg[iVar];
            if ( Cof0 != Cof1 )
                return Cof0 < Cof1 ? -1 : 1;
        }
        return 0;
    }
    // if ( iVar > 5 )
    {
        word * pLimit = pTruth + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        assert( nWords >= 2 );
        for ( pLimit -= 2*iStep; pLimit >= pTruth; pLimit -= 2*iStep )
            for ( i = iStep - 1; i >= 0; i-- )
                if ( pLimit[i] != pLimit[i + iStep] )
                    return pLimit[i] < pLimit[i + iStep] ? -1 : 1;
        return 0;
    }    
}
Alan Mishchenko committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

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

  Synopsis    [Compute elementary truth tables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_TtElemInit( word ** pTtElems, int nVars )
{
    int i, k, nWords = Abc_TtWordNum( nVars );
    for ( i = 0; i < nVars; i++ )
        if ( i < 6 )
            for ( k = 0; k < nWords; k++ )
                pTtElems[i][k] = s_Truths6[i];
        else
            for ( k = 0; k < nWords; k++ )
                pTtElems[i][k] = (k & (1 << (i-6))) ? ~(word)0 : 0;
}
933 934 935 936 937 938 939 940 941 942 943 944 945 946
static inline void Abc_TtElemInit2( word * pTtElems, int nVars )
{
    int i, k, nWords = Abc_TtWordNum( nVars );
    for ( i = 0; i < nVars; i++ )
    {
        word * pTruth = pTtElems + i * nWords;
        if ( i < 6 )
            for ( k = 0; k < nWords; k++ )
                pTruth[k] = s_Truths6[i];
        else
            for ( k = 0; k < nWords; k++ )
                pTruth[k] = (k & (1 << (i-6))) ? ~(word)0 : 0;
    }
}
Alan Mishchenko committed
947

948
/**Function*************************************************************
949 950 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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_Tt5HasVar( unsigned t, int iVar )
{
    return ((t << (1<<iVar)) & s_Truths5[iVar]) != (t & s_Truths5[iVar]);
}
static inline unsigned Abc_Tt5Cofactor0( unsigned t, int iVar )
{
    assert( iVar >= 0 && iVar < 5 );
    return (t &s_Truths5Neg[iVar]) | ((t &s_Truths5Neg[iVar]) << (1<<iVar));
}
static inline unsigned Abc_Tt5Cofactor1( unsigned t, int iVar )
{
    assert( iVar >= 0 && iVar < 5 );
    return (t & s_Truths5[iVar]) | ((t & s_Truths5[iVar]) >> (1<<iVar));
}


/**Function*************************************************************
976

977
  Synopsis    []
978 979 980 981 982 983 984 985

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
986 987 988 989 990 991 992 993 994 995 996
static inline word Abc_Tt6Cofactor0( word t, int iVar )
{
    assert( iVar >= 0 && iVar < 6 );
    return (t &s_Truths6Neg[iVar]) | ((t &s_Truths6Neg[iVar]) << (1<<iVar));
}
static inline word Abc_Tt6Cofactor1( word t, int iVar )
{
    assert( iVar >= 0 && iVar < 6 );
    return (t & s_Truths6[iVar]) | ((t & s_Truths6[iVar]) >> (1<<iVar));
}

Alan Mishchenko committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
static inline void Abc_TtCofactor0p( word * pOut, word * pIn, int nWords, int iVar )
{
    if ( nWords == 1 )
        pOut[0] = ((pIn[0] & s_Truths6Neg[iVar]) << (1 << iVar)) | (pIn[0] & s_Truths6Neg[iVar]);
    else if ( iVar <= 5 )
    {
        int w, shift = (1 << iVar);
        for ( w = 0; w < nWords; w++ )
            pOut[w] = ((pIn[w] & s_Truths6Neg[iVar]) << shift) | (pIn[w] & s_Truths6Neg[iVar]);
    }
    else // if ( iVar > 5 )
    {
        word * pLimit = pIn + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        for ( ; pIn < pLimit; pIn += 2*iStep, pOut += 2*iStep )
            for ( i = 0; i < iStep; i++ )
            {
                pOut[i]         = pIn[i];
                pOut[i + iStep] = pIn[i];
            }
    }    
}
static inline void Abc_TtCofactor1p( word * pOut, word * pIn, int nWords, int iVar )
{
    if ( nWords == 1 )
        pOut[0] = (pIn[0] & s_Truths6[iVar]) | ((pIn[0] & s_Truths6[iVar]) >> (1 << iVar));
    else if ( iVar <= 5 )
    {
        int w, shift = (1 << iVar);
        for ( w = 0; w < nWords; w++ )
            pOut[w] = (pIn[w] & s_Truths6[iVar]) | ((pIn[w] & s_Truths6[iVar]) >> shift);
    }
    else // if ( iVar > 5 )
    {
        word * pLimit = pIn + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        for ( ; pIn < pLimit; pIn += 2*iStep, pOut += 2*iStep )
            for ( i = 0; i < iStep; i++ )
            {
                pOut[i]         = pIn[i + iStep];
                pOut[i + iStep] = pIn[i + iStep];
            }
    }    
}
1041
static inline void Abc_TtCofactor0( word * pTruth, int nWords, int iVar )
1042 1043
{
    if ( nWords == 1 )
1044 1045
        pTruth[0] = ((pTruth[0] & s_Truths6Neg[iVar]) << (1 << iVar)) | (pTruth[0] & s_Truths6Neg[iVar]);
    else if ( iVar <= 5 )
1046 1047 1048
    {
        int w, shift = (1 << iVar);
        for ( w = 0; w < nWords; w++ )
1049
            pTruth[w] = ((pTruth[w] & s_Truths6Neg[iVar]) << shift) | (pTruth[w] & s_Truths6Neg[iVar]);
1050
    }
1051
    else // if ( iVar > 5 )
1052 1053 1054 1055 1056
    {
        word * pLimit = pTruth + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        for ( ; pTruth < pLimit; pTruth += 2*iStep )
            for ( i = 0; i < iStep; i++ )
1057
                pTruth[i + iStep] = pTruth[i];
Alan Mishchenko committed
1058
    }
1059
}
1060
static inline void Abc_TtCofactor1( word * pTruth, int nWords, int iVar )
1061 1062
{
    if ( nWords == 1 )
1063 1064
        pTruth[0] = (pTruth[0] & s_Truths6[iVar]) | ((pTruth[0] & s_Truths6[iVar]) >> (1 << iVar));
    else if ( iVar <= 5 )
1065 1066 1067
    {
        int w, shift = (1 << iVar);
        for ( w = 0; w < nWords; w++ )
1068
            pTruth[w] = (pTruth[w] & s_Truths6[iVar]) | ((pTruth[w] & s_Truths6[iVar]) >> shift);
1069
    }
1070
    else // if ( iVar > 5 )
1071 1072 1073
    {
        word * pLimit = pTruth + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
1074
        for ( ; pTruth < pLimit; pTruth += 2*iStep )
1075
            for ( i = 0; i < iStep; i++ )
1076
                pTruth[i] = pTruth[i + iStep];
Alan Mishchenko committed
1077
    }
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 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
}

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

  Synopsis    [Checks pairs of cofactors w.r.t. two variables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtCheckEqualCofs( word * pTruth, int nWords, int iVar, int jVar, int Num1, int Num2 )
{
    assert( Num1 < Num2 && Num2 < 4 );
    assert( iVar < jVar );
    if ( nWords == 1 )
    {
        word Mask = s_Truths6Neg[jVar] & s_Truths6Neg[iVar];
        int shift1 = (Num1 >> 1) * (1 << jVar) + (Num1 & 1) * (1 << iVar);
        int shift2 = (Num2 >> 1) * (1 << jVar) + (Num2 & 1) * (1 << iVar);
        return ((pTruth[0] >> shift1) & Mask) == ((pTruth[0] >> shift2) & Mask);
    }
    if ( jVar <= 5 )
    {
        word Mask = s_Truths6Neg[jVar] & s_Truths6Neg[iVar];
        int shift1 = (Num1 >> 1) * (1 << jVar) + (Num1 & 1) * (1 << iVar);
        int shift2 = (Num2 >> 1) * (1 << jVar) + (Num2 & 1) * (1 << iVar);
        int w;
        for ( w = 0; w < nWords; w++ )
            if ( ((pTruth[w] >> shift1) & Mask) != ((pTruth[w] >> shift2) & Mask) )
                return 0;
        return 1;
    }
    if ( iVar <= 5 && jVar > 5 )
    {
        word * pLimit = pTruth + nWords;
        int j, jStep = Abc_TtWordNum(jVar);
        int shift1 = (Num1 & 1) * (1 << iVar);
        int shift2 = (Num2 & 1) * (1 << iVar);
        int Offset1 = (Num1 >> 1) * jStep;
        int Offset2 = (Num2 >> 1) * jStep;
        for ( ; pTruth < pLimit; pTruth += 2*jStep )
            for ( j = 0; j < jStep; j++ )
                if ( ((pTruth[j + Offset1] >> shift1) & s_Truths6Neg[iVar]) != ((pTruth[j + Offset2] >> shift2) & s_Truths6Neg[iVar]) )
                    return 0;
        return 1;
    }
    {
        word * pLimit = pTruth + nWords;
        int j, jStep = Abc_TtWordNum(jVar);
        int i, iStep = Abc_TtWordNum(iVar);
        int Offset1 = (Num1 >> 1) * jStep + (Num1 & 1) * iStep;
        int Offset2 = (Num2 >> 1) * jStep + (Num2 & 1) * iStep;
        for ( ; pTruth < pLimit; pTruth += 2*jStep )
            for ( i = 0; i < jStep; i += 2*iStep )
                for ( j = 0; j < iStep; j++ )
                    if ( pTruth[Offset1 + i + j] != pTruth[Offset2 + i + j] )
                        return 0;
        return 1;
    }    
}

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1154 1155 1156 1157
static inline int Abc_Tt6Cof0IsConst0( word t, int iVar ) { return (t & s_Truths6Neg[iVar]) == 0;                                          }
static inline int Abc_Tt6Cof0IsConst1( word t, int iVar ) { return (t & s_Truths6Neg[iVar]) == s_Truths6Neg[iVar];                         }
static inline int Abc_Tt6Cof1IsConst0( word t, int iVar ) { return (t & s_Truths6[iVar]) == 0;                                             }
static inline int Abc_Tt6Cof1IsConst1( word t, int iVar ) { return (t & s_Truths6[iVar]) == s_Truths6[iVar];                               }
Alan Mishchenko committed
1158 1159 1160 1161
static inline int Abc_Tt6CofsOpposite( word t, int iVar ) { return (~t & s_Truths6Neg[iVar]) == ((t >> (1 << iVar)) & s_Truths6Neg[iVar]); } 
static inline int Abc_Tt6Cof0EqualCof1( word t1, word t2, int iVar ) { return (t1 & s_Truths6Neg[iVar]) == ((t2 >> (1 << iVar)) & s_Truths6Neg[iVar]); } 
static inline int Abc_Tt6Cof0EqualCof0( word t1, word t2, int iVar ) { return (t1 & s_Truths6Neg[iVar]) == (t2 & s_Truths6Neg[iVar]); } 
static inline int Abc_Tt6Cof1EqualCof1( word t1, word t2, int iVar ) { return (t1 & s_Truths6[iVar])    == (t2 & s_Truths6[iVar]); } 
1162

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtTruthIsConst0( word * p, int nWords ) { int w; for ( w = 0; w < nWords; w++ ) if ( p[w] != 0        ) return 0; return 1; }
static inline int Abc_TtTruthIsConst1( word * p, int nWords ) { int w; for ( w = 0; w < nWords; w++ ) if ( p[w] != ~(word)0 ) return 0; return 1; }

static inline int Abc_TtCof0IsConst0( word * t, int nWords, int iVar ) 
{ 
    if ( iVar < 6 )
    {
        int i;
        for ( i = 0; i < nWords; i++ )
            if ( t[i] & s_Truths6Neg[iVar] )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( t[i] )
                    return 0;
        return 1;
    }
}
static inline int Abc_TtCof0IsConst1( word * t, int nWords, int iVar ) 
{ 
    if ( iVar < 6 )
    {
        int i;
        for ( i = 0; i < nWords; i++ )
            if ( (t[i] & s_Truths6Neg[iVar]) != s_Truths6Neg[iVar] )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( ~t[i] )
                    return 0;
        return 1;
    }
}
static inline int Abc_TtCof1IsConst0( word * t, int nWords, int iVar ) 
{ 
    if ( iVar < 6 )
    {
        int i;
        for ( i = 0; i < nWords; i++ )
            if ( t[i] & s_Truths6[iVar] )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( t[i+Step] )
                    return 0;
        return 1;
    }
}
static inline int Abc_TtCof1IsConst1( word * t, int nWords, int iVar ) 
{ 
    if ( iVar < 6 )
    {
        int i;
        for ( i = 0; i < nWords; i++ )
            if ( (t[i] & s_Truths6[iVar]) != s_Truths6[iVar] )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( ~t[i+Step] )
                    return 0;
        return 1;
    }
}
static inline int Abc_TtCofsOpposite( word * t, int nWords, int iVar ) 
{ 
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            if ( ((t[i] << Shift) & s_Truths6[iVar]) != (~t[i] & s_Truths6[iVar]) )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( t[i] != ~t[i+Step] )
                    return 0;
        return 1;
    }
}

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

Alan Mishchenko committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
  Synopsis    [Stretch truthtable to have more input variables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_TtStretch5( unsigned * pInOut, int nVarS, int nVarB )
{
    int w, i, step, nWords;
    if ( nVarS == nVarB )
        return;
    assert( nVarS < nVarB );
    step = Abc_TruthWordNum(nVarS);
    nWords = Abc_TruthWordNum(nVarB);
    if ( step == nWords )
        return;
    assert( step < nWords );
    for ( w = 0; w < nWords; w += step )
        for ( i = 0; i < step; i++ )
            pInOut[w + i] = pInOut[i];              
}
static inline void Abc_TtStretch6( word * pInOut, int nVarS, int nVarB )
{
    int w, i, step, nWords;
    if ( nVarS == nVarB )
        return;
    assert( nVarS < nVarB );
    step = Abc_Truth6WordNum(nVarS);
    nWords = Abc_Truth6WordNum(nVarB);
    if ( step == nWords )
        return;
    assert( step < nWords );
    for ( w = 0; w < nWords; w += step )
        for ( i = 0; i < step; i++ )
            pInOut[w + i] = pInOut[i];              
}
static inline word Abc_Tt6Stretch( word t, int nVars )
{
    assert( nVars >= 0 );
    if ( nVars == 0 )
        nVars++, t = (t & 0x1) | ((t & 0x1) << 1);
    if ( nVars == 1 )
        nVars++, t = (t & 0x3) | ((t & 0x3) << 2);
    if ( nVars == 2 )
        nVars++, t = (t & 0xF) | ((t & 0xF) << 4);
    if ( nVars == 3 )
        nVars++, t = (t & 0xFF) | ((t & 0xFF) << 8);
    if ( nVars == 4 )
        nVars++, t = (t & 0xFFFF) | ((t & 0xFFFF) << 16);
    if ( nVars == 5 )
        nVars++, t = (t & 0xFFFFFFFF) | ((t & 0xFFFFFFFF) << 32);
    assert( nVars == 6 );
    return t;
}

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

1345 1346 1347 1348 1349 1350 1351 1352 1353
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1354 1355 1356 1357
static inline int Abc_TtIsHexDigit( char HexChar )
{
    return (HexChar >= '0' && HexChar <= '9') || (HexChar >= 'A' && HexChar <= 'F') || (HexChar >= 'a' && HexChar <= 'f');
}
Alan Mishchenko committed
1358
static inline char Abc_TtPrintDigit( int Digit )
1359 1360 1361
{
    assert( Digit >= 0 && Digit < 16 );
    if ( Digit < 10 )
Alan Mishchenko committed
1362
        return '0' + Digit;
Alan Mishchenko committed
1363
    return 'A' + Digit-10;
1364
}
1365 1366 1367 1368 1369 1370 1371
static inline char Abc_TtPrintDigitLower( int Digit )
{
    assert( Digit >= 0 && Digit < 16 );
    if ( Digit < 10 )
        return '0' + Digit;
    return 'a' + Digit-10;
}
Alan Mishchenko committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
static inline int Abc_TtReadHexDigit( char HexChar )
{
    if ( HexChar >= '0' && HexChar <= '9' )
        return HexChar - '0';
    if ( HexChar >= 'A' && HexChar <= 'F' )
        return HexChar - 'A' + 10;
    if ( HexChar >= 'a' && HexChar <= 'f' )
        return HexChar - 'a' + 10;
    assert( 0 ); // not a hexadecimal symbol
    return -1; // return value which makes no sense
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1395 1396 1397
static inline void Abc_TtPrintHex( word * pTruth, int nVars )
{
    word * pThis, * pLimit = pTruth + Abc_TtWordNum(nVars);
1398
    int k;
1399 1400 1401 1402 1403 1404 1405 1406 1407
    if ( nVars < 2 )
        printf( "%c", Abc_TtPrintDigit((int)pTruth[0] & 15) );
    else
    {
        assert( nVars >= 2 );
        for ( pThis = pTruth; pThis < pLimit; pThis++ )
            for ( k = 0; k < 16; k++ )
                printf( "%c", Abc_TtPrintDigit((int)(pThis[0] >> (k << 2)) & 15) );
    }
1408 1409
    printf( "\n" );
}
Alan Mishchenko committed
1410
static inline void Abc_TtPrintHexRev( FILE * pFile, word * pTruth, int nVars )
1411 1412
{
    word * pThis;
Alan Mishchenko committed
1413
    int k, StartK = nVars >= 6 ? 16 : (1 << (nVars - 2));
1414 1415 1416 1417 1418 1419 1420 1421 1422
    if ( nVars < 2 )
        fprintf( pFile, "%c", Abc_TtPrintDigit((int)pTruth[0] & 15) );
    else
    {
        assert( nVars >= 2 );
        for ( pThis = pTruth + Abc_TtWordNum(nVars) - 1; pThis >= pTruth; pThis-- )
            for ( k = StartK - 1; k >= 0; k-- )
                fprintf( pFile, "%c", Abc_TtPrintDigit((int)(pThis[0] >> (k << 2)) & 15) );
    }
Alan Mishchenko committed
1423
//    printf( "\n" );
1424 1425 1426 1427
}
static inline void Abc_TtPrintHexSpecial( word * pTruth, int nVars )
{
    word * pThis;
1428
    int k;
1429 1430 1431 1432 1433 1434 1435 1436 1437
    if ( nVars < 2 )
        printf( "%c", Abc_TtPrintDigit((int)pTruth[0] & 15) );
    else
    {
        assert( nVars >= 2 );
        for ( pThis = pTruth + Abc_TtWordNum(nVars) - 1; pThis >= pTruth; pThis-- )
            for ( k = 0; k < 16; k++ )
                printf( "%c", Abc_TtPrintDigit((int)(pThis[0] >> (k << 2)) & 15) );
    }
1438 1439
    printf( "\n" );
}
Alan Mishchenko committed
1440 1441 1442 1443 1444
static inline int Abc_TtWriteHexRev( char * pStr, word * pTruth, int nVars )
{
    word * pThis;
    char * pStrInit = pStr;
    int k, StartK = nVars >= 6 ? 16 : (1 << (nVars - 2));
1445 1446 1447 1448 1449 1450 1451 1452 1453
    if ( nVars < 2 )
        *pStr++ = Abc_TtPrintDigit((int)pTruth[0] & 15);
    else
    {
        assert( nVars >= 2 );
        for ( pThis = pTruth + Abc_TtWordNum(nVars) - 1; pThis >= pTruth; pThis-- )
            for ( k = StartK - 1; k >= 0; k-- )
                *pStr++ = Abc_TtPrintDigit( (int)(pThis[0] >> (k << 2)) & 15 );
    }
Alan Mishchenko committed
1454 1455
    return pStr - pStrInit;
}
1456 1457 1458 1459 1460 1461
static inline void Abc_TtPrintHexArrayRev( FILE * pFile, word * pTruth, int nDigits )
{
    int k;
    for ( k = nDigits - 1; k >= 0; k-- )
        fprintf( pFile, "%c", Abc_TtPrintDigitLower( Abc_TtGetHex(pTruth, k) ) );
}
1462

Alan Mishchenko committed
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
/**Function*************************************************************

  Synopsis    [Reads hex truth table from a string.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtReadHex( word * pTruth, char * pString )
{
    int k, nVars, Digit, nDigits;
    // skip the first 2 symbols if they are "0x"
    if ( pString[0] == '0' && pString[1] == 'x' )
        pString += 2;
    // count the number of hex digits
    nDigits = 0;
    for ( k = 0; Abc_TtIsHexDigit(pString[k]); k++ )
        nDigits++;
    if ( nDigits == 1 )
    {
        if ( pString[0] == '0' || pString[0] == 'F' )
        {
            pTruth[0] = (pString[0] == '0') ? 0 : ~(word)0;
            return 0;
        }
        if ( pString[0] == '5' || pString[0] == 'A' )
        {
            pTruth[0] = (pString[0] == '5') ? s_Truths6Neg[0] : s_Truths6[0];
            return 1;
        }
    }
    // determine the number of variables
1498
    nVars = 2 + (nDigits == 1 ? 0 : Abc_Base2Log(nDigits));
Alan Mishchenko committed
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
    // clean storage
    for ( k = Abc_TtWordNum(nVars) - 1; k >= 0; k-- )
        pTruth[k] = 0;
    // read hexadecimal digits in the reverse order
    // (the last symbol in the string is the least significant digit)
    for ( k = 0; k < nDigits; k++ )
    {
        Digit = Abc_TtReadHexDigit( pString[nDigits - 1 - k] );
        assert( Digit >= 0 && Digit < 16 );
        Abc_TtSetHex( pTruth, k, Digit );
    }
    if ( nVars < 6 )
        pTruth[0] = Abc_Tt6Stretch( pTruth[0], nVars );
    return nVars;
}
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
static inline int Abc_TtReadHexNumber( word * pTruth, char * pString )
{
    // count the number of hex digits
    int k, Digit, nDigits = 0;
    for ( k = 0; Abc_TtIsHexDigit(pString[k]); k++ )
        nDigits++;
    // read hexadecimal digits in the reverse order
    // (the last symbol in the string is the least significant digit)
    for ( k = 0; k < nDigits; k++ )
    {
        Digit = Abc_TtReadHexDigit( pString[nDigits - 1 - k] );
        assert( Digit >= 0 && Digit < 16 );
        Abc_TtSetHex( pTruth, k, Digit );
    }
    return nDigits;
}
Alan Mishchenko committed
1530

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
static inline void Abc_TtPrintBits( word * pTruth, int nBits )
{
    int k;
    for ( k = 0; k < nBits; k++ )
        printf( "%d", Abc_InfoHasBit( (unsigned *)pTruth, k ) );
    printf( "\n" );
}
static inline void Abc_TtPrintBits2( word * pTruth, int nBits )
{
    int k;
    for ( k = nBits-1; k >= 0; k-- )
        printf( "%d", Abc_InfoHasBit( (unsigned *)pTruth, k ) );
    printf( "\n" );
}
1557 1558 1559
static inline void Abc_TtPrintBinary( word * pTruth, int nVars )
{
    word * pThis, * pLimit = pTruth + Abc_TtWordNum(nVars);
Alan Mishchenko committed
1560
    int k, Limit = Abc_MinInt( 64, (1 << nVars) );
1561 1562
    assert( nVars >= 2 );
    for ( pThis = pTruth; pThis < pLimit; pThis++ )
Alan Mishchenko committed
1563
        for ( k = 0; k < Limit; k++ )
1564 1565 1566
            printf( "%d", Abc_InfoHasBit( (unsigned *)pThis, k ) );
    printf( "\n" );
}
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
static inline void Abc_TtPrintBinary1( FILE * pFile, word * pTruth, int nVars )
{
    word * pThis, * pLimit = pTruth + Abc_TtWordNum(nVars);
    int k, Limit = Abc_MinInt( 64, (1 << nVars) );
    assert( nVars >= 2 );
    for ( pThis = pTruth; pThis < pLimit; pThis++ )
        for ( k = 0; k < Limit; k++ )
            fprintf( pFile, "%d", Abc_InfoHasBit( (unsigned *)pThis, k ) );
}
static inline void Abc_TtPrintBinary2( FILE * pFile, word * pTruth, int nVars )
{
    word * pThis;
    int k, Limit = Abc_MinInt( 64, (1 << nVars) );
    assert( nVars >= 2 );
    for ( pThis = pTruth + Abc_TtWordNum(nVars) - 1; pThis >= pTruth; pThis-- )
        for ( k = Limit-1; k >= 0; k-- )
            fprintf( pFile, "%d", Abc_InfoHasBit( (unsigned *)pThis, k ) );
}
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtSuppFindFirst( int Supp )
{
    int i;
    assert( Supp > 0 );
    for ( i = 0; i < 32; i++ )
        if ( Supp & (1 << i) )
            return i;
    return -1;
}
static inline int Abc_TtSuppOnlyOne( int Supp )
{
Alan Mishchenko committed
1608 1609
    if ( Supp == 0 )
        return 0;
1610 1611
    return (Supp & (Supp-1)) == 0;
}
1612 1613
static inline int Abc_TtSuppIsMinBase( int Supp )
{
1614
    assert( Supp > 0 );
1615 1616 1617 1618
    return (Supp & (Supp+1)) == 0;
}
static inline int Abc_Tt6HasVar( word t, int iVar )
{
1619
    return ((t >> (1<<iVar)) & s_Truths6Neg[iVar]) != (t & s_Truths6Neg[iVar]);
1620
}
1621 1622 1623 1624
static inline int Abc_Tt6XorVar( word t, int iVar )
{
    return ((t >> (1<<iVar)) & s_Truths6Neg[iVar]) == ~(t & s_Truths6Neg[iVar]);
}
1625 1626 1627
static inline int Abc_TtHasVar( word * t, int nVars, int iVar )
{
    assert( iVar < nVars );
Alan Mishchenko committed
1628 1629
    if ( nVars <= 6 )
        return Abc_Tt6HasVar( t[0], iVar );
1630 1631 1632
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
Alan Mishchenko committed
1633
        int nWords = Abc_TtWordNum( nVars );
1634
        for ( i = 0; i < nWords; i++ )
1635
            if ( ((t[i] >> Shift) & s_Truths6Neg[iVar]) != (t[i] & s_Truths6Neg[iVar]) )
1636 1637 1638 1639 1640
                return 1;
        return 0;
    }
    else
    {
1641
        int i, Step = (1 << (iVar - 6));
Alan Mishchenko committed
1642
        word * tLimit = t + Abc_TtWordNum( nVars );
1643
        for ( ; t < tLimit; t += 2*Step )
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
            for ( i = 0; i < Step; i++ )
                if ( t[i] != t[Step+i] )
                    return 1;
        return 0;
    }
}
static inline int Abc_TtSupport( word * t, int nVars )
{
    int v, Supp = 0;
    for ( v = 0; v < nVars; v++ )
        if ( Abc_TtHasVar( t, nVars, v ) )
            Supp |= (1 << v);
    return Supp;
}
static inline int Abc_TtSupportSize( word * t, int nVars )
{
    int v, SuppSize = 0;
    for ( v = 0; v < nVars; v++ )
        if ( Abc_TtHasVar( t, nVars, v ) )
            SuppSize++;
    return SuppSize;
}
static inline int Abc_TtSupportAndSize( word * t, int nVars, int * pSuppSize )
{
    int v, Supp = 0;
    *pSuppSize = 0;
    for ( v = 0; v < nVars; v++ )
        if ( Abc_TtHasVar( t, nVars, v ) )
            Supp |= (1 << v), (*pSuppSize)++;
    return Supp;
}
static inline int Abc_Tt6SupportAndSize( word t, int nVars, int * pSuppSize )
{
    int v, Supp = 0;
    *pSuppSize = 0;
    assert( nVars <= 6 );
    for ( v = 0; v < nVars; v++ )
        if ( Abc_Tt6HasVar( t, v ) )
            Supp |= (1 << v), (*pSuppSize)++;
    return Supp;
}
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
static inline int Abc_Tt6Check1( word t, int nVars )
{
    int n, v, u;
    for ( n = 0; n < 2; n++ )
    for ( v = 0; v < nVars; v++ )
    {
        word Cof = n ? Abc_Tt6Cofactor1(t, v) : Abc_Tt6Cofactor0(t, v);
        for ( u = 0; u < nVars; u++ )
            if ( v != u && !Abc_Tt6HasVar(Cof, u) )
                return 1;
    }
    return 0;
}
static inline int Abc_Tt6Check2( word t, int nVars )
{
    int n, v;
    for ( n = 0; n < 2; n++ )
    for ( v = 0; v < nVars; v++ )
    {
        word Cof = n ? Abc_Tt6Cofactor1(t, v) : Abc_Tt6Cofactor0(t, v);
        if ( Cof == 0 || ~Cof == 0 )
            return 1;
    }
    return 0;
}
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
/**Function*************************************************************

  Synopsis    [Checks if there is a var whose both cofs have supp <= nSuppLim.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtCheckCondDep2( word * pTruth, int nVars, int nSuppLim )
{
    int v, d, nWords = Abc_TtWordNum(nVars);
    if ( nVars <= nSuppLim + 1 )
        return 0;
    for ( v = 0; v < nVars; v++ )
    {
        int nDep0 = 0, nDep1 = 0;
        for ( d = 0; d < nVars; d++ )
        {
            if ( v == d )
                continue;
            if ( v < d )
            {
                nDep0 += !Abc_TtCheckEqualCofs( pTruth, nWords, v, d, 0, 2 );
                nDep1 += !Abc_TtCheckEqualCofs( pTruth, nWords, v, d, 1, 3 );
            }
            else // if ( v > d )
            {
                nDep0 += !Abc_TtCheckEqualCofs( pTruth, nWords, d, v, 0, 1 );
                nDep1 += !Abc_TtCheckEqualCofs( pTruth, nWords, d, v, 2, 3 );
            }
            if ( nDep0 > nSuppLim || nDep1 > nSuppLim )
                break;
        }
        if ( d == nVars )
            return v;
    }
    return nVars;
}
static inline int Abc_TtCheckCondDep( word * pTruth, int nVars, int nSuppLim )
{
1753 1754
    int nVarsMax = 13;
    word Cof0[128], Cof1[128]; // pow( 2, nVarsMax-6 )
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
    int v, d, nWords = Abc_TtWordNum(nVars);
    assert( nVars <= nVarsMax );
    if ( nVars <= nSuppLim + 1 )
        return 0;
    for ( v = 0; v < nVars; v++ )
    {
        int nDep0 = 0, nDep1 = 0;
        Abc_TtCofactor0p( Cof0, pTruth, nWords, v );
        Abc_TtCofactor1p( Cof1, pTruth, nWords, v );
        for ( d = 0; d < nVars; d++ )
        {
            if ( v == d )
                continue;
            nDep0 += Abc_TtHasVar( Cof0, nVars, d );
            nDep1 += Abc_TtHasVar( Cof1, nVars, d );
            if ( nDep0 > nSuppLim || nDep1 > nSuppLim )
                break;
        }
        if ( d == nVars )
            return v;
    }
    return nVars;
}

1779 1780

/**Function*************************************************************
Alan Mishchenko committed
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

  Synopsis    [Detecting elementary functions.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtOnlyOneOne( word t )
{
    if ( t == 0 )
        return 0;
    return (t & (t-1)) == 0;
}
1797
static inline int Abc_Tt6IsAndType( word t, int nVars )
Alan Mishchenko committed
1798 1799 1800
{
    return Abc_TtOnlyOneOne( t & Abc_Tt6Mask(1 << nVars) );
}
1801
static inline int Abc_Tt6IsOrType( word t, int nVars )
Alan Mishchenko committed
1802 1803 1804
{
    return Abc_TtOnlyOneOne( ~t & Abc_Tt6Mask(1 << nVars) );
}
1805
static inline int Abc_Tt6IsXorType( word t, int nVars )
Alan Mishchenko committed
1806 1807 1808 1809 1810 1811
{
    return ((((t & 1) ? ~t : t) ^ s_TruthXors[nVars]) & Abc_Tt6Mask(1 << nVars)) == 0;
}


/**Function*************************************************************
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1822 1823 1824 1825
static inline word Abc_Tt6Flip( word Truth, int iVar )
{
    return Truth = ((Truth << (1 << iVar)) & s_Truths6[iVar]) | ((Truth & s_Truths6[iVar]) >> (1 << iVar));
}
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
static inline void Abc_TtFlip( word * pTruth, int nWords, int iVar )
{
    if ( nWords == 1 )
        pTruth[0] = ((pTruth[0] << (1 << iVar)) & s_Truths6[iVar]) | ((pTruth[0] & s_Truths6[iVar]) >> (1 << iVar));
    else if ( iVar <= 5 )
    {
        int w, shift = (1 << iVar);
        for ( w = 0; w < nWords; w++ )
            pTruth[w] = ((pTruth[w] << shift) & s_Truths6[iVar]) | ((pTruth[w] & s_Truths6[iVar]) >> shift);
    }
    else // if ( iVar > 5 )
    {
        word * pLimit = pTruth + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        for ( ; pTruth < pLimit; pTruth += 2*iStep )
            for ( i = 0; i < iStep; i++ )
                ABC_SWAP( word, pTruth[i], pTruth[i + iStep] );
    }    
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
1857 1858 1859 1860
static inline word Abc_Tt6SwapAdjacent( word Truth, int iVar )
{
    return (Truth & s_PMasks[iVar][0]) | ((Truth & s_PMasks[iVar][1]) << (1 << iVar)) | ((Truth & s_PMasks[iVar][2]) >> (1 << iVar));
}
1861 1862 1863 1864 1865 1866
static inline void Abc_TtSwapAdjacent( word * pTruth, int nWords, int iVar )
{
    if ( iVar < 5 )
    {
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
1867
            pTruth[i] = (pTruth[i] & s_PMasks[iVar][0]) | ((pTruth[i] & s_PMasks[iVar][1]) << Shift) | ((pTruth[i] & s_PMasks[iVar][2]) >> Shift);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
    }
    else if ( iVar == 5 )
    {
        unsigned * pTruthU = (unsigned *)pTruth;
        unsigned * pLimitU = (unsigned *)(pTruth + nWords);
        for ( ; pTruthU < pLimitU; pTruthU += 4 )
            ABC_SWAP( unsigned, pTruthU[1], pTruthU[2] );
    }
    else // if ( iVar > 5 )
    {
        word * pLimit = pTruth + nWords;
        int i, iStep = Abc_TtWordNum(iVar);
        for ( ; pTruth < pLimit; pTruth += 4*iStep )
            for ( i = 0; i < iStep; i++ )
                ABC_SWAP( word, pTruth[i + iStep], pTruth[i + 2*iStep] );
    }
}
1885 1886
static inline word Abc_Tt6SwapVars( word t, int iVar, int jVar )
{
Alan Mishchenko committed
1887
    word * s_PMasks = s_PPMasks[iVar][jVar];
1888 1889 1890 1891
    int shift = (1 << jVar) - (1 << iVar);
    assert( iVar < jVar );
    return (t & s_PMasks[0]) | ((t & s_PMasks[1]) << shift) | ((t & s_PMasks[2]) >> shift);
}
1892 1893
static inline void Abc_TtSwapVars( word * pTruth, int nVars, int iVar, int jVar )
{
1894 1895 1896 1897 1898 1899 1900
    if ( iVar == jVar )
        return;
    if ( jVar < iVar )
        ABC_SWAP( int, iVar, jVar );
    assert( iVar < jVar && jVar < nVars );
    if ( nVars <= 6 )
    {
1901
        pTruth[0] = Abc_Tt6SwapVars( pTruth[0], iVar, jVar );
1902
        return;
1903
    }
1904
    if ( jVar <= 5 )
1905
    {
Alan Mishchenko committed
1906
        word * s_PMasks = s_PPMasks[iVar][jVar];
1907 1908 1909
        int nWords = Abc_TtWordNum(nVars);
        int w, shift = (1 << jVar) - (1 << iVar);
        for ( w = 0; w < nWords; w++ )
1910
            pTruth[w] = (pTruth[w] & s_PMasks[0]) | ((pTruth[w] & s_PMasks[1]) << shift) | ((pTruth[w] & s_PMasks[2]) >> shift);
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
        return;
    }
    if ( iVar <= 5 && jVar > 5 )
    {
        word low2High, high2Low;
        word * pLimit = pTruth + Abc_TtWordNum(nVars);
        int j, jStep = Abc_TtWordNum(jVar);
        int shift = 1 << iVar;
        for ( ; pTruth < pLimit; pTruth += 2*jStep )
            for ( j = 0; j < jStep; j++ )
            {
                low2High = (pTruth[j] & s_Truths6[iVar]) >> shift;
                high2Low = (pTruth[j+jStep] << shift) & s_Truths6[iVar];
                pTruth[j] = (pTruth[j] & ~s_Truths6[iVar]) | high2Low;
                pTruth[j+jStep] = (pTruth[j+jStep] & s_Truths6[iVar]) | low2High;
            }
        return;
1928
    }
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
    {
        word * pLimit = pTruth + Abc_TtWordNum(nVars);
        int i, iStep = Abc_TtWordNum(iVar);
        int j, jStep = Abc_TtWordNum(jVar);
        for ( ; pTruth < pLimit; pTruth += 2*jStep )
            for ( i = 0; i < jStep; i += 2*iStep )
                for ( j = 0; j < iStep; j++ )
                    ABC_SWAP( word, pTruth[iStep + i + j], pTruth[jStep + i + j] );
        return;
    }    
1939
}
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
// moves one var (v) to the given position (p)
static inline void Abc_TtMoveVar( word * pF, int nVars, int * V2P, int * P2V, int v, int p )
{
    int iVar = V2P[v], jVar = p;
    if ( iVar == jVar )
        return;
    Abc_TtSwapVars( pF, nVars, iVar, jVar );
    V2P[P2V[iVar]] = jVar;
    V2P[P2V[jVar]] = iVar;
    P2V[iVar] ^= P2V[jVar];
    P2V[jVar] ^= P2V[iVar];
    P2V[iVar] ^= P2V[jVar];
}
1953 1954 1955 1956 1957 1958 1959
static inline word Abc_Tt6RemoveVar( word t, int iVar )
{
    assert( !Abc_Tt6HasVar(t, iVar) );
    while ( iVar < 5 )
        t = Abc_Tt6SwapAdjacent( t, iVar++ );
    return t;
}
1960

1961 1962
/**Function*************************************************************

1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word Abc_Tt6Permute_rec( word t, int * pPerm, int nVars )
{
    word uRes0, uRes1; int Var;
    if (  t == 0 ) return 0;
    if ( ~t == 0 ) return ~(word)0;
    for ( Var = nVars-1; Var >= 0; Var-- )
        if ( Abc_Tt6HasVar( t, Var ) )
             break;
    assert( Var >= 0 );
    uRes0 = Abc_Tt6Permute_rec( Abc_Tt6Cofactor0(t, Var), pPerm, Var );
    uRes1 = Abc_Tt6Permute_rec( Abc_Tt6Cofactor1(t, Var), pPerm, Var );
    return (uRes0 & s_Truths6Neg[pPerm[Var]]) | (uRes1 & s_Truths6[pPerm[Var]]);
}
static inline void Abc_TtPermute( word * p, int * pPerm, int nVars )
{
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
    int v, UnPerm[16], Perm[16];
    assert( nVars <= 16 );
    for ( v = 0; v < nVars; v++ )
        UnPerm[v] = Perm[v] = v;
    for ( v = nVars-1; v >= 0; v-- )
    {
        int Lev = UnPerm[pPerm[v]];
        if ( v == Lev )
            continue;
        Abc_TtSwapVars( p, nVars, v, Lev );
        ABC_SWAP( int, Perm[v], Perm[Lev] );
        UnPerm[Perm[Lev]] = Lev;
        UnPerm[Perm[v]] = v;
    }
    for ( v = 0; v < nVars; v++ )
        assert( Perm[v] == pPerm[v] );
}
static inline void Abc_TtUnpermute( word * p, int * pPerm, int nVars )
{
2006
    int v, Perm[16];
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
    assert( nVars <= 16 );
    for ( v = 0; v < nVars; v++ )
        Perm[v] = pPerm[v];
    for ( v = nVars-1; v >= 0; v-- )
    {
        while ( v != Perm[v] )
        {
            int vCur = Perm[v];
            Abc_TtSwapVars( p, nVars, v, vCur );
            Perm[v] = Perm[vCur];
            Perm[vCur]= vCur;
        }
    }
2020 2021
    for ( v = 0; v < nVars; v++ )
        assert( Perm[v] == v );
2022 2023 2024 2025
}

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

2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  Synopsis    [Support minimization.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_TtShrink( word * pF, int nVars, int nVarsAll, unsigned Phase )
{
    int i, k, Var = 0;
    assert( nVarsAll <= 16 );
    for ( i = 0; i < nVarsAll; i++ )
        if ( Phase & (1 << i) )
        {
            for ( k = i-1; k >= Var; k-- )
                Abc_TtSwapAdjacent( pF, Abc_TtWordNum(nVarsAll), k );
            Var++;
        }
    assert( Var == nVars );
}
static inline int Abc_TtMinimumBase( word * t, int * pSupp, int nVarsAll, int * pnVars )
{
    int v, iVar = 0, uSupp = 0;
    assert( nVarsAll <= 16 );
    for ( v = 0; v < nVarsAll; v++ )
        if ( Abc_TtHasVar( t, nVarsAll, v ) )
        {
            uSupp |= (1 << v);
            if ( pSupp )
                pSupp[iVar] = pSupp[v];
            iVar++;
        }
    if ( pnVars )
        *pnVars = iVar;
    if ( uSupp == 0 || Abc_TtSuppIsMinBase( uSupp ) )
        return 0;
    Abc_TtShrink( t, iVar, nVarsAll, uSupp );
    return 1;
}
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
static inline int Abc_TtSimplify( word * t, int * pLits, int nVarsAll, int * pnVars )
{
    int v, u;
    for ( v = 0; v < nVarsAll; v++ )
    {
        if ( pLits[v] == 0 )
            Abc_TtCofactor0( t, Abc_TtWordNum(nVarsAll), v );
        else if ( pLits[v] == 1 )
            Abc_TtCofactor1( t, Abc_TtWordNum(nVarsAll), v );
    }
    for ( v = 0;   v < nVarsAll; v++ )
    for ( u = v+1; u < nVarsAll; u++ )
        if ( Abc_Lit2Var(pLits[v]) == Abc_Lit2Var(pLits[u]) )
        {
            assert( nVarsAll <= 6 );
            if ( pLits[v] == pLits[u] )
            {
                word t0 = Abc_Tt6Cofactor0(Abc_Tt6Cofactor0(*t, v), u);
                word t1 = Abc_Tt6Cofactor1(Abc_Tt6Cofactor1(*t, v), u);
                *t = (t0 & s_Truths6Neg[v]) | (t1 & s_Truths6[v]);
            }
            else // if ( pLits[v] == Abc_LitNot(pLits[u]) )
            {
                word t0 = Abc_Tt6Cofactor1(Abc_Tt6Cofactor0(*t, v), u);
                word t1 = Abc_Tt6Cofactor0(Abc_Tt6Cofactor1(*t, v), u);
                *t = (t0 & s_Truths6Neg[v]) | (t1 & s_Truths6[v]);
            }
        }
    return Abc_TtMinimumBase( t, pLits, nVarsAll, pnVars );
}
2097 2098 2099

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

2100 2101 2102 2103 2104 2105 2106 2107 2108
  Synopsis    [Cut minimization.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2109
static inline word Abc_Tt6Expand( word t, int * pCut0, int nCutSize0, int * pCut, int nCutSize )
2110 2111
{
    int i, k;
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
    for ( i = nCutSize - 1, k = nCutSize0 - 1; i >= 0 && k >= 0; i-- )
    {
        if ( pCut[i] > pCut0[k] )
            continue;
        assert( pCut[i] == pCut0[k] );
        if ( k < i )
            t = Abc_Tt6SwapVars( t, k, i );
        k--;
    }
    assert( k == -1 );
    return t;
}
static inline void Abc_TtExpand( word * pTruth0, int nVars, int * pCut0, int nCutSize0, int * pCut, int nCutSize )
{
    int i, k;
    for ( i = nCutSize - 1, k = nCutSize0 - 1; i >= 0 && k >= 0; i-- )
    {
        if ( pCut[i] > pCut0[k] )
            continue;
        assert( pCut[i] == pCut0[k] );
        if ( k < i )
            Abc_TtSwapVars( pTruth0, nVars, k, i );
        k--;
    }
    assert( k == -1 );
}
static inline int Abc_Tt6MinBase( word * pTruth, int * pVars, int nVars ) 
{
    word t = *pTruth;
    int i, k;
2142 2143
    for ( i = k = 0; i < nVars; i++ )
    {
2144
        if ( !Abc_Tt6HasVar( t, i ) )
2145 2146 2147
            continue;
        if ( k < i )
        {
2148
            if ( pVars ) pVars[k] = pVars[i];
2149
            t = Abc_Tt6SwapVars( t, k, i );
2150 2151 2152 2153 2154 2155
        }
        k++;
    }
    if ( k == nVars )
        return k;
    assert( k < nVars );
2156
    *pTruth = t;
2157 2158
    return k;
}
2159
static inline int Abc_TtMinBase( word * pTruth, int * pVars, int nVars, int nVarsAll ) 
2160 2161
{
    int i, k;
2162 2163
    assert( nVars <= nVarsAll );
    for ( i = k = 0; i < nVars; i++ )
2164
    {
2165
        if ( !Abc_TtHasVar( pTruth, nVarsAll, i ) )
2166 2167
            continue;
        if ( k < i )
2168 2169 2170 2171 2172
        {
            if ( pVars ) pVars[k] = pVars[i];
            Abc_TtSwapVars( pTruth, nVarsAll, k, i );
        }
        k++;
2173
    }
2174 2175 2176 2177 2178
    if ( k == nVars )
        return k;
    assert( k < nVars );
//    assert( k == Abc_TtSupportSize(pTruth, nVars) );
    return k;
2179 2180 2181 2182
}

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

2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
  Synopsis    [Implemeting given NPN config.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_TtImplementNpnConfig( word * pTruth, int nVars, char * pCanonPerm, unsigned uCanonPhase )
{
    int i, k, nWords = Abc_TtWordNum( nVars );
    if ( (uCanonPhase >> nVars) & 1 )
        Abc_TtNot( pTruth, nWords );
    for ( i = 0; i < nVars; i++ )
        if ( (uCanonPhase >> i) & 1 )
            Abc_TtFlip( pTruth, nWords, i );
2200
    if ( pCanonPerm )
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
    for ( i = 0; i < nVars; i++ )
    {
        for ( k = i; k < nVars; k++ )
            if ( pCanonPerm[k] == i )
                break;
        assert( k < nVars );
        if ( i == k )
            continue;
        Abc_TtSwapVars( pTruth, nVars, i, k );
        ABC_SWAP( int, pCanonPerm[i], pCanonPerm[k] );
    }
}

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

2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtCountOnesSlow( word t )
2226
{
2227 2228 2229 2230 2231 2232
    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);
2233 2234 2235
}
static inline int Abc_TtCountOnes( word x )
{
2236 2237 2238
    x = x - ((x >> 1) & ABC_CONST(0x5555555555555555));   
    x = (x & ABC_CONST(0x3333333333333333)) + ((x >> 2) & ABC_CONST(0x3333333333333333));    
    x = (x + (x >> 4)) & ABC_CONST(0x0F0F0F0F0F0F0F0F);    
2239 2240 2241 2242 2243
    x = x + (x >> 8);
    x = x + (x >> 16);
    x = x + (x >> 32); 
    return (int)(x & 0xFF);
}
Alan Mishchenko committed
2244 2245 2246 2247
static inline int Abc_TtCountOnes2( word x )
{
    return x ? Abc_TtCountOnes(x) : 0;
}
2248 2249 2250 2251
static inline int Abc_TtCountOnesVec( word * x, int nWords )
{
    int w, Count = 0;
    for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2252
        Count += Abc_TtCountOnes2( x[w] );
2253 2254 2255 2256 2257 2258
    return Count;
}
static inline int Abc_TtCountOnesVecMask( word * x, word * pMask, int nWords, int fCompl )
{
    int w, Count = 0;
    if ( fCompl )
Alan Mishchenko committed
2259
    {
2260
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2261
            Count += Abc_TtCountOnes2( pMask[w] & ~x[w] );
Alan Mishchenko committed
2262
    }
2263
    else
Alan Mishchenko committed
2264
    {
2265
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2266
            Count += Abc_TtCountOnes2( pMask[w] & x[w] );
Alan Mishchenko committed
2267
    }
2268 2269
    return Count;
}
2270 2271 2272 2273 2274
static inline int Abc_TtCountOnesVecMask2( word * x0, word * x1, int fComp0, int fComp1, word * pMask, int nWords )
{
    int w, Count = 0;
    if ( !fComp0 && !fComp1 )
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2275
            Count += Abc_TtCountOnes2( pMask[w] &  x0[w] &  x1[w] );
2276 2277
    else if (  fComp0 && !fComp1 )
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2278
            Count += Abc_TtCountOnes2( pMask[w] & ~x0[w] &  x1[w] );
2279 2280
    else if ( !fComp0 &&  fComp1 )
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2281
            Count += Abc_TtCountOnes2( pMask[w] &  x0[w] & ~x1[w] );
2282 2283
    else 
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2284
            Count += Abc_TtCountOnes2( pMask[w] & ~x0[w] & ~x1[w] );
2285 2286
    return Count;
}
2287 2288 2289 2290
static inline int Abc_TtCountOnesVecXor( word * x, word * y, int nWords )
{
    int w, Count = 0;
    for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2291
        Count += Abc_TtCountOnes2( x[w] ^ y[w] );
2292 2293
    return Count;
}
2294 2295 2296 2297 2298
static inline int Abc_TtCountOnesVecXorMask( word * x, word * y, int fCompl, word * pMask, int nWords )
{
    int w, Count = 0;
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2299
            Count += Abc_TtCountOnes2( pMask[w] & (x[w] ^ ~y[w]) );
2300 2301
    else
        for ( w = 0; w < nWords; w++ )
Alan Mishchenko committed
2302
            Count += Abc_TtCountOnes2( pMask[w] & (x[w] ^ y[w]) );
2303 2304
    return Count;
}
2305 2306 2307 2308 2309 2310
static inline int Abc_TtAndXorSum( word * pOut, word * pIn1, word * pIn2, int nWords )
{
    int w, Count = 0;
    for ( w = 0; w < nWords; w++ )
    {
        pOut[w] &= pIn1[w] ^ pIn2[w];
Alan Mishchenko committed
2311
        Count += Abc_TtCountOnes2( pOut[w] );
2312 2313 2314
    }
    return Count;
}
Alan Mishchenko committed
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
static inline void Abc_TtIsfPrint( word * pOff, word * pOn, int nWords )
{
    int nTotal  = 64*nWords;
    int nOffset = Abc_TtCountOnesVec(pOff, nWords);
    int nOnset  = Abc_TtCountOnesVec(pOn, nWords);
    int nDcset  = nTotal - nOffset - nOnset;
    printf( "OFF =%6d (%6.2f %%)  ", nOffset, 100.0*nOffset/nTotal );
    printf( "ON =%6d (%6.2f %%)  ",  nOnset,  100.0*nOnset/nTotal );
    printf( "DC =%6d (%6.2f %%)",    nDcset,  100.0*nDcset/nTotal );
}
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
2337 2338 2339 2340
static inline int Abc_Tt6FirstBit( word t )
{
    int n = 0;
    if ( t == 0 ) return -1;
2341 2342 2343 2344 2345 2346
    if ( (t & ABC_CONST(0x00000000FFFFFFFF)) == 0 ) { n += 32; t >>= 32; }
    if ( (t & ABC_CONST(0x000000000000FFFF)) == 0 ) { n += 16; t >>= 16; }
    if ( (t & ABC_CONST(0x00000000000000FF)) == 0 ) { n +=  8; t >>=  8; }
    if ( (t & ABC_CONST(0x000000000000000F)) == 0 ) { n +=  4; t >>=  4; }
    if ( (t & ABC_CONST(0x0000000000000003)) == 0 ) { n +=  2; t >>=  2; }
    if ( (t & ABC_CONST(0x0000000000000001)) == 0 ) { n++; }
Alan Mishchenko committed
2347 2348 2349 2350 2351 2352
    return n;
}
static inline int Abc_Tt6LastBit( word t )
{
    int n = 0;
    if ( t == 0 ) return -1;
2353 2354 2355 2356 2357 2358
    if ( (t & ABC_CONST(0xFFFFFFFF00000000)) == 0 ) { n += 32; t <<= 32; }
    if ( (t & ABC_CONST(0xFFFF000000000000)) == 0 ) { n += 16; t <<= 16; }
    if ( (t & ABC_CONST(0xFF00000000000000)) == 0 ) { n +=  8; t <<=  8; }
    if ( (t & ABC_CONST(0xF000000000000000)) == 0 ) { n +=  4; t <<=  4; }
    if ( (t & ABC_CONST(0xC000000000000000)) == 0 ) { n +=  2; t <<=  2; }
    if ( (t & ABC_CONST(0x8000000000000000)) == 0 ) { n++; }
Alan Mishchenko committed
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
    return 63-n;
}
static inline int Abc_TtFindFirstBit( word * pIn, int nVars )
{
    int w, nWords = Abc_TtWordNum(nVars);
    for ( w = 0; w < nWords; w++ )
        if ( pIn[w] )
            return 64*w + Abc_Tt6FirstBit(pIn[w]);
    return -1;
}
2369 2370 2371 2372 2373 2374 2375 2376
static inline int Abc_TtFindFirstBit2( word * pIn, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn[w] )
            return 64*w + Abc_Tt6FirstBit(pIn[w]);
    return -1;
}
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
static inline int Abc_TtFindLastBit( word * pIn, int nVars )
{
    int w, nWords = Abc_TtWordNum(nVars);
    for ( w = nWords - 1; w >= 0; w-- )
        if ( pIn[w] )
            return 64*w + Abc_Tt6LastBit(pIn[w]);
    return -1;
}
static inline int Abc_TtFindLastBit2( word * pIn, int nWords )
{
    int w;
    for ( w = nWords - 1; w >= 0; w-- )
        if ( pIn[w] )
            return 64*w + Abc_Tt6LastBit(pIn[w]);
    return -1;
}
2393 2394 2395 2396 2397 2398 2399 2400
static inline int Abc_TtFindFirstDiffBit( word * pIn1, word * pIn2, int nVars )
{
    int w, nWords = Abc_TtWordNum(nVars);
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] ^ pIn2[w] )
            return 64*w + Abc_Tt6FirstBit(pIn1[w] ^ pIn2[w]);
    return -1;
}
2401 2402 2403 2404 2405 2406 2407 2408
static inline int Abc_TtFindFirstDiffBit2( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] ^ pIn2[w] )
            return 64*w + Abc_Tt6FirstBit(pIn1[w] ^ pIn2[w]);
    return -1;
}
2409
static inline int Abc_TtFindLastDiffBit( word * pIn1, word * pIn2, int nVars )
Alan Mishchenko committed
2410 2411
{
    int w, nWords = Abc_TtWordNum(nVars);
2412 2413 2414
    for ( w = nWords - 1; w >= 0; w-- )
        if ( pIn1[w] ^ pIn2[w] )
            return 64*w + Abc_Tt6LastBit(pIn1[w] ^ pIn2[w]);
Alan Mishchenko committed
2415 2416
    return -1;
}
2417
static inline int Abc_TtFindLastDiffBit2( word * pIn1, word * pIn2, int nWords )
Alan Mishchenko committed
2418
{
2419
    int w;
Alan Mishchenko committed
2420
    for ( w = nWords - 1; w >= 0; w-- )
2421 2422 2423 2424
        if ( pIn1[w] ^ pIn2[w] )
            return 64*w + Abc_Tt6LastBit(pIn1[w] ^ pIn2[w]);
    return -1;
}
Alan Mishchenko committed
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
static inline int Abc_TtFindFirstAndBit2( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] & pIn2[w] )
            return 64*w + Abc_Tt6FirstBit(pIn1[w] & pIn2[w]);
    return -1;
}
static inline int Abc_TtFindLastAndBit2( word * pIn1, word * pIn2, int nWords )
{
    int w;
    for ( w = nWords - 1; w >= 0; w-- )
        if ( pIn1[w] & pIn2[w] )
            return 64*w + Abc_Tt6LastBit(pIn1[w] & pIn2[w]);
    return -1;
}
2441 2442 2443 2444 2445 2446
static inline int Abc_TtFindFirstZero( word * pIn, int nVars )
{
    int w, nWords = Abc_TtWordNum(nVars);
    for ( w = 0; w < nWords; w++ )
        if ( ~pIn[w] )
            return 64*w + Abc_Tt6FirstBit(~pIn[w]);
Alan Mishchenko committed
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
    return -1;
}
static inline int Abc_TtFindLastZero( word * pIn, int nVars )
{
    int w, nWords = Abc_TtWordNum(nVars);
    for ( w = nWords - 1; w >= 0; w-- )
        if ( ~pIn[w] )
            return 64*w + Abc_Tt6LastBit(~pIn[w]);
    return -1;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
static inline void Abc_TtReverseVars( word * pTruth, int nVars )
{
    int k;
    for ( k = 0; k < nVars/2 ; k++ )
        Abc_TtSwapVars( pTruth, nVars, k, nVars - 1 - k );
}
static inline void Abc_TtReverseBits( word * pTruth, int nVars )
{
    static unsigned char pMirror[256] = {
          0, 128,  64, 192,  32, 160,  96, 224,  16, 144,  80, 208,  48, 176, 112, 240,
          8, 136,  72, 200,  40, 168, 104, 232,  24, 152,  88, 216,  56, 184, 120, 248,
          4, 132,  68, 196,  36, 164, 100, 228,  20, 148,  84, 212,  52, 180, 116, 244,
         12, 140,  76, 204,  44, 172, 108, 236,  28, 156,  92, 220,  60, 188, 124, 252,
          2, 130,  66, 194,  34, 162,  98, 226,  18, 146,  82, 210,  50, 178, 114, 242,
         10, 138,  74, 202,  42, 170, 106, 234,  26, 154,  90, 218,  58, 186, 122, 250,
          6, 134,  70, 198,  38, 166, 102, 230,  22, 150,  86, 214,  54, 182, 118, 246,
         14, 142,  78, 206,  46, 174, 110, 238,  30, 158,  94, 222,  62, 190, 126, 254,
          1, 129,  65, 193,  33, 161,  97, 225,  17, 145,  81, 209,  49, 177, 113, 241,
          9, 137,  73, 201,  41, 169, 105, 233,  25, 153,  89, 217,  57, 185, 121, 249,
          5, 133,  69, 197,  37, 165, 101, 229,  21, 149,  85, 213,  53, 181, 117, 245,
         13, 141,  77, 205,  45, 173, 109, 237,  29, 157,  93, 221,  61, 189, 125, 253,
          3, 131,  67, 195,  35, 163,  99, 227,  19, 147,  83, 211,  51, 179, 115, 243,
         11, 139,  75, 203,  43, 171, 107, 235,  27, 155,  91, 219,  59, 187, 123, 251,
          7, 135,  71, 199,  39, 167, 103, 231,  23, 151,  87, 215,  55, 183, 119, 247,
         15, 143,  79, 207,  47, 175, 111, 239,  31, 159,  95, 223,  63, 191, 127, 255
    };
    unsigned char Temp, * pTruthC = (unsigned char *)pTruth;
    int i, nBytes = (nVars > 6) ? (1 << (nVars - 3)) : 8;
    for ( i = 0; i < nBytes/2; i++ )
    {
        Temp = pMirror[pTruthC[i]];
        pTruthC[i] = pMirror[pTruthC[nBytes-1-i]];
        pTruthC[nBytes-1-i] = Temp;
    }
}


Alan Mishchenko committed
2507 2508
/**Function*************************************************************

2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
  Synopsis    [Checks unateness of a function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_Tt6PosVar( word t, int iVar )
{
    return ((t >> (1<<iVar)) & t & s_Truths6Neg[iVar]) == (t & s_Truths6Neg[iVar]);
}
static inline int Abc_Tt6NegVar( word t, int iVar )
{
    return ((t << (1<<iVar)) & t & s_Truths6[iVar]) == (t & s_Truths6[iVar]);
}
static inline int Abc_TtPosVar( word * t, int nVars, int iVar )
{
    assert( iVar < nVars );
    if ( nVars <= 6 )
        return Abc_Tt6PosVar( t[0], iVar );
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        int nWords = Abc_TtWordNum( nVars );
        for ( i = 0; i < nWords; i++ )
            if ( ((t[i] >> Shift) & t[i] & s_Truths6Neg[iVar]) != (t[i] & s_Truths6Neg[iVar]) )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + Abc_TtWordNum( nVars );
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( t[i] != (t[i] & t[Step+i]) )
                    return 0;
        return 1;
    }
}
static inline int Abc_TtNegVar( word * t, int nVars, int iVar )
{
    assert( iVar < nVars );
    if ( nVars <= 6 )
        return Abc_Tt6NegVar( t[0], iVar );
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        int nWords = Abc_TtWordNum( nVars );
        for ( i = 0; i < nWords; i++ )
            if ( ((t[i] << Shift) & t[i] & s_Truths6[iVar]) != (t[i] & s_Truths6[iVar]) )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + Abc_TtWordNum( nVars );
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( (t[i] & t[Step+i]) != t[Step+i] )
                    return 0;
        return 1;
    }
}
static inline int Abc_TtIsUnate( word * t, int nVars )
{
    int i;
    for ( i = 0; i < nVars; i++ )
        if ( !Abc_TtNegVar(t, nVars, i) && !Abc_TtPosVar(t, nVars, i) )
            return 0;
    return 1;
}
static inline int Abc_TtIsPosUnate( word * t, int nVars )
{
    int i;
    for ( i = 0; i < nVars; i++ )
        if ( !Abc_TtPosVar(t, nVars, i) )
            return 0;
    return 1;
}
static inline void Abc_TtMakePosUnate( word * t, int nVars )
{
    int i, nWords = Abc_TtWordNum(nVars);
    for ( i = 0; i < nVars; i++ )
        if ( Abc_TtNegVar(t, nVars, i) )
            Abc_TtFlip( t, nWords, i );
        else assert( Abc_TtPosVar(t, nVars, i) );
}


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

Alan Mishchenko committed
2604 2605 2606 2607 2608 2609 2610 2611 2612
  Synopsis    [Computes ISOP for 6 variables or less.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2613
static inline word Abc_Tt6Isop( word uOn, word uOnDc, int nVars, int * pnCubes )
Alan Mishchenko committed
2614 2615 2616
{
    word uOn0, uOn1, uOnDc0, uOnDc1, uRes0, uRes1, uRes2;
    int Var;
2617
    assert( nVars <= 6 );
Alan Mishchenko committed
2618 2619 2620 2621
    assert( (uOn & ~uOnDc) == 0 );
    if ( uOn == 0 )
        return 0;
    if ( uOnDc == ~(word)0 )
2622 2623
    {
        (*pnCubes)++;
Alan Mishchenko committed
2624
        return ~(word)0;
2625
    }
Alan Mishchenko committed
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
    assert( nVars > 0 );
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
        if ( Abc_Tt6HasVar( uOn, Var ) || Abc_Tt6HasVar( uOnDc, Var ) )
             break;
    assert( Var >= 0 );
    // cofactor
    uOn0   = Abc_Tt6Cofactor0( uOn,   Var );
    uOn1   = Abc_Tt6Cofactor1( uOn  , Var );
    uOnDc0 = Abc_Tt6Cofactor0( uOnDc, Var );
    uOnDc1 = Abc_Tt6Cofactor1( uOnDc, Var );
    // solve for cofactors
2638 2639 2640
    uRes0 = Abc_Tt6Isop( uOn0 & ~uOnDc1, uOnDc0, Var, pnCubes );
    uRes1 = Abc_Tt6Isop( uOn1 & ~uOnDc0, uOnDc1, Var, pnCubes );
    uRes2 = Abc_Tt6Isop( (uOn0 & ~uRes0) | (uOn1 & ~uRes1), uOnDc0 & uOnDc1, Var, pnCubes );
Alan Mishchenko committed
2641 2642 2643 2644 2645 2646
    // derive the final truth table
    uRes2 |= (uRes0 & s_Truths6Neg[Var]) | (uRes1 & s_Truths6[Var]);
    assert( (uOn & ~uRes2) == 0 );
    assert( (uRes2 & ~uOnDc) == 0 );
    return uRes2;
}
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
static inline int Abc_Tt7Isop( word uOn[2], word uOnDc[2], int nVars, word uRes[2] )
{
    int nCubes = 0;
    if ( nVars <= 6 || (uOn[0] == uOn[1] && uOnDc[0] == uOnDc[1]) )
        uRes[0] = uRes[1] = Abc_Tt6Isop( uOn[0], uOnDc[0], Abc_MinInt(nVars, 6), &nCubes );
    else
    {
        word uRes0, uRes1, uRes2;
        assert( nVars == 7 );
        // solve for cofactors
        uRes0 = Abc_Tt6Isop( uOn[0] & ~uOnDc[1], uOnDc[0], 6, &nCubes );
        uRes1 = Abc_Tt6Isop( uOn[1] & ~uOnDc[0], uOnDc[1], 6, &nCubes );
        uRes2 = Abc_Tt6Isop( (uOn[0] & ~uRes0) | (uOn[1] & ~uRes1), uOnDc[0] & uOnDc[1], 6, &nCubes );
        // derive the final truth table
        uRes[0] = uRes2 | uRes0;
        uRes[1] = uRes2 | uRes1;
        assert( (uOn[0] & ~uRes[0]) == 0 && (uOn[1] & ~uRes[1]) == 0 );
        assert( (uRes[0] & ~uOnDc[0])==0 && (uRes[1] & ~uOnDc[1])==0 );
    }
    return nCubes;
}
static inline int Abc_Tt8Isop( word uOn[4], word uOnDc[4], int nVars, word uRes[4] )
{
    int nCubes = 0;
    if ( nVars <= 6 )
        uRes[0] = uRes[1] = uRes[2] = uRes[3] = Abc_Tt6Isop( uOn[0], uOnDc[0], nVars, &nCubes );
    else if ( nVars == 7 || (uOn[0] == uOn[2] && uOn[1] == uOn[3] && uOnDc[0] == uOnDc[2] && uOnDc[1] == uOnDc[3]) )
    {
        nCubes = Abc_Tt7Isop( uOn, uOnDc, 7, uRes );
        uRes[2] = uRes[0];
        uRes[3] = uRes[1];
    }
    else 
    {
        word uOn0[2], uOn1[2], uOn2[2], uOnDc2[2], uRes0[2], uRes1[2], uRes2[2];
        assert( nVars == 8 );
        // cofactor
        uOn0[0] = uOn[0] & ~uOnDc[2];
        uOn0[1] = uOn[1] & ~uOnDc[3];
        uOn1[0] = uOn[2] & ~uOnDc[0];
        uOn1[1] = uOn[3] & ~uOnDc[1];
        uOnDc2[0] = uOnDc[0] & uOnDc[2];
        uOnDc2[1] = uOnDc[1] & uOnDc[3];
        // solve for cofactors
        nCubes += Abc_Tt7Isop( uOn0, uOnDc+0, 7, uRes0 );
        nCubes += Abc_Tt7Isop( uOn1, uOnDc+2, 7, uRes1 );
        uOn2[0] = (uOn[0] & ~uRes0[0]) | (uOn[2] & ~uRes1[0]);
        uOn2[1] = (uOn[1] & ~uRes0[1]) | (uOn[3] & ~uRes1[1]);
        nCubes += Abc_Tt7Isop( uOn2, uOnDc2, 7, uRes2 );
        // derive the final truth table
        uRes[0] = uRes2[0] | uRes0[0];
        uRes[1] = uRes2[1] | uRes0[1];
        uRes[2] = uRes2[0] | uRes1[0];
        uRes[3] = uRes2[1] | uRes1[1];
        assert( (uOn[0] & ~uRes[0]) == 0 && (uOn[1] & ~uRes[1]) == 0 && (uOn[2] & ~uRes[2]) == 0 && (uOn[3] & ~uRes[3]) == 0 );
        assert( (uRes[0] & ~uOnDc[0])==0 && (uRes[1] & ~uOnDc[1])==0 && (uRes[2] & ~uOnDc[2])==0 && (uRes[3] & ~uOnDc[3])==0 );
    }
    return nCubes;
}

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

  Synopsis    [Computes CNF size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_Tt6CnfSize( word t, int nVars )
{
    int nCubes = 0;
    Abc_Tt6Isop(  t,  t, nVars, &nCubes );
    Abc_Tt6Isop( ~t, ~t, nVars, &nCubes );
    assert( nCubes <= 64 );
    return nCubes;
}
static inline int Abc_Tt8CnfSize( word t[4], int nVars )
{
    word uRes[4], tc[4] = {~t[0], ~t[1], ~t[2], ~t[3]};
    int nCubes = 0;
    nCubes += Abc_Tt8Isop( t,  t,  nVars, uRes );
    nCubes += Abc_Tt8Isop( tc, tc, nVars, uRes );
    assert( nCubes <= 256 );
    return nCubes;
}

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

  Synopsis    [Derives ISOP cover for the function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word Abc_Tt6IsopCover( word uOn, word uOnDc, int nVars, int * pCover, int * pnCubes )
{
    word uOn0, uOn1, uOnDc0, uOnDc1, uRes0, uRes1, uRes2;
    int c, Var, nBeg0, nEnd0, nEnd1;
    assert( nVars <= 6 );
    assert( (uOn & ~uOnDc) == 0 );
    if ( uOn == 0 )
        return 0;
    if ( uOnDc == ~(word)0 )
    {
        pCover[(*pnCubes)++] = 0;
        return ~(word)0;
    }
    assert( nVars > 0 );
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
        if ( Abc_Tt6HasVar( uOn, Var ) || Abc_Tt6HasVar( uOnDc, Var ) )
             break;
    assert( Var >= 0 );
    // cofactor
    uOn0   = Abc_Tt6Cofactor0( uOn,   Var );
    uOn1   = Abc_Tt6Cofactor1( uOn  , Var );
    uOnDc0 = Abc_Tt6Cofactor0( uOnDc, Var );
    uOnDc1 = Abc_Tt6Cofactor1( uOnDc, Var );
    // solve for cofactors
    nBeg0 = *pnCubes; 
    uRes0 = Abc_Tt6IsopCover( uOn0 & ~uOnDc1, uOnDc0, Var, pCover, pnCubes );
    nEnd0 = *pnCubes;
    uRes1 = Abc_Tt6IsopCover( uOn1 & ~uOnDc0, uOnDc1, Var, pCover, pnCubes );
    nEnd1 = *pnCubes;
    uRes2 = Abc_Tt6IsopCover( (uOn0 & ~uRes0) | (uOn1 & ~uRes1), uOnDc0 & uOnDc1, Var, pCover, pnCubes );
    // derive the final truth table
    uRes2 |= (uRes0 & s_Truths6Neg[Var]) | (uRes1 & s_Truths6[Var]);
    for ( c = nBeg0; c < nEnd0; c++ )
        pCover[c] |= (1 << (2*Var+0));
    for ( c = nEnd0; c < nEnd1; c++ )
        pCover[c] |= (1 << (2*Var+1));
    assert( (uOn & ~uRes2) == 0 );
    assert( (uRes2 & ~uOnDc) == 0 );
    return uRes2;
}
static inline void Abc_Tt7IsopCover( word uOn[2], word uOnDc[2], int nVars, word uRes[2], int * pCover, int * pnCubes )
{
    if ( nVars <= 6 || (uOn[0] == uOn[1] && uOnDc[0] == uOnDc[1]) )
        uRes[0] = uRes[1] = Abc_Tt6IsopCover( uOn[0], uOnDc[0], Abc_MinInt(nVars, 6), pCover, pnCubes );
    else
    {
        word uRes0, uRes1, uRes2;
        int c, nBeg0, nEnd0, nEnd1;
        assert( nVars == 7 );
        // solve for cofactors
        nBeg0 = *pnCubes; 
        uRes0 = Abc_Tt6IsopCover( uOn[0] & ~uOnDc[1], uOnDc[0], 6, pCover, pnCubes );   
        nEnd0 = *pnCubes;
        uRes1 = Abc_Tt6IsopCover( uOn[1] & ~uOnDc[0], uOnDc[1], 6, pCover, pnCubes );   
        nEnd1 = *pnCubes;
        uRes2 = Abc_Tt6IsopCover( (uOn[0] & ~uRes0) | (uOn[1] & ~uRes1), uOnDc[0] & uOnDc[1], 6, pCover, pnCubes );
        // derive the final truth table
        uRes[0] = uRes2 | uRes0;
        uRes[1] = uRes2 | uRes1;
        for ( c = nBeg0; c < nEnd0; c++ )
            pCover[c] |= (1 << (2*6+0));
        for ( c = nEnd0; c < nEnd1; c++ )
            pCover[c] |= (1 << (2*6+1));
        assert( (uOn[0] & ~uRes[0]) == 0 && (uOn[1] & ~uRes[1]) == 0 );
        assert( (uRes[0] & ~uOnDc[0])==0 && (uRes[1] & ~uOnDc[1])==0 );
    }
}
static inline void Abc_Tt8IsopCover( word uOn[4], word uOnDc[4], int nVars, word uRes[4], int * pCover, int * pnCubes )
{
    if ( nVars <= 6 )
        uRes[0] = uRes[1] = uRes[2] = uRes[3] = Abc_Tt6IsopCover( uOn[0], uOnDc[0], nVars, pCover, pnCubes );
    else if ( nVars == 7 || (uOn[0] == uOn[2] && uOn[1] == uOn[3] && uOnDc[0] == uOnDc[2] && uOnDc[1] == uOnDc[3]) )
    {
        Abc_Tt7IsopCover( uOn, uOnDc, 7, uRes, pCover, pnCubes );
        uRes[2] = uRes[0];
        uRes[3] = uRes[1];
    }
    else 
    {
        word uOn0[2], uOn1[2], uOn2[2], uOnDc2[2], uRes0[2], uRes1[2], uRes2[2];
        int c, nBeg0, nEnd0, nEnd1;
        assert( nVars == 8 );
        // cofactor
        uOn0[0] = uOn[0] & ~uOnDc[2];
        uOn0[1] = uOn[1] & ~uOnDc[3];
        uOn1[0] = uOn[2] & ~uOnDc[0];
        uOn1[1] = uOn[3] & ~uOnDc[1];
        uOnDc2[0] = uOnDc[0] & uOnDc[2];
        uOnDc2[1] = uOnDc[1] & uOnDc[3];
        // solve for cofactors
        nBeg0 = *pnCubes; 
        Abc_Tt7IsopCover( uOn0, uOnDc+0, 7, uRes0, pCover, pnCubes );
        nEnd0 = *pnCubes;
        Abc_Tt7IsopCover( uOn1, uOnDc+2, 7, uRes1, pCover, pnCubes );
        nEnd1 = *pnCubes;
        uOn2[0] = (uOn[0] & ~uRes0[0]) | (uOn[2] & ~uRes1[0]);
        uOn2[1] = (uOn[1] & ~uRes0[1]) | (uOn[3] & ~uRes1[1]);
        Abc_Tt7IsopCover( uOn2, uOnDc2, 7, uRes2, pCover, pnCubes );
        // derive the final truth table
        uRes[0] = uRes2[0] | uRes0[0];
        uRes[1] = uRes2[1] | uRes0[1];
        uRes[2] = uRes2[0] | uRes1[0];
        uRes[3] = uRes2[1] | uRes1[1];
        for ( c = nBeg0; c < nEnd0; c++ )
            pCover[c] |= (1 << (2*7+0));
        for ( c = nEnd0; c < nEnd1; c++ )
            pCover[c] |= (1 << (2*7+1));
        assert( (uOn[0] & ~uRes[0]) == 0 && (uOn[1] & ~uRes[1]) == 0 && (uOn[2] & ~uRes[2]) == 0 && (uOn[3] & ~uRes[3]) == 0 );
        assert( (uRes[0] & ~uOnDc[0])==0 && (uRes[1] & ~uOnDc[1])==0 && (uRes[2] & ~uOnDc[2])==0 && (uRes[3] & ~uOnDc[3])==0 );
    }
}

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

  Synopsis    [Computes CNF for the function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_Tt6Cnf( word t, int nVars, int * pCover )
{
    int c, nCubes = 0;
    Abc_Tt6IsopCover( t, t, nVars, pCover, &nCubes );
    for ( c = 0; c < nCubes; c++ )
        pCover[c] |= (1 << (2*nVars+0));
    Abc_Tt6IsopCover( ~t, ~t, nVars, pCover, &nCubes );
    for ( ; c < nCubes; c++ )
        pCover[c] |= (1 << (2*nVars+1));
    assert( nCubes <= 64 );
    return nCubes;
}
static inline int Abc_Tt8Cnf( word t[4], int nVars, int * pCover )
{
    word uRes[4], tc[4] = {~t[0], ~t[1], ~t[2], ~t[3]};
    int c, nCubes = 0;
    Abc_Tt8IsopCover( t,  t,  nVars, uRes, pCover, &nCubes );
    for ( c = 0; c < nCubes; c++ )
        pCover[c] |= (1 << (2*nVars+0));
    Abc_Tt8IsopCover( tc, tc, nVars, uRes, pCover, &nCubes );
    for ( ; c < nCubes; c++ )
        pCover[c] |= (1 << (2*nVars+1));
    assert( nCubes <= 256 );
    return nCubes;
}
Alan Mishchenko committed
2896

2897 2898 2899

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

2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
  Synopsis    [Computes ISOP for 6 variables or less.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_Tt6Esop( word t, int nVars, int * pCover )
{
    word c0, c1;
    int Var, r0, r1, r2, Max, i;
    assert( nVars <= 6 );
    if ( t == 0 )
        return 0;
    if ( t == ~(word)0 )
    {
        if ( pCover ) *pCover = 0;
        return 1;
    }
    assert( nVars > 0 );
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
        if ( Abc_Tt6HasVar( t, Var ) )
             break;
    assert( Var >= 0 );
    // cofactor
    c0 = Abc_Tt6Cofactor0( t, Var );
    c1 = Abc_Tt6Cofactor1( t, Var );
    // call recursively
    r0 = Abc_Tt6Esop( c0,      Var, pCover ? pCover : NULL );
    r1 = Abc_Tt6Esop( c1,      Var, pCover ? pCover + r0 : NULL );
    r2 = Abc_Tt6Esop( c0 ^ c1, Var, pCover ? pCover + r0 + r1 : NULL );
    Max = Abc_MaxInt( r0, Abc_MaxInt(r1, r2) );
    // add literals
    if ( pCover )
    {
        if ( Max == r0 )
        {
            for ( i = 0; i < r1; i++ )
                pCover[i] = pCover[r0+i];
            for ( i = 0; i < r2; i++ )
                pCover[r1+i] = pCover[r0+r1+i] | (1 << (2*Var+0));
        }
        else if ( Max == r1 )
        {
            for ( i = 0; i < r2; i++ )
                pCover[r0+i] = pCover[r0+r1+i] | (1 << (2*Var+1));
        }
        else
        {
            for ( i = 0; i < r0; i++ )
                pCover[i] |= (1 << (2*Var+0));
            for ( i = 0; i < r1; i++ )
                pCover[r0+i] |= (1 << (2*Var+1));
        }
    }
    return r0 + r1 + r2 - Max;
}
static inline word Abc_Tt6EsopBuild( int nVars, int * pCover, int nCubes )
{
    word p, t = 0; int c, v;
    for ( c = 0; c < nCubes; c++ )
    {
        p = ~(word)0;
        for ( v = 0; v < nVars; v++ )
            if ( ((pCover[c] >> (v << 1)) & 3) == 1 )
                p &= ~s_Truths6[v];
            else if ( ((pCover[c] >> (v << 1)) & 3) == 2 )
                p &= s_Truths6[v];
        t ^= p;
    }
    return t;
}
static inline int Abc_Tt6EsopVerify( word t, int nVars )
{
    int pCover[64];
    int nCubes = Abc_Tt6Esop( t, nVars, pCover );
    word t2 = Abc_Tt6EsopBuild( nVars, pCover, nCubes );
    if ( t != t2 )
        printf( "Verification failed.\n" );
    return nCubes;
}

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

2987 2988 2989 2990 2991 2992 2993 2994 2995
  Synopsis    [Check if the function is output-decomposable with the given var.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
2996
static inline int Abc_Tt6CheckOutDec( word t, int i, word * pOut )
2997 2998 2999
{
    word c0 = Abc_Tt6Cofactor0( t, i );
    word c1 = Abc_Tt6Cofactor1( t, i );
3000
    assert( c0 != c1 );
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
    if ( c0 == 0 ) //  F = i * G
    {
        if ( pOut ) *pOut = c1;
        return 0;
    }
    if ( c1 == 0 ) //  F = ~i * G
    {
        if ( pOut ) *pOut = c0;
        return 1;
    }
    if ( ~c0 == 0 ) //  F = ~i + G
    {
        if ( pOut ) *pOut = c1;
        return 2;
    }
    if ( ~c1 == 0 ) //  F = i + G
    {
        if ( pOut ) *pOut = c0;
        return 3;
    }
    if ( c0 == ~c1 ) //  F = i # G
    {
        if ( pOut ) *pOut = c0;
        return 4;
    }
    return -1;
}
3028
static inline int Abc_TtCheckOutDec( word * pTruth, int nVars, int v, word * pOut )
3029
{
3030 3031 3032 3033 3034 3035 3036
    word Cof0[4], Cof1[4];
    int nWords = Abc_TtWordNum(nVars);
    assert( nVars <= 8 );
    Abc_TtCofactor0p( Cof0, pTruth, nWords, v );
    Abc_TtCofactor1p( Cof1, pTruth, nWords, v );
    assert( !Abc_TtEqual(Cof0, Cof1, nWords) );
    if ( Abc_TtIsConst0(Cof0, nWords) ) //if ( c0 == 0 ) //  F = i * G
3037
    {
3038 3039
        if ( pOut ) Abc_TtCopy( pOut, Cof1, nWords, 0 ); //*pOut = c1;
        return 0;
3040
    }
3041
    if ( Abc_TtIsConst0(Cof1, nWords) ) //if ( c1 == 0 ) //  F = ~i * G
3042
    {
3043 3044
        if ( pOut ) Abc_TtCopy( pOut, Cof0, nWords, 0 ); //*pOut = c0;
        return 1;
3045
    }
3046
    if ( Abc_TtIsConst1(Cof0, nWords) ) //if ( ~c0 == 0 ) //  F = ~i + G
3047
    {
3048 3049
        if ( pOut ) Abc_TtCopy( pOut, Cof1, nWords, 0 ); //*pOut = c1;
        return 2;
3050
    }
3051
    if ( Abc_TtIsConst1(Cof1, nWords) ) //if ( ~c1 == 0 ) //  F = i + G
3052
    {
3053 3054
        if ( pOut ) Abc_TtCopy( pOut, Cof0, nWords, 0 ); //*pOut = c0;
        return 3;
3055
    }
3056
    if ( Abc_TtOpposite(Cof0, Cof1, nWords) ) //if ( c0 == ~c1 )  //  F = i # G
3057
    {
3058 3059
        if ( pOut ) Abc_TtCopy( pOut, Cof0, nWords, 0 ); //*pOut = c0;
        return 4;
3060 3061 3062 3063 3064
    }
    return -1;
}
static inline word Abc_TtCheckDecOutOne7( word * t, int * piVar, int * pType )
{
3065
    int v, Type, Type2; word Out[2];
3066
    for ( v = 6; v >= 0; v-- )
3067
        if ( (Type = Abc_TtCheckOutDec(t, 7, v, NULL)) != -1 )
3068
        {
3069 3070
            Abc_TtSwapVars( t, 7, 6, v );
            Type2 = Abc_TtCheckOutDec( t, 7, 6, Out );
3071 3072 3073
            assert( Type == Type2 );
            *piVar = v;
            *pType = Type;
3074
            return Out[0];
3075 3076 3077 3078 3079
        }
    return 0;
}
static inline word Abc_TtCheckDecOutOne8( word * t, int * piVar1, int * piVar2, int * pType1, int * pType2 )
{
3080
    int v, Type1, Type12, Type2, Type22; word Out[4], Out2[2];
3081
    for ( v = 7; v >= 0; v-- )
3082
        if ( (Type1 = Abc_TtCheckOutDec(t, 8, v, NULL)) != -1 )
3083
        {
3084 3085
            Abc_TtSwapVars( t, 8, 7, v );
            Type12 = Abc_TtCheckOutDec( t, 8, 7, Out );
3086 3087
            assert( Type1 == Type12 );
            *piVar1 = v;
3088
            *pType1 = Type1;
3089 3090 3091 3092 3093
            break;
        }
    if ( v == -1 )
        return 0;
    for ( v = 6; v >= 0; v-- )
3094
        if ( (Type2 = Abc_TtCheckOutDec(Out, 7, v, NULL)) != -1 && Abc_Lit2Var(Type2) == Abc_Lit2Var(Type1) )
3095
        {
3096 3097
            Abc_TtSwapVars( Out, 7, 6, v );
            Type22 = Abc_TtCheckOutDec(Out, 7, 6, Out2);
3098 3099 3100 3101
            assert( Type2 == Type22 );
            *piVar2 = v;
            *pType2 = Type2;
            assert( *piVar2 < *piVar1 );
3102
            return Out2[0];
3103 3104 3105
        }
    return 0;
}
3106 3107 3108 3109

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

  Synopsis    [Check if the function is input-decomposable with the given pair.]
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Abc_TtCheckDsdAnd( word t, int i, int j, word * pOut )
{
    word c0 = Abc_Tt6Cofactor0( t, i );
    word c1 = Abc_Tt6Cofactor1( t, i );
    word c00 = Abc_Tt6Cofactor0( c0, j );
    word c01 = Abc_Tt6Cofactor1( c0, j );
    word c10 = Abc_Tt6Cofactor0( c1, j );
    word c11 = Abc_Tt6Cofactor1( c1, j );
    if ( c00 == c01 && c00 == c10 ) //  i *  j
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c00) | (s_Truths6[i] & c11);
        return 0;
    }
    if ( c11 == c00 && c11 == c10 ) //  i * !j
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c11) | (s_Truths6[i] & c01);
        return 1;
    }
    if ( c11 == c00 && c11 == c01 ) // !i *  j
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c11) | (s_Truths6[i] & c10);
        return 2;
    }
    if ( c11 == c01 && c11 == c10 ) // !i * !j
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c11) | (s_Truths6[i] & c00);
        return 3;
    }
    if ( c00 == c11 && c01 == c10 )
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c11) | (s_Truths6[i] & c10);
        return 4;
    }
    return -1;
}
static inline int Abc_TtCheckDsdMux( word t, int i, word * pOut )
{
    word c0 = Abc_Tt6Cofactor0( t, i );
    word c1 = Abc_Tt6Cofactor1( t, i );
    word c00, c01, c10, c11;
    int k, fPres0, fPres1, iVar0 = -1, iVar1 = -1;
    for ( k = 0; k < 6; k++ )
    {
        if ( k == i ) continue;
        fPres0 = Abc_Tt6HasVar( c0, k );
        fPres1 = Abc_Tt6HasVar( c1, k );
        if ( fPres0 && !fPres1 )
        {
            if ( iVar0 >= 0 )
                return -1;
            iVar0 = k;
        }
        if ( !fPres0 && fPres1 )
        {
            if ( iVar1 >= 0 )
                return -1;
            iVar1 = k;
        }
    }
    if ( iVar0 == -1 || iVar1 == -1 )
        return -1;
    c00 = Abc_Tt6Cofactor0( c0, iVar0 );
    c01 = Abc_Tt6Cofactor1( c0, iVar0 );
    c10 = Abc_Tt6Cofactor0( c1, iVar1 );
    c11 = Abc_Tt6Cofactor1( c1, iVar1 );
    if ( c00 ==  c10 && c01 ==  c11 ) //  ITE(i,  iVar1,  iVar0)
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c10) | (s_Truths6[i] & c11);
        return (Abc_Var2Lit(iVar1, 0) << 16) | Abc_Var2Lit(iVar0, 0);
    }
    if ( c00 == ~c10 && c01 == ~c11 ) //  ITE(i,  iVar1, !iVar0)
    {
        if ( pOut ) *pOut = (~s_Truths6[i] & c10) | (s_Truths6[i] & c11);
        return (Abc_Var2Lit(iVar1, 0) << 16) | Abc_Var2Lit(iVar0, 1);
    }
    return -1;
}
static inline void Unm_ManCheckTest2()
{
    word t, t1, Out, Var0, Var1, Var0_, Var1_;
    int iVar0, iVar1, i, Res;
    for ( iVar0 = 0; iVar0 < 6; iVar0++ )
    for ( iVar1 = 0; iVar1 < 6; iVar1++ )
    {
        if ( iVar0 == iVar1 )
            continue;
        Var0 = s_Truths6[iVar0];
        Var1 = s_Truths6[iVar1];
        for ( i = 0; i < 5; i++ )
        {
            Var0_ = ((i >> 0) & 1) ? ~Var0 : Var0;
            Var1_ = ((i >> 1) & 1) ? ~Var1 : Var1;

            t = Var0_ & Var1_;
            if ( i == 4 )
                t = ~(Var0_ ^ Var1_);

//            Kit_DsdPrintFromTruth( (unsigned *)&t, 6 ), printf( "\n" );

            Res = Abc_TtCheckDsdAnd( t, iVar0, iVar1, &Out );
            if ( Res == -1 )
            {
                printf( "No decomposition\n" );
                continue;
            }

            Var0_ = s_Truths6[iVar0];
            Var0_ = ((Res >> 0) & 1) ? ~Var0_ : Var0_;

            Var1_ = s_Truths6[iVar1];
            Var1_ = ((Res >> 1) & 1) ? ~Var1_ : Var1_;

            t1 = Var0_ & Var1_;
            if ( Res == 4 )
                t1 = Var0_ ^ Var1_;

            t1 = (~t1 & Abc_Tt6Cofactor0(Out, iVar0)) | (t1 & Abc_Tt6Cofactor1(Out, iVar0));

//            Kit_DsdPrintFromTruth( (unsigned *)&t1, 6 ), printf( "\n" );

            if ( t1 != t )
                printf( "Verification failed.\n" );
            else
                printf( "Verification succeeded.\n" );
        }
    }
}
static inline void Unm_ManCheckTest()
{
    word t, t1, Out, Ctrl, Var0, Var1, Ctrl_, Var0_, Var1_;
    int iVar0, iVar1, iCtrl, i, Res;
    for ( iCtrl = 0; iCtrl < 6; iCtrl++ )
    for ( iVar0 = 0; iVar0 < 6; iVar0++ )
    for ( iVar1 = 0; iVar1 < 6; iVar1++ )
    {
        if ( iCtrl == iVar0 || iCtrl == iVar1 || iVar0 == iVar1 )
            continue;
        Ctrl = s_Truths6[iCtrl];
        Var0 = s_Truths6[iVar0];
        Var1 = s_Truths6[iVar1];
        for ( i = 0; i < 8; i++ )
        {
            Ctrl_ = ((i >> 0) & 1) ? ~Ctrl : Ctrl;
            Var0_ = ((i >> 1) & 1) ? ~Var0 : Var0;
            Var1_ = ((i >> 2) & 1) ? ~Var1 : Var1;

            t = (~Ctrl_ & Var0_) | (Ctrl_ & Var1_);

//            Kit_DsdPrintFromTruth( (unsigned *)&t, 6 ), printf( "\n" );

            Res = Abc_TtCheckDsdMux( t, iCtrl, &Out );
            if ( Res == -1 )
            {
                printf( "No decomposition\n" );
                continue;
            }

//            Kit_DsdPrintFromTruth( (unsigned *)&Out, 6 ), printf( "\n" );

            Ctrl_ = s_Truths6[iCtrl];
            Var0_ = s_Truths6[Abc_Lit2Var(Res & 0xFFFF)];
            Var0_ = Abc_LitIsCompl(Res & 0xFFFF) ? ~Var0_ : Var0_;

            Res >>= 16;
            Var1_ = s_Truths6[Abc_Lit2Var(Res & 0xFFFF)];
            Var1_ = Abc_LitIsCompl(Res & 0xFFFF) ? ~Var1_ : Var1_;

            t1 = (~Ctrl_ & Var0_) | (Ctrl_ & Var1_);

//            Kit_DsdPrintFromTruth( (unsigned *)&t1, 6 ), printf( "\n" );
//            Kit_DsdPrintFromTruth( (unsigned *)&Out, 6 ), printf( "\n" );

            t1 = (~t1 & Abc_Tt6Cofactor0(Out, iCtrl)) | (t1 & Abc_Tt6Cofactor1(Out, iCtrl));

//            Kit_DsdPrintFromTruth( (unsigned *)&t1, 6 ), printf( "\n" );

            if ( t1 != t )
                printf( "Verification failed.\n" );
            else
                printf( "Verification succeeded.\n" );
        }
    }
}


3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
/**Function*************************************************************

  Synopsis    [Truth table evaluation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word Abc_TtEvalLut6( word Ins[6], word Lut, int nVars )
{
    word Cube, Res = 0; int k, i;
    for ( k = 0; k < (1<<nVars); k++ )
    {
        if ( ((Lut >> k) & 1) == 0 )
            continue;
        Cube = ~(word)0;
        for ( i = 0; i < nVars; i++ )
            Cube &= ((k >> i) & 1) ? Ins[i] : ~Ins[i];
3324
        Res |= Cube;
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337
    }
    return Res;
}
static inline unsigned Abc_TtEvalLut5( unsigned Ins[5], int Lut, int nVars )
{
    unsigned Cube, Res = 0; int k, i;
    for ( k = 0; k < (1<<nVars); k++ )
    {
        if ( ((Lut >> k) & 1) == 0 )
            continue;
        Cube = ~(unsigned)0;
        for ( i = 0; i < nVars; i++ )
            Cube &= ((k >> i) & 1) ? Ins[i] : ~Ins[i];
3338
        Res |= Cube;
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351
    }
    return Res;
}
static inline int Abc_TtEvalLut4( int Ins[4], int Lut, int nVars )
{
    int Cube, Res = 0; int k, i;
    for ( k = 0; k < (1<<nVars); k++ )
    {
        if ( ((Lut >> k) & 1) == 0 )
            continue;
        Cube = ~(int)0;
        for ( i = 0; i < nVars; i++ )
            Cube &= ((k >> i) & 1) ? Ins[i] : ~Ins[i];
3352
        Res |= Cube;
3353 3354 3355 3356
    }
    return Res & ~(~0 << (1<<nVars));
}

3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515

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

  Synopsis    [Checks existence of bi-decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Abc_TtComputeGraph( word * pTruth, int v, int nVars, int * pGraph )
{
    word Cof0[64], Cof1[64]; // pow( 2, nVarsMax-6 )
    word Cof00[64], Cof01[64], Cof10[64], Cof11[64];
    word CofXor, CofAndTest; 
    int i, w, nWords = Abc_TtWordNum(nVars);
    pGraph[v] |= (1 << v);
    if ( v == nVars - 1 )
        return;
    assert( v < nVars - 1 );
    Abc_TtCofactor0p( Cof0, pTruth, nWords, v );
    Abc_TtCofactor1p( Cof1, pTruth, nWords, v );
    for ( i = v + 1; i < nVars; i++ )
    {
        Abc_TtCofactor0p( Cof00, Cof0, nWords, i );
        Abc_TtCofactor1p( Cof01, Cof0, nWords, i );
        Abc_TtCofactor0p( Cof10, Cof1, nWords, i );
        Abc_TtCofactor1p( Cof11, Cof1, nWords, i );
        for ( w = 0; w < nWords; w++ )
        {
            CofXor     =  Cof00[w] ^ Cof01[w]  ^  Cof10[w] ^ Cof11[w];
            CofAndTest = (Cof00[w] & Cof01[w]) | (Cof10[w] & Cof11[w]);
            if ( CofXor & CofAndTest )
            {
                pGraph[v] |= (1 << i);
                pGraph[i] |= (1 << v);
            }
            else if ( CofXor & ~CofAndTest )
            {
                pGraph[v] |= (1 << (16+i));
                pGraph[i] |= (1 << (16+v));
            }
        }
    }
}
static inline void Abc_TtPrintVarSet( int Mask, int nVars )
{
    int i;
    for ( i = 0; i < nVars; i++ )
        if ( (Mask >> i) & 1 )
            printf( "1" );
        else
            printf( "." );
}
static inline void Abc_TtPrintBiDec( word * pTruth, int nVars )
{
    int v, pGraph[12] = {0};
    assert( nVars <= 12 );
    for ( v = 0; v < nVars; v++ )
    {
        Abc_TtComputeGraph( pTruth, v, nVars, pGraph );
        Abc_TtPrintVarSet( pGraph[v], nVars );
        printf( "    " );
        Abc_TtPrintVarSet( pGraph[v] >> 16, nVars );
        printf( "\n" );
    }
}
static inline int Abc_TtVerifyBiDec( word * pTruth, int nVars, int This, int That, int nSuppLim, word wThis, word wThat )
{
    int pVarsThis[12], pVarsThat[12], pVarsAll[12];
    int nThis = Abc_TtBitCount16(This);
    int nThat = Abc_TtBitCount16(That);
    int i, k, nWords = Abc_TtWordNum(nVars);
    word pThis[64] = {wThis}, pThat[64] = {wThat};
    assert( nVars <= 12 );
    for ( i = 0; i < nVars; i++ )
        pVarsAll[i] = i;
    for ( i = k = 0; i < nVars; i++ )
        if ( (This >> i) & 1 )
            pVarsThis[k++] = i;
    assert( k == nThis );
    for ( i = k = 0; i < nVars; i++ )
        if ( (That >> i) & 1 )
            pVarsThat[k++] = i;
    assert( k == nThat );
    Abc_TtStretch6( pThis, nThis, nVars );
    Abc_TtStretch6( pThat, nThat, nVars );
    Abc_TtExpand( pThis, nVars, pVarsThis, nThis, pVarsAll, nVars );
    Abc_TtExpand( pThat, nVars, pVarsThat, nThat, pVarsAll, nVars );
    for ( k = 0; k < nWords; k++ )
        if ( pTruth[k] != (pThis[k] & pThat[k]) )
            return 0;
    return 1;
}
static inline void Abc_TtExist( word * pTruth, int iVar, int nWords )
{
    word Cof0[64], Cof1[64]; 
    Abc_TtCofactor0p( Cof0, pTruth, nWords, iVar );
    Abc_TtCofactor1p( Cof1, pTruth, nWords, iVar );
    Abc_TtOr( pTruth, Cof0, Cof1, nWords );
}
static inline int Abc_TtCheckBiDec( word * pTruth, int nVars, int This, int That )
{
    int VarMask[2] = {This & ~That, That & ~This};
    int v, c, nWords = Abc_TtWordNum(nVars);
    word pTempR[2][64]; 
    for ( c = 0; c < 2; c++ )
    {
        Abc_TtCopy( pTempR[c], pTruth, nWords, 0 );
        for ( v = 0; v < nVars; v++ )
            if ( ((VarMask[c] >> v) & 1) )
                Abc_TtExist( pTempR[c], v, nWords );
    }
    for ( v = 0; v < nWords; v++ )
        if ( ~pTruth[v] & pTempR[0][v] & pTempR[1][v] )
            return 0;
    return 1;
}
static inline word Abc_TtDeriveBiDecOne( word * pTruth, int nVars, int This )
{
    word pTemp[64]; 
    int nThis = Abc_TtBitCount16(This);
    int v, nWords = Abc_TtWordNum(nVars);
    Abc_TtCopy( pTemp, pTruth, nWords, 0 );
    for ( v = 0; v < nVars; v++ )
        if ( !((This >> v) & 1) )
            Abc_TtExist( pTemp, v, nWords );
    Abc_TtShrink( pTemp, nThis, nVars, This );
    return Abc_Tt6Stretch( pTemp[0], nThis );
}
static inline void Abc_TtDeriveBiDec( word * pTruth, int nVars, int This, int That, int nSuppLim, word * pThis, word * pThat )
{
    assert( Abc_TtBitCount16(This) <= nSuppLim );
    assert( Abc_TtBitCount16(That) <= nSuppLim );
    pThis[0] = Abc_TtDeriveBiDecOne( pTruth, nVars, This );
    pThat[0] = Abc_TtDeriveBiDecOne( pTruth, nVars, That );
    if ( !Abc_TtVerifyBiDec(pTruth, nVars, This, That, nSuppLim, pThis[0], pThat[0] ) )
        printf( "Bi-decomposition verification failed.\n" );
}
// detect simple case of decomposition with topmost AND gate
static inline int Abc_TtCheckBiDecSimple( word * pTruth, int nVars, int nSuppLim )
{
    word Cof0[64], Cof1[64]; 
    int v, Res = 0, nDecVars = 0, nWords = Abc_TtWordNum(nVars);
    for ( v = 0; v < nVars; v++ )
    {
        Abc_TtCofactor0p( Cof0, pTruth, nWords, v );
        Abc_TtCofactor1p( Cof1, pTruth, nWords, v );
        if ( !Abc_TtIsConst0(Cof0, nWords) && !Abc_TtIsConst0(Cof1, nWords) )
            continue;
        nDecVars++;
        Res |= 1 << v;
        if ( nDecVars >= nVars - nSuppLim )
            return ((Res ^ (int)Abc_Tt6Mask(nVars)) << 16) | Res;
    }
    return 0;
}
3516
static inline int Abc_TtProcessBiDecInt( word * pTruth, int nVars, int nSuppLim )
3517 3518
{
    int i, v, Res, nSupp, CountShared = 0, pGraph[12] = {0};
3519
    assert( nSuppLim < nVars && nVars <= 2 * nSuppLim && nVars <= 12 );
3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
    assert( 2 <= nSuppLim && nSuppLim <= 6 );
    Res = Abc_TtCheckBiDecSimple( pTruth, nVars, nSuppLim );
    if ( Res )
        return Res;
    for ( v = 0; v < nVars; v++ )
    {
        Abc_TtComputeGraph( pTruth, v, nVars, pGraph );
        nSupp = Abc_TtBitCount16(pGraph[v] & 0xFFFF);
        if ( nSupp > nSuppLim ) 
        {
            // this variable is shared - check if the limit is exceeded
            if ( ++CountShared > 2*nSuppLim - nVars )
                return 0;
        }
        else if ( nVars - nSupp <= nSuppLim )
        {
            int This = pGraph[v] & 0xFFFF;
            int That = This ^ (int)Abc_Tt6Mask(nVars);
            // find the other component
            int Graph = That;
            for ( i = 0; i < nVars; i++ )
                if ( (That >> i) & 1 )
                    Graph |= pGraph[i] & 0xFFFF;
            // check if this can be done
            if ( Abc_TtBitCount16(Graph) > nSuppLim )
                continue;
            // try decomposition
            if ( Abc_TtCheckBiDec(pTruth, nVars, This, Graph) )
                return (Graph << 16) | This;
        }
    }
    return 0;
}
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566
static inline int Abc_TtProcessBiDec( word * pTruth, int nVars, int nSuppLim )
{
    word pFunc[64];
    int Res, nWords = Abc_TtWordNum(nVars);
    Abc_TtCopy( pFunc, pTruth, nWords, 0 );
    Res = Abc_TtProcessBiDecInt( pFunc, nVars, nSuppLim );
    if ( Res )
        return Res;
    Abc_TtCopy( pFunc, pTruth, nWords, 1 );
    Res = Abc_TtProcessBiDecInt( pFunc, nVars, nSuppLim );
    if ( Res )
        return Res | (1 << 30);
    return 0;
}
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581

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

  Synopsis    [Tests decomposition procedures.]

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
static inline void Abc_TtProcessBiDecTest( word * pTruth, int nVars, int nSuppLim )
{
    word This, That, pTemp[64];
Alan Mishchenko committed
3582
    int Res, resThis, resThat;//, nThis, nThat;
3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
    int nWords = Abc_TtWordNum(nVars);
    Abc_TtCopy( pTemp, pTruth, nWords, 0 );
    Res = Abc_TtProcessBiDec( pTemp, nVars, nSuppLim );
    if ( Res == 0 )
    {
        //Dau_DsdPrintFromTruth( pTemp, nVars );
        //printf( "Non_dec\n\n" );
        return;
    }

    resThis = Res & 0xFFFF;
    resThat = Res >> 16;

    Abc_TtDeriveBiDec( pTemp, nVars, resThis, resThat, nSuppLim, &This, &That );
    return;

    //if ( !(resThis & resThat) )
    //    return;

//    Dau_DsdPrintFromTruth( pTemp, nVars );

Alan Mishchenko committed
3604 3605
    //nThis = Abc_TtBitCount16(resThis);
    //nThat = Abc_TtBitCount16(resThat);
3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636

    printf( "Variable sets:    " );
    Abc_TtPrintVarSet( resThis, nVars );
    printf( "    " );
    Abc_TtPrintVarSet( resThat, nVars );
    printf( "\n" );
    Abc_TtDeriveBiDec( pTemp, nVars, resThis, resThat, nSuppLim, &This, &That );
//    Dau_DsdPrintFromTruth( &This, nThis );
//    Dau_DsdPrintFromTruth( &That, nThat );
    printf( "\n" );
}
static inline void Abc_TtProcessBiDecExperiment()
{
    int nVars = 3;
    int nSuppLim = 2;
    int Res, resThis, resThat;
    word This, That;
//    word t = ABC_CONST(0x8000000000000000);
//    word t = (s_Truths6[0] | s_Truths6[1]) & (s_Truths6[2] | s_Truths6[3] | s_Truths6[4] | s_Truths6[5]);
//    word t = ((s_Truths6[0] & s_Truths6[1]) | (~s_Truths6[1] & s_Truths6[2]));
    word t = ((s_Truths6[0] | s_Truths6[1]) & (s_Truths6[1] | s_Truths6[2]));
    Abc_TtPrintBiDec( &t, nVars );
    Res = Abc_TtProcessBiDec( &t, nVars, nSuppLim );
    resThis = Res & 0xFFFF;
    resThat = Res >> 16;
    Abc_TtDeriveBiDec( &t, nVars, resThis, resThat, nSuppLim, &This, &That );
//    Dau_DsdPrintFromTruth( &This, Abc_TtBitCount16(resThis) );
//    Dau_DsdPrintFromTruth( &That, Abc_TtBitCount16(resThat) );
    nVars = nSuppLim;
}

3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
/**Function*************************************************************

  Synopsis    [Truth table checking procedure.]

  Description []
               
  SideEffects []

  SeeAlso     [] 

***********************************************************************/
static inline int Abc_Tt4Equal3( int c0, int c1, int c2, int c3 )
{
    if ( c0 == c1 && c0 == c2 ) return 3;
    if ( c0 == c1 && c0 == c3 ) return 2;
    if ( c0 == c3 && c0 == c2 ) return 1;
    if ( c3 == c1 && c3 == c2 ) return 0;
    return -1;
}
static inline int Abc_Tt4Check2( int t, int i, int j, int * f, int * r )
{
    int c0  =  t & r[j];
    int c1  = (t & f[j]) >> (1 << j);
    int c00 =  c0 & r[i];
    int c01 = (c0 & f[i]) >> (1 << i);
    int c10 =  c1 & r[i];
    int c11 = (c1 & f[i]) >> (1 << i);
    return Abc_Tt4Equal3( c00, c01, c10, c11 );
}
static inline int Abc_Tt4CheckTwoLevel( int t )
{
    int pair1, pair2;
    int f[4] = { 0xAAAA, 0xCCCC, 0xF0F0, 0xFF00 };
    int r[4] = { 0x5555, 0x3333, 0x0F0F, 0x00FF };
    if ( (pair1 = Abc_Tt4Check2(t, 0, 1, f, r)) >= 0 && (pair2 = Abc_Tt4Check2(t, 2, 3, f, r)) >= 0 ) return (1 << 4) | (pair2 << 2) | pair1;
    if ( (pair1 = Abc_Tt4Check2(t, 0, 2, f, r)) >= 0 && (pair2 = Abc_Tt4Check2(t, 1, 3, f, r)) >= 0 ) return (2 << 4) | (pair2 << 2) | pair1;
    if ( (pair1 = Abc_Tt4Check2(t, 0, 3, f, r)) >= 0 && (pair2 = Abc_Tt4Check2(t, 1, 2, f, r)) >= 0 ) return (3 << 4) | (pair2 << 2) | pair1;
    return -1;
}
Alan Mishchenko committed
3676
static inline int Abc_Tt4CountOnes( int t )
3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
{
    t = (t & (0x5555)) + ((t >> 1) & (0x5555));
    t = (t & (0x3333)) + ((t >> 2) & (0x3333));
    t = (t & (0x0f0f)) + ((t >> 4) & (0x0f0f));
    t = (t & (0x00ff)) + ((t >> 8) & (0x00ff));
    return t;
}
static inline int Abc_Tt4FirstBit( int t )
{
    int n = 0;
    if ( t == 0 ) return -1;
    if ( (t & 0x00FF) == 0 ) { n +=  8; t >>=  8; }
    if ( (t & 0x000F) == 0 ) { n +=  4; t >>=  4; }
    if ( (t & 0x0003) == 0 ) { n +=  2; t >>=  2; }
    if ( (t & 0x0001) == 0 ) { n++; }
    return n;
}
static inline int Abc_Tt4Check( int t )
{
    int Count, tn = 0xFFFF & ~t;
    if ( t == 0x6996 || tn == 0x6996 ) return 1;
    if ( (t & (t-1)) == 0 )            return 1;
    if ( (tn & (tn-1)) == 0 )          return 1;
    Count = Abc_Tt4CountOnes( t );
    if ( Count == 7 && Abc_Tt4CheckTwoLevel(t)  > 0 ) return 1;
    if ( Count == 9 && Abc_Tt4CheckTwoLevel(tn) > 0 ) return 1;
    return 0;
}

3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717

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

  Synopsis    [Returns symmetry profile of the function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731
static inline int Abc_Tt6VarsAreSymmetric( word t, int iVar, int jVar )
{
    word * s_PMasks = s_PPMasks[iVar][jVar];
    int shift = (1 << jVar) - (1 << iVar);
    assert( iVar < jVar );
    return ((t & s_PMasks[1]) << shift) == (t & s_PMasks[2]);
}
static inline int Abc_Tt6VarsAreAntiSymmetric( word t, int iVar, int jVar )
{
    word * s_PMasks = s_PPMasks[iVar][jVar];
    int shift = (1 << jVar) + (1 << iVar);
    assert( iVar < jVar );
    return ((t & (s_PMasks[1] >> (1 << iVar))) << shift) == (t & (s_PMasks[2] << (1 << iVar)));
}
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
static inline int Abc_TtVarsAreSymmetric( word * pTruth, int nVars, int i, int j, word * pCof0, word * pCof1 )
{
    int nWords = Abc_TtWordNum( nVars );
    assert( i < nVars && j < nVars );
    Abc_TtCofactor0p( pCof0, pTruth, nWords, i );
    Abc_TtCofactor1p( pCof1, pTruth, nWords, i );
    Abc_TtCofactor1( pCof0, nWords, j );
    Abc_TtCofactor0( pCof1, nWords, j );
    return Abc_TtEqual( pCof0, pCof1, nWords );
}
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751
static inline int Abc_TtVarsAreAntiSymmetric( word * pTruth, int nVars, int i, int j, word * pCof0, word * pCof1 )
{
    int nWords = Abc_TtWordNum( nVars );
    assert( i < nVars && j < nVars );
    Abc_TtCofactor0p( pCof0, pTruth, nWords, i );
    Abc_TtCofactor1p( pCof1, pTruth, nWords, i );
    Abc_TtCofactor0( pCof0, nWords, j );
    Abc_TtCofactor1( pCof1, nWords, j );
    return Abc_TtEqual( pCof0, pCof1, nWords );
}
3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
static inline int Abc_TtIsFullySymmetric( word * pTruth, int nVars )
{
    int m, v, Polar = 0, Seen = 0; 
    for ( m = 0; m < (1<<nVars); m++ )
    {
        int Count = 0;
        int Value = Abc_TtGetBit( pTruth, m );
        for ( v = 0; v < nVars; v++ )
            Count += ((m >> v) & 1);
        if ( (Seen >> Count) & 1 ) // seen this count
        {
            if ( Value != ((Polar >> Count) & 1) )
                return -1;
        }
        else // new count
        {
            Seen  |= 1 << Count;
            if ( Value )
                Polar |= 1 << Count;
        }
    }
    return Polar;
}
static inline void Abc_TtGenFullySymmetric( word * pTruth, int nVars, int Polar )
{
    int m, v, nWords = Abc_TtWordNum( nVars );
    Abc_TtClear( pTruth, nWords );
    for ( m = 0; m < (1<<nVars); m++ )
    {
        int Count = 0;
        for ( v = 0; v < nVars; v++ )
            Count += ((m >> v) & 1);
        if ( (Polar >> Count) & 1 )
            Abc_TtSetBit( pTruth, m );
    }
}
static inline void Abc_TtTestFullySymmetric()
{
    word pTruth[4]; // 8-var function
    int PolarOut, PolarIn = 271;
    Abc_TtGenFullySymmetric( pTruth, 8, PolarIn );
    //Abc_TtXorBit( pTruth, 171 );
    PolarOut = Abc_TtIsFullySymmetric( pTruth, 8 );
    assert( PolarIn == PolarOut );
}


3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824
/**Function*************************************************************

  Synopsis    [Generates truth table of a symmetric function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word * Abc_TtSymFunGenerate( char * pOnes, int nVars )
{
    int m, k, Count;
    word * pTruth = ABC_CALLOC( word, Abc_TtWordNum(nVars) );
    assert( (int)strlen(pOnes) == nVars + 1 );
    for ( m = 0; m < (1 << nVars); m++ )
    {
        Count = 0;
        for ( k = 0; k < nVars; k++ )
            Count += (m >> k) & 1;
        if ( pOnes[Count] == '1' )
            Abc_TtXorBit( pTruth, m );
    }
    return pTruth;
}
3825 3826 3827 3828 3829 3830 3831 3832 3833


ABC_NAMESPACE_HEADER_END

#endif

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