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

  Synopsis    [Converting SOP into a truth table.]

  Description [The SOP is represented as a C-string, as documented in 
  file "bblif.h". The truth table is returned as a bit-string composed 
  of 2^nVars bits. For functions of less than 6 variables, the full
  machine word is returned. (The truth table looks as if the function
  had 5 variables.) The use of this procedure should be limited to 
  Boolean functions with no more than 16 inputs.]
               
  SideEffects []

  SeeAlso     []

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

Description [The SOP is represented as a C-string, as documented in
file "bblif.h". The truth table is returned as a bit-string composed
of 2^nVars bits. For functions of less than 6 variables, the full
machine word is returned. (The truth table looks as if the function
had 5 variables.) The use of this procedure should be limited to
Boolean functions with no more than 16 inputs.]




Fnction Synopsis [ Converting SOP into a truth table . ] Description [ The SOP is represented as a C - string as documented in file " bblif . h " . The truth table is returned as a bit - string composed of 2^nVars bits . For functions of less than 6 variables the full machine word is returned . ( The truth table looks as if the function had 5 variables . ) The use of this procedure should be limited to Boolean functions with no more than 16 inputs . ]
unsigned * Bbl_ManSopToTruth( char * pSop, int nVars )
{
    unsigned * pTruth, * pCube, * pVars[16];
    int nWords = nVars <= 5 ? 1 : (1 << (nVars - 5));
    int v, c, w, nCubes, fCompl = 0;
    if ( pSop == NULL )
        return NULL;
    if ( strlen(pSop) % (nVars + 3) != 0 )
    {
        printf( "Bbl_ManSopToTruth(): SOP is represented incorrectly.\n" );
        return NULL;
    }
    pTruth   = BBLIF_ALLOC( unsigned, nWords );   
    pVars[0] = BBLIF_ALLOC( unsigned, nWords * (nVars+1) );   
    for ( v = 1; v < nVars; v++ )
        pVars[v] = pVars[v-1] + nWords;
    pCube = pVars[v-1] + nWords;
    Bbl_ManSopToTruthElem( nVars, pVars );
    memset( pTruth, 0, sizeof(unsigned) * nWords );
    nCubes =  strlen(pSop) / (nVars + 3);
    for ( c = 0; c < nCubes; c++ )
    {
        fCompl = (pSop[nVars+1] == '0');
        memset( pCube, 0xff, sizeof(unsigned) * nWords );
        for ( v = 0; v < nVars; v++ )
            if ( pSop[v] == '1' )
                for ( w = 0; w < nWords; w++ )
                    pCube[w] &= pVars[v][w];
            else if ( pSop[v] == '0' )
                for ( w = 0; w < nWords; w++ )
                    pCube[w] &= ~pVars[v][w];
        for ( w = 0; w < nWords; w++ )
            pTruth[w] |= pCube[w];
        pSop += (nVars + 3);
    }
    BBLIF_FREE( pVars[0] );
    if ( fCompl )
        for ( w = 0; w < nWords; w++ )
            pTruth[w] = ~pTruth[w];
    return pTruth;
}
unsigned * Bbl_ManSopToTruth ( char * pSop , int nVars ) { unsigned * pTruth , * pCube , * pVars [ 16 ] ; int nWords = nVars <= 5 ? 1 : ( 1 << ( nVars - 5 ) ) ; int v , c , w , nCubes , fCompl = 0 ; if ( pSop == NULL ) return NULL ; if ( strlen ( pSop ) % ( nVars + 3 ) != 0 ) { printf ( " \n " ) ; return NULL ; } pTruth = BBLIF_ALLOC ( unsigned , nWords ) ; pVars [ 0 ] = BBLIF_ALLOC ( unsigned , nWords * ( nVars + 1 ) ) ; for ( v = 1 ; v < nVars ; v ++ ) pVars [ v ] = pVars [ v - 1 ] + nWords ; pCube = pVars [ v - 1 ] + nWords ; Bbl_ManSopToTruthElem ( nVars , pVars ) ; memset ( pTruth , 0 , sizeof ( unsigned ) * nWords ) ; nCubes = strlen ( pSop ) / ( nVars + 3 ) ; for ( c = 0 ; c < nCubes ; c ++ ) { fCompl = ( pSop [ nVars + 1 ] == ' ' ) ; memset ( pCube , 0xff , sizeof ( unsigned ) * nWords ) ; for ( v = 0 ; v < nVars ; v ++ ) if ( pSop [ v ] == ' ' ) for ( w = 0 ; w < nWords ; w ++ ) pCube [ w ] &= pVars [ v ] [ w ] ; else if ( pSop [ v ] == ' ' ) for ( w = 0 ; w < nWords ; w ++ ) pCube [ w ] &= ~ pVars [ v ] [ w ] ; for ( w = 0 ; w < nWords ; w ++ ) pTruth [ w ] |= pCube [ w ] ; pSop += ( nVars + 3 ) ; } BBLIF_FREE ( pVars [ 0 ] ) ; if ( fCompl ) for ( w = 0 ; w < nWords ; w ++ ) pTruth [ w ] = ~ pTruth [ w ] ; return pTruth ; }
Fnction
Synopsis    [Converting SOP into a truth table.]
Fnction Synopsis [ Converting SOP into a truth table . ]
// create storage for TTs of the result, elementary variables and the temp cube // iterate through the cubes // iterate through the literals of the cube // add cube to storage // go to the next cube