abc/src/misc/bbl/bblif.c
/**Fnction*************************************************************

  Synopsis    [Converting truth table into an SOP.]

  Description [The truth table is given as a bit-string pTruth
  composed of 2^nVars bits. The result is an SOP derived by
  collecting minterms appearing in the truth table. The SOP is 
  represented as a C-string, as documented in file "bblif.h".
  It is recommended to limit the use of this procedure to Boolean 
  functions up to 6 inputs.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Fnction
Synopsis    [Converting truth table into an SOP.]

Description [The truth table is given as a bit-string pTruth
composed of 2^nVars bits. The result is an SOP derived by
collecting minterms appearing in the truth table. The SOP is
represented as a C-string, as documented in file "bblif.h".
It is recommended to limit the use of this procedure to Boolean
functions up to 6 inputs.]




Fnction Synopsis [ Converting truth table into an SOP . ] Description [ The truth table is given as a bit - string pTruth composed of 2^nVars bits . The result is an SOP derived by collecting minterms appearing in the truth table . The SOP is represented as a C - string as documented in file " bblif . h " . It is recommended to limit the use of this procedure to Boolean functions up to 6 inputs . ]
char * Bbl_ManTruthToSop( unsigned * pTruth, int nVars )
{
    char * pResult, * pTemp;
    int nMints, nOnes, b, v;
    assert( nVars >= 0 && nVars <= 16 );
    nMints = (1 << nVars);
    nOnes = 0;
    for ( b = 0; b < nMints; b++ )
        nOnes += ((pTruth[b>>5] >> (b&31)) & 1);
    if ( nOnes == 0 || nOnes == nMints )
    {
        pResult = pTemp = BBLIF_ALLOC( char, nVars + 4 );        
        for ( v = 0; v < nVars; v++ )
            *pTemp++ = '-';
        *pTemp++ = ' ';
        *pTemp++ = nOnes? '1' : '0';
        *pTemp++ = '\n';
        *pTemp++ = 0;
        assert( pTemp - pResult == nVars + 4 );
        return pResult;
    }
    pResult = pTemp = BBLIF_ALLOC( char, nOnes * (nVars + 3) + 1 );        
    for ( b = 0; b < nMints; b++ )
    {
        if ( ((pTruth[b>>5] >> (b&31)) & 1) == 0 )
            continue;
        for ( v = 0; v < nVars; v++ )
            *pTemp++ = ((b >> v) & 1)? '1' : '0';
        *pTemp++ = ' ';
        *pTemp++ = '1';
        *pTemp++ = '\n';
    }
    *pTemp++ = 0;
    assert( pTemp - pResult == nOnes * (nVars + 3) + 1 );
    return pResult;
}
char * Bbl_ManTruthToSop ( unsigned * pTruth , int nVars ) { char * pResult , * pTemp ; int nMints , nOnes , b , v ; assert ( nVars >= 0 && nVars <= 16 ) ; nMints = ( 1 << nVars ) ; nOnes = 0 ; for ( b = 0 ; b < nMints ; b ++ ) nOnes += ( ( pTruth [ b >> 5 ] >> ( b & 31 ) ) & 1 ) ; if ( nOnes == 0 || nOnes == nMints ) { pResult = pTemp = BBLIF_ALLOC ( char , nVars + 4 ) ; for ( v = 0 ; v < nVars ; v ++ ) * pTemp ++ = ' ' ; * pTemp ++ = ' ' ; * pTemp ++ = nOnes ? ' ' : ' ' ; * pTemp ++ = ' \n ' ; * pTemp ++ = 0 ; assert ( pTemp - pResult == nVars + 4 ) ; return pResult ; } pResult = pTemp = BBLIF_ALLOC ( char , nOnes * ( nVars + 3 ) + 1 ) ; for ( b = 0 ; b < nMints ; b ++ ) { if ( ( ( pTruth [ b >> 5 ] >> ( b & 31 ) ) & 1 ) == 0 ) continue ; for ( v = 0 ; v < nVars ; v ++ ) * pTemp ++ = ( ( b >> v ) & 1 ) ? ' ' : ' ' ; * pTemp ++ = ' ' ; * pTemp ++ = ' ' ; * pTemp ++ = ' \n ' ; } * pTemp ++ = 0 ; assert ( pTemp - pResult == nOnes * ( nVars + 3 ) + 1 ) ; return pResult ; }
Fnction
Synopsis    [Converting truth table into an SOP.]
Fnction Synopsis [ Converting truth table into an SOP . ]
// count the number of ones // handle constants