mpmTruth.c 8.25 KB
Newer Older
Alan Mishchenko committed
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
/**CFile****************************************************************

  FileName    [mpmTruth.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Configurable technology mapper.]

  Synopsis    [Truth table manipulation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 1, 2013.]

  Revision    [$Id: mpmTruth.c,v 1.00 2013/06/01 00:00:00 alanmi Exp $]

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

#include "mpmInt.h"

ABC_NAMESPACE_IMPL_START


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

Alan Mishchenko committed
30 31
//#define MPM_TRY_NEW

Alan Mishchenko committed
32 33 34 35 36 37
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  Synopsis    [Unifies variable order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Mpm_TruthStretch( word * pTruth, Mpm_Cut_t * pCut, Mpm_Cut_t * pCut0, int nLimit )
{
    int i, k;
    for ( i = (int)pCut->nLeaves - 1, k = (int)pCut0->nLeaves - 1; i >= 0 && k >= 0; i-- )
    {
        if ( pCut0->pLeaves[k] < pCut->pLeaves[i] )
            continue;
        assert( pCut0->pLeaves[k] == pCut->pLeaves[i] );
        if ( k < i )
            Abc_TtSwapVars( pTruth, nLimit, k, i );
        k--;
    }
}

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

  Synopsis    [Performs truth table support minimization.]
Alan Mishchenko committed
64 65 66 67 68 69 70 71

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
72 73 74 75 76
static inline int Mpm_CutTruthMinimize6( Mpm_Man_t * p, Mpm_Cut_t * pCut )
{
    unsigned uSupport;
    int i, k, nSuppSize;
    // compute the support of the cut's function
Alan Mishchenko committed
77
    word t = *Mpm_CutTruth( p, Abc_Lit2Var(pCut->iFunc) );
Alan Mishchenko committed
78 79 80
    uSupport = Abc_Tt6SupportAndSize( t, Mpm_CutLeafNum(pCut), &nSuppSize );
    if ( nSuppSize == Mpm_CutLeafNum(pCut) )
        return 0;
Alan Mishchenko committed
81
    p->nSmallSupp += (int)(nSuppSize < 2);
Alan Mishchenko committed
82 83 84
    // update leaves and signature
    for ( i = k = 0; i < Mpm_CutLeafNum(pCut); i++ )
    {
Alan Mishchenko committed
85
        if ( ((uSupport >> i) & 1) )
Alan Mishchenko committed
86
        {
Alan Mishchenko committed
87 88 89 90 91 92 93
            if ( k < i )
            {
                pCut->pLeaves[k] = pCut->pLeaves[i];
                Abc_TtSwapVars( &t, p->nLutSize, k, i );
            }
            k++;
        }
Alan Mishchenko committed
94 95 96
    }
    assert( k == nSuppSize );
    pCut->nLeaves = nSuppSize;
Alan Mishchenko committed
97
    assert( nSuppSize == Abc_TtSupportSize(&t, 6) );
Alan Mishchenko committed
98
    // save the result
Alan Mishchenko committed
99
    pCut->iFunc = Abc_Var2Lit( Vec_MemHashInsert(p->vTtMem, &t), Abc_LitIsCompl(pCut->iFunc) );
Alan Mishchenko committed
100 101
    return 1;
}
Alan Mishchenko committed
102
static inline int Mpm_CutTruthMinimize7( Mpm_Man_t * p, Mpm_Cut_t * pCut )
Alan Mishchenko committed
103
{
Alan Mishchenko committed
104 105 106 107 108 109 110 111 112 113 114
    unsigned uSupport;
    int i, k, nSuppSize;
    // compute the support of the cut's function
    word * pTruth = Mpm_CutTruth( p, Abc_Lit2Var(pCut->iFunc) );
    uSupport = Abc_TtSupportAndSize( pTruth, Mpm_CutLeafNum(pCut), &nSuppSize );
    if ( nSuppSize == Mpm_CutLeafNum(pCut) )
        return 0;
    p->nSmallSupp += (int)(nSuppSize < 2);
    // update leaves and signature
    Abc_TtCopy( p->Truth, pTruth, p->nTruWords, 0 );
    for ( i = k = 0; i < Mpm_CutLeafNum(pCut); i++ )
Alan Mishchenko committed
115
    {
Alan Mishchenko committed
116 117 118 119 120 121 122 123 124
        if ( ((uSupport >> i) & 1) )
        {
            if ( k < i )
            {
                pCut->pLeaves[k] = pCut->pLeaves[i];
                Abc_TtSwapVars( p->Truth, p->nLutSize, k, i );
            }
            k++;
        }
Alan Mishchenko committed
125
    }
Alan Mishchenko committed
126
    assert( k == nSuppSize );
Alan Mishchenko committed
127
    assert( nSuppSize == Abc_TtSupportSize(p->Truth, Mpm_CutLeafNum(pCut)) );
Alan Mishchenko committed
128 129 130 131
    pCut->nLeaves = nSuppSize;
    // save the result
    pCut->iFunc = Abc_Var2Lit( Vec_MemHashInsert(p->vTtMem, p->Truth), Abc_LitIsCompl(pCut->iFunc) );
    return 1;
Alan Mishchenko committed
132
}
Alan Mishchenko committed
133 134 135 136 137 138 139 140 141 142 143 144 145

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

  Synopsis    [Performs truth table computation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Mpm_CutComputeTruth6( Mpm_Man_t * p, Mpm_Cut_t * pCut, Mpm_Cut_t * pCut0, Mpm_Cut_t * pCut1, Mpm_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, int Type )
Alan Mishchenko committed
146 147 148 149 150 151
{
    word * pTruth0 = Mpm_CutTruth( p, Abc_Lit2Var(pCut0->iFunc) );
    word * pTruth1 = Mpm_CutTruth( p, Abc_Lit2Var(pCut1->iFunc) );
    word * pTruthC = NULL;
    word t0 = (fCompl0 ^ pCut0->fCompl ^ Abc_LitIsCompl(pCut0->iFunc)) ? ~*pTruth0 : *pTruth0;
    word t1 = (fCompl1 ^ pCut1->fCompl ^ Abc_LitIsCompl(pCut1->iFunc)) ? ~*pTruth1 : *pTruth1;
Alan Mishchenko committed
152
    word tC = 0, t = 0;
Alan Mishchenko committed
153 154
    Mpm_TruthStretch( &t0, pCut, pCut0, p->nLutSize );
    Mpm_TruthStretch( &t1, pCut, pCut1, p->nLutSize );
Alan Mishchenko committed
155 156 157 158
    if ( pCutC )
    {
        pTruthC = Mpm_CutTruth( p, Abc_Lit2Var(pCutC->iFunc) );
        tC = (fComplC ^ pCutC->fCompl ^ Abc_LitIsCompl(pCutC->iFunc)) ? ~*pTruthC : *pTruthC;
Alan Mishchenko committed
159
        Mpm_TruthStretch( &tC, pCut, pCutC, p->nLutSize );
Alan Mishchenko committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }
    assert( p->nLutSize <= 6 );
    if ( Type == 1 )
        t = t0 & t1;
    else if ( Type == 2 )
        t = t0 ^ t1;
    else if ( Type == 3 )
        t = (tC & t1) | (~tC & t0);
    else assert( 0 );
    // save the result
    if ( t & 1 )
    {
        t = ~t;
        pCut->iFunc = Abc_Var2Lit( Vec_MemHashInsert( p->vTtMem, &t ), 1 );
    }
    else
        pCut->iFunc = Abc_Var2Lit( Vec_MemHashInsert( p->vTtMem, &t ), 0 );
Alan Mishchenko committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    if ( p->pPars->fCutMin )
        return Mpm_CutTruthMinimize6( p, pCut );
    return 1;
}
static inline int Mpm_CutComputeTruth7( Mpm_Man_t * p, Mpm_Cut_t * pCut, Mpm_Cut_t * pCut0, Mpm_Cut_t * pCut1, Mpm_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, int Type )
{
    word * pTruth0 = Mpm_CutTruth( p, Abc_Lit2Var(pCut0->iFunc) );
    word * pTruth1 = Mpm_CutTruth( p, Abc_Lit2Var(pCut1->iFunc) );
    word * pTruthC = NULL;
    Abc_TtCopy( p->Truth0, pTruth0, p->nTruWords, fCompl0 ^ pCut0->fCompl ^ Abc_LitIsCompl(pCut0->iFunc) );
    Abc_TtCopy( p->Truth1, pTruth1, p->nTruWords, fCompl1 ^ pCut1->fCompl ^ Abc_LitIsCompl(pCut1->iFunc) );
    Mpm_TruthStretch( p->Truth0, pCut, pCut0, p->nLutSize );
    Mpm_TruthStretch( p->Truth1, pCut, pCut1, p->nLutSize );
    if ( pCutC )
    {
        pTruthC = Mpm_CutTruth( p, Abc_Lit2Var(pCutC->iFunc) );
        Abc_TtCopy( p->TruthC, pTruthC, p->nTruWords, fComplC ^ pCutC->fCompl ^ Abc_LitIsCompl(pCutC->iFunc) );
        Mpm_TruthStretch( p->TruthC, pCut, pCutC, p->nLutSize );
    }
    if ( Type == 1 )
        Abc_TtAnd( p->Truth, p->Truth0, p->Truth1, p->nTruWords, 0 );
    else if ( Type == 2 )
        Abc_TtXor( p->Truth, p->Truth0, p->Truth1, p->nTruWords, 0 );
    else if ( Type == 3 )
        Abc_TtMux( p->Truth, p->TruthC, p->Truth1, p->Truth0, p->nTruWords );
    else assert( 0 );
    // save the result
    if ( p->Truth[0] & 1 )
    {
        Abc_TtNot( p->Truth, p->nTruWords );
        pCut->iFunc = Abc_Var2Lit( Vec_MemHashInsert( p->vTtMem, p->Truth ), 1 );
    }
    else
        pCut->iFunc = Abc_Var2Lit( Vec_MemHashInsert( p->vTtMem, p->Truth ), 0 );
    if ( p->pPars->fCutMin )
        return Mpm_CutTruthMinimize7( p, pCut );
    return 1;
}
int Mpm_CutComputeTruth( Mpm_Man_t * p, Mpm_Cut_t * pCut, Mpm_Cut_t * pCut0, Mpm_Cut_t * pCut1, Mpm_Cut_t * pCutC, int fCompl0, int fCompl1, int fComplC, int Type )
{
    int RetValue;
    if ( p->nLutSize <= 6 )
        RetValue = Mpm_CutComputeTruth6( p, pCut, pCut0, pCut1, pCutC, fCompl0, fCompl1, fComplC, Type );
    else
        RetValue = Mpm_CutComputeTruth7( p, pCut, pCut0, pCut1, pCutC, fCompl0, fCompl1, fComplC, Type );
Alan Mishchenko committed
222 223
#ifdef MPM_TRY_NEW
    {
Alan Mishchenko committed
224
        extern unsigned Abc_TtCanonicize( word * pTruth, int nVars, char * pCanonPerm );
Alan Mishchenko committed
225
        char pCanonPerm[16];
Alan Mishchenko committed
226 227
        memcpy( p->Truth0, p->Truth, sizeof(word) * p->nTruWords );
        Abc_TtCanonicize( p->Truth0, pCut->nLimit, pCanonPerm );
Alan Mishchenko committed
228 229
    }
#endif
Alan Mishchenko committed
230
    return RetValue;
Alan Mishchenko committed
231
}
Alan Mishchenko committed
232 233 234 235 236 237 238 239

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


ABC_NAMESPACE_IMPL_END