abc/src/bool/kit/kitTruth.c
/**Function*************************************************************

  Synopsis    [Fast counting minterms in the cofactors of a function.]

  Description [Returns the total number of minterms in the function.
  The resulting array (pRes) contains the number of minterms in 0-cofactor
  w.r.t. each variables. The additional array (pBytes) is used for internal 
  storage. It should have the size equal to the number of truth table bytes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Function
Synopsis    [Fast counting minterms in the cofactors of a function.]

Description [Returns the total number of minterms in the function.
The resulting array (pRes) contains the number of minterms in 0-cofactor
w.r.t. each variables. The additional array (pBytes) is used for internal
storage. It should have the size equal to the number of truth table bytes.]




Function Synopsis [ Fast counting minterms in the cofactors of a function . ] Description [ Returns the total number of minterms in the function . The resulting array ( pRes ) contains the number of minterms in 0 - cofactor w . r . t . each variables . The additional array ( pBytes ) is used for internal storage . It should have the size equal to the number of truth table bytes . ]
int Kit_TruthCountMinterms( unsigned * pTruth, int nVars, int * pRes, int * pBytesInit )
{
    static unsigned Table[256] = {
        0x00000000, 0x01010101, 0x01010001, 0x02020102, 0x01000101, 0x02010202, 0x02010102, 0x03020203,
        0x01000001, 0x02010102, 0x02010002, 0x03020103, 0x02000102, 0x03010203, 0x03010103, 0x04020204,
        0x00010101, 0x01020202, 0x01020102, 0x02030203, 0x01010202, 0x02020303, 0x02020203, 0x03030304,
        0x01010102, 0x02020203, 0x02020103, 0x03030204, 0x02010203, 0x03020304, 0x03020204, 0x04030305,
        0x00010001, 0x01020102, 0x01020002, 0x02030103, 0x01010102, 0x02020203, 0x02020103, 0x03030204,
        0x01010002, 0x02020103, 0x02020003, 0x03030104, 0x02010103, 0x03020204, 0x03020104, 0x04030205,
        0x00020102, 0x01030203, 0x01030103, 0x02040204, 0x01020203, 0x02030304, 0x02030204, 0x03040305,
        0x01020103, 0x02030204, 0x02030104, 0x03040205, 0x02020204, 0x03030305, 0x03030205, 0x04040306,
        0x00000101, 0x01010202, 0x01010102, 0x02020203, 0x01000202, 0x02010303, 0x02010203, 0x03020304,
        0x01000102, 0x02010203, 0x02010103, 0x03020204, 0x02000203, 0x03010304, 0x03010204, 0x04020305,
        0x00010202, 0x01020303, 0x01020203, 0x02030304, 0x01010303, 0x02020404, 0x02020304, 0x03030405,
        0x01010203, 0x02020304, 0x02020204, 0x03030305, 0x02010304, 0x03020405, 0x03020305, 0x04030406,
        0x00010102, 0x01020203, 0x01020103, 0x02030204, 0x01010203, 0x02020304, 0x02020204, 0x03030305,
        0x01010103, 0x02020204, 0x02020104, 0x03030205, 0x02010204, 0x03020305, 0x03020205, 0x04030306,
        0x00020203, 0x01030304, 0x01030204, 0x02040305, 0x01020304, 0x02030405, 0x02030305, 0x03040406,
        0x01020204, 0x02030305, 0x02030205, 0x03040306, 0x02020305, 0x03030406, 0x03030306, 0x04040407,
        0x00000001, 0x01010102, 0x01010002, 0x02020103, 0x01000102, 0x02010203, 0x02010103, 0x03020204,
        0x01000002, 0x02010103, 0x02010003, 0x03020104, 0x02000103, 0x03010204, 0x03010104, 0x04020205,
        0x00010102, 0x01020203, 0x01020103, 0x02030204, 0x01010203, 0x02020304, 0x02020204, 0x03030305,
        0x01010103, 0x02020204, 0x02020104, 0x03030205, 0x02010204, 0x03020305, 0x03020205, 0x04030306,
        0x00010002, 0x01020103, 0x01020003, 0x02030104, 0x01010103, 0x02020204, 0x02020104, 0x03030205,
        0x01010003, 0x02020104, 0x02020004, 0x03030105, 0x02010104, 0x03020205, 0x03020105, 0x04030206,
        0x00020103, 0x01030204, 0x01030104, 0x02040205, 0x01020204, 0x02030305, 0x02030205, 0x03040306,
        0x01020104, 0x02030205, 0x02030105, 0x03040206, 0x02020205, 0x03030306, 0x03030206, 0x04040307,
        0x00000102, 0x01010203, 0x01010103, 0x02020204, 0x01000203, 0x02010304, 0x02010204, 0x03020305,
        0x01000103, 0x02010204, 0x02010104, 0x03020205, 0x02000204, 0x03010305, 0x03010205, 0x04020306,
        0x00010203, 0x01020304, 0x01020204, 0x02030305, 0x01010304, 0x02020405, 0x02020305, 0x03030406,
        0x01010204, 0x02020305, 0x02020205, 0x03030306, 0x02010305, 0x03020406, 0x03020306, 0x04030407,
        0x00010103, 0x01020204, 0x01020104, 0x02030205, 0x01010204, 0x02020305, 0x02020205, 0x03030306,
        0x01010104, 0x02020205, 0x02020105, 0x03030206, 0x02010205, 0x03020306, 0x03020206, 0x04030307,
        0x00020204, 0x01030305, 0x01030205, 0x02040306, 0x01020305, 0x02030406, 0x02030306, 0x03040407,
        0x01020205, 0x02030306, 0x02030206, 0x03040307, 0x02020306, 0x03030407, 0x03030307, 0x04040408
    };
    unsigned uSum;
    unsigned char * pTruthC, * pLimit;
    int * pBytes = pBytesInit;
    int i, iVar, Step, nWords, nBytes, nTotal;
    assert( nVars <= 20 );
    memset( pRes, 0, sizeof(int) * nVars );
    nTotal = uSum = 0;
    nWords = Kit_TruthWordNum( nVars );
    nBytes = nWords * 4;
    pTruthC = (unsigned char *)pTruth;
    pLimit = pTruthC + nBytes;
    for ( ; pTruthC < pLimit; pTruthC++ )
    {
        uSum += Table[*pTruthC];
        *pBytes++ = (Table[*pTruthC] & 0xff);
        if ( (uSum & 0xff) > 246 )
        {
            nTotal += (uSum & 0xff);
            pRes[0] += ((uSum >>  8) & 0xff);
            pRes[2] += ((uSum >> 16) & 0xff);
            pRes[3] += ((uSum >> 24) & 0xff);
            uSum = 0;
        }
    }
    if ( uSum )
    {
        nTotal += (uSum & 0xff);
        pRes[0] += ((uSum >>  8) & 0xff);
        pRes[1] += ((uSum >> 16) & 0xff);
        pRes[2] += ((uSum >> 24) & 0xff);
    }
    for ( iVar = 3, Step = 1; Step < nBytes; Step *= 2, iVar++ )
        for ( i = 0; i < nBytes; i += Step + Step )
        {
            pRes[iVar] += pBytesInit[i];
            pBytesInit[i] += pBytesInit[i+Step];
        }
    assert( pBytesInit[0] == nTotal );
    assert( iVar == nVars );
    for ( i = 0; i < nVars; i++ )
        assert( pRes[i] == Kit_TruthCofactor0Count(pTruth, nVars, i) );
    return nTotal;
}
int Kit_TruthCountMinterms ( unsigned * pTruth , int nVars , int * pRes , int * pBytesInit ) { static unsigned Table [ 256 ] = { 0x00000000 , 0x01010101 , 0x01010001 , 0x02020102 , 0x01000101 , 0x02010202 , 0x02010102 , 0x03020203 , 0x01000001 , 0x02010102 , 0x02010002 , 0x03020103 , 0x02000102 , 0x03010203 , 0x03010103 , 0x04020204 , 0x00010101 , 0x01020202 , 0x01020102 , 0x02030203 , 0x01010202 , 0x02020303 , 0x02020203 , 0x03030304 , 0x01010102 , 0x02020203 , 0x02020103 , 0x03030204 , 0x02010203 , 0x03020304 , 0x03020204 , 0x04030305 , 0x00010001 , 0x01020102 , 0x01020002 , 0x02030103 , 0x01010102 , 0x02020203 , 0x02020103 , 0x03030204 , 0x01010002 , 0x02020103 , 0x02020003 , 0x03030104 , 0x02010103 , 0x03020204 , 0x03020104 , 0x04030205 , 0x00020102 , 0x01030203 , 0x01030103 , 0x02040204 , 0x01020203 , 0x02030304 , 0x02030204 , 0x03040305 , 0x01020103 , 0x02030204 , 0x02030104 , 0x03040205 , 0x02020204 , 0x03030305 , 0x03030205 , 0x04040306 , 0x00000101 , 0x01010202 , 0x01010102 , 0x02020203 , 0x01000202 , 0x02010303 , 0x02010203 , 0x03020304 , 0x01000102 , 0x02010203 , 0x02010103 , 0x03020204 , 0x02000203 , 0x03010304 , 0x03010204 , 0x04020305 , 0x00010202 , 0x01020303 , 0x01020203 , 0x02030304 , 0x01010303 , 0x02020404 , 0x02020304 , 0x03030405 , 0x01010203 , 0x02020304 , 0x02020204 , 0x03030305 , 0x02010304 , 0x03020405 , 0x03020305 , 0x04030406 , 0x00010102 , 0x01020203 , 0x01020103 , 0x02030204 , 0x01010203 , 0x02020304 , 0x02020204 , 0x03030305 , 0x01010103 , 0x02020204 , 0x02020104 , 0x03030205 , 0x02010204 , 0x03020305 , 0x03020205 , 0x04030306 , 0x00020203 , 0x01030304 , 0x01030204 , 0x02040305 , 0x01020304 , 0x02030405 , 0x02030305 , 0x03040406 , 0x01020204 , 0x02030305 , 0x02030205 , 0x03040306 , 0x02020305 , 0x03030406 , 0x03030306 , 0x04040407 , 0x00000001 , 0x01010102 , 0x01010002 , 0x02020103 , 0x01000102 , 0x02010203 , 0x02010103 , 0x03020204 , 0x01000002 , 0x02010103 , 0x02010003 , 0x03020104 , 0x02000103 , 0x03010204 , 0x03010104 , 0x04020205 , 0x00010102 , 0x01020203 , 0x01020103 , 0x02030204 , 0x01010203 , 0x02020304 , 0x02020204 , 0x03030305 , 0x01010103 , 0x02020204 , 0x02020104 , 0x03030205 , 0x02010204 , 0x03020305 , 0x03020205 , 0x04030306 , 0x00010002 , 0x01020103 , 0x01020003 , 0x02030104 , 0x01010103 , 0x02020204 , 0x02020104 , 0x03030205 , 0x01010003 , 0x02020104 , 0x02020004 , 0x03030105 , 0x02010104 , 0x03020205 , 0x03020105 , 0x04030206 , 0x00020103 , 0x01030204 , 0x01030104 , 0x02040205 , 0x01020204 , 0x02030305 , 0x02030205 , 0x03040306 , 0x01020104 , 0x02030205 , 0x02030105 , 0x03040206 , 0x02020205 , 0x03030306 , 0x03030206 , 0x04040307 , 0x00000102 , 0x01010203 , 0x01010103 , 0x02020204 , 0x01000203 , 0x02010304 , 0x02010204 , 0x03020305 , 0x01000103 , 0x02010204 , 0x02010104 , 0x03020205 , 0x02000204 , 0x03010305 , 0x03010205 , 0x04020306 , 0x00010203 , 0x01020304 , 0x01020204 , 0x02030305 , 0x01010304 , 0x02020405 , 0x02020305 , 0x03030406 , 0x01010204 , 0x02020305 , 0x02020205 , 0x03030306 , 0x02010305 , 0x03020406 , 0x03020306 , 0x04030407 , 0x00010103 , 0x01020204 , 0x01020104 , 0x02030205 , 0x01010204 , 0x02020305 , 0x02020205 , 0x03030306 , 0x01010104 , 0x02020205 , 0x02020105 , 0x03030206 , 0x02010205 , 0x03020306 , 0x03020206 , 0x04030307 , 0x00020204 , 0x01030305 , 0x01030205 , 0x02040306 , 0x01020305 , 0x02030406 , 0x02030306 , 0x03040407 , 0x01020205 , 0x02030306 , 0x02030206 , 0x03040307 , 0x02020306 , 0x03030407 , 0x03030307 , 0x04040408 } ; unsigned uSum ; unsigned char * pTruthC , * pLimit ; int * pBytes = pBytesInit ; int i , iVar , Step , nWords , nBytes , nTotal ; assert ( nVars <= 20 ) ; memset ( pRes , 0 , sizeof ( int ) * nVars ) ; nTotal = uSum = 0 ; nWords = Kit_TruthWordNum ( nVars ) ; nBytes = nWords * 4 ; pTruthC = ( unsigned char * ) pTruth ; pLimit = pTruthC + nBytes ; for ( ; pTruthC < pLimit ; pTruthC ++ ) { uSum += Table [ * pTruthC ] ; * pBytes ++ = ( Table [ * pTruthC ] & 0xff ) ; if ( ( uSum & 0xff ) > 246 ) { nTotal += ( uSum & 0xff ) ; pRes [ 0 ] += ( ( uSum >> 8 ) & 0xff ) ; pRes [ 2 ] += ( ( uSum >> 16 ) & 0xff ) ; pRes [ 3 ] += ( ( uSum >> 24 ) & 0xff ) ; uSum = 0 ; } } if ( uSum ) { nTotal += ( uSum & 0xff ) ; pRes [ 0 ] += ( ( uSum >> 8 ) & 0xff ) ; pRes [ 1 ] += ( ( uSum >> 16 ) & 0xff ) ; pRes [ 2 ] += ( ( uSum >> 24 ) & 0xff ) ; } for ( iVar = 3 , Step = 1 ; Step < nBytes ; Step *= 2 , iVar ++ ) for ( i = 0 ; i < nBytes ; i += Step + Step ) { pRes [ iVar ] += pBytesInit [ i ] ; pBytesInit [ i ] += pBytesInit [ i + Step ] ; } assert ( pBytesInit [ 0 ] == nTotal ) ; assert ( iVar == nVars ) ; for ( i = 0 ; i < nVars ; i ++ ) assert ( pRes [ i ] == Kit_TruthCofactor0Count ( pTruth , nVars , i ) ) ; return nTotal ; }
Function
Synopsis    [Fast counting minterms in the cofactors of a function.]
Function Synopsis [ Fast counting minterms in the cofactors of a function . ]
// the number of 1s if every byte as well as in the 0-cofactors w.r.t. three variables // clear storage // count the number of one's in 0-cofactors of the first three variables // count all other variables