lpkAbcUtil.c 6.68 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
/**CFile****************************************************************

  FileName    [lpkAbcUtil.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Fast Boolean matching for LUT structures.]

  Synopsis    [Procedures working on decomposed functions.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: lpkAbcUtil.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

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

#include "lpkInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Allocates the function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Lpk_Fun_t * Lpk_FunAlloc( int nVars )
{
    Lpk_Fun_t * p;
Alan Mishchenko committed
48
    p = (Lpk_Fun_t *)ABC_ALLOC( char, sizeof(Lpk_Fun_t) + sizeof(unsigned) * Kit_TruthWordNum(nVars) * 3 );
Alan Mishchenko committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    memset( p, 0, sizeof(Lpk_Fun_t) );
    return p;
}

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

  Synopsis    [Deletes the function]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Lpk_FunFree( Lpk_Fun_t * p )
{
Alan Mishchenko committed
66
    ABC_FREE( p );
Alan Mishchenko committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

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

  Synopsis    [Creates the starting function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Lpk_Fun_t * Lpk_FunCreate( Abc_Ntk_t * pNtk, Vec_Ptr_t * vLeaves, unsigned * pTruth, int nLutK, int AreaLim, int DelayLim )
{
    Lpk_Fun_t * p;
    Abc_Obj_t * pNode;
    int i;
    p = Lpk_FunAlloc( Vec_PtrSize(vLeaves) );
    p->Id = Vec_PtrSize(vLeaves);
    p->vNodes = vLeaves;
    p->nVars = Vec_PtrSize(vLeaves);
    p->nLutK = nLutK;
    p->nAreaLim = AreaLim;
    p->nDelayLim = DelayLim;
    p->uSupp = Kit_TruthSupport( pTruth, p->nVars );
    Kit_TruthCopy( Lpk_FunTruth(p,0), pTruth, p->nVars );
94
    Vec_PtrForEachEntry( Abc_Obj_t *, vLeaves, pNode, i )
Alan Mishchenko committed
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
    {
        p->pFanins[i] = i;
        p->pDelays[i] = pNode->Level;
    }
    Vec_PtrPush( p->vNodes, p );
    return p;
}

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

  Synopsis    [Creates the new function with the given truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Lpk_Fun_t * Lpk_FunDup( Lpk_Fun_t * p, unsigned * pTruth )
{
    Lpk_Fun_t * pNew;
    pNew = Lpk_FunAlloc( p->nVars );
    pNew->Id = Vec_PtrSize(p->vNodes);
    pNew->vNodes = p->vNodes;
    pNew->nVars = p->nVars;
    pNew->nLutK = p->nLutK;
    pNew->nAreaLim = p->nAreaLim;
    pNew->nDelayLim = p->nDelayLim;
    pNew->uSupp = Kit_TruthSupport( pTruth, p->nVars );
    Kit_TruthCopy( Lpk_FunTruth(pNew,0), pTruth, p->nVars );
    memcpy( pNew->pFanins, p->pFanins, 16 );
127
    memcpy( pNew->pDelays, p->pDelays, sizeof(int)*16 );
Alan Mishchenko committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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
    Vec_PtrPush( p->vNodes, pNew );
    return pNew;
}

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

  Synopsis    [Minimizes support of the function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_FunSuppMinimize( Lpk_Fun_t * p )
{
    int i, k, nVarsNew;
    // compress the truth table
    if ( p->uSupp == Kit_BitMask(p->nVars) )
        return 0;
    // invalidate support info
    p->fSupports = 0;
//Extra_PrintBinary( stdout, &p->uSupp, p->nVars ); printf( "\n" );
    // minimize support
    nVarsNew = Kit_WordCountOnes(p->uSupp);
    Kit_TruthShrink( Lpk_FunTruth(p, 1), Lpk_FunTruth(p, 0), nVarsNew, p->nVars, p->uSupp, 1 );
    k = 0;
    Lpk_SuppForEachVar( p->uSupp, i )
    {
        p->pFanins[k] = p->pFanins[i];
        p->pDelays[k] = p->pDelays[i];
/*
        if ( p->fSupports )
        {
            p->puSupps[2*k+0] = p->puSupps[2*i+0];
            p->puSupps[2*k+1] = p->puSupps[2*i+1];
        }
*/
        k++;
    }
    assert( k == nVarsNew );
    p->nVars = k;
    p->uSupp = Kit_BitMask(p->nVars);
    return 1;
}

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

  Synopsis    [Computes cofactors w.r.t. each variable.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Lpk_FunComputeCofSupps( Lpk_Fun_t * p )
{
    unsigned * pTruth  = Lpk_FunTruth( p, 0 );
    unsigned * pTruth0 = Lpk_FunTruth( p, 1 );
    unsigned * pTruth1 = Lpk_FunTruth( p, 2 );
    int Var;
    assert( p->fSupports == 0 );
//    Lpk_SuppForEachVar( p->uSupp, Var )
    for ( Var = 0; Var < (int)p->nVars; Var++ )
    {
        Kit_TruthCofactor0New( pTruth0, pTruth, p->nVars, Var );
        Kit_TruthCofactor1New( pTruth1, pTruth, p->nVars, Var );
        p->puSupps[2*Var+0] = Kit_TruthSupport( pTruth0, p->nVars );
        p->puSupps[2*Var+1] = Kit_TruthSupport( pTruth1, p->nVars );
    }
    p->fSupports = 1;
}

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

  Synopsis    [Get the delay of the bound set.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
215
int Lpk_SuppDelay( unsigned uSupp, int * pDelays )
Alan Mishchenko committed
216 217 218 219
{
    int Delay, Var;
    Delay = 0;
    Lpk_SuppForEachVar( uSupp, Var )
220
        Delay = Abc_MaxInt( Delay, pDelays[Var] );
Alan Mishchenko committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    return Delay + 1;
}

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

  Synopsis    [Converts support into variables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Lpk_SuppToVars( unsigned uBoundSet, char * pVars )
{
    int i, nVars = 0;
    Lpk_SuppForEachVar( uBoundSet, i )
        pVars[nVars++] = i;
    return nVars;
}

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


248 249
ABC_NAMESPACE_IMPL_END