amapUniq.c 9.03 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    [amapUniq.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Technology mapper for standard cells.]

  Synopsis    [Checks if the structural node already exists.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: amapUniq.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "amapInt.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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Checks if the entry exists and returns value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_IntCheckWithMask( Vec_Int_t * p, int Entry )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        if ( (0xffff & p->pArray[i]) == (0xffff & Entry) )
            return p->pArray[i] >> 16;
    return -1;
}

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

  Synopsis    [Pushes entry in the natural order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_IntPushOrderWithMask( Vec_Int_t * p, int Entry )
{
    int i;
    if ( p->nSize == p->nCap )
        Vec_IntGrow( p, 2 * p->nCap );
    p->nSize++;
    for ( i = p->nSize-2; i >= 0; i-- )
        if ( (0xffff & p->pArray[i]) > (0xffff & Entry) )
            p->pArray[i+1] = p->pArray[i];
        else
            break;
    p->pArray[i+1] = Entry;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibFindNode( Amap_Lib_t * pLib, int iFan0, int iFan1, int fXor )
{
    if ( fXor )
93
        return Vec_IntCheckWithMask( (Vec_Int_t *)Vec_PtrEntry(pLib->vRulesX, iFan0), iFan1 );
Alan Mishchenko committed
94
    else
95
        return Vec_IntCheckWithMask( (Vec_Int_t *)Vec_PtrEntry(pLib->vRules, iFan0), iFan1 );
Alan Mishchenko committed
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 127 128 129 130 131 132 133 134 135 136 137 138 139
}

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

  Synopsis    [Checks if the three-argument rule exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibFindMux( Amap_Lib_t * p, int iFan0, int iFan1, int iFan2 )
{
    int x;
    for ( x = 0; x < Vec_IntSize(p->vRules3); x += 4 )
    {
        if ( Vec_IntEntry(p->vRules3, x)   == iFan0 &&
             Vec_IntEntry(p->vRules3, x+1) == iFan1 &&
             Vec_IntEntry(p->vRules3, x+2) == iFan2 )
        {
            return Vec_IntEntry(p->vRules3, x+3);
        }
    }
    return -1;
}

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

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Nod_t * Amap_LibCreateObj( Amap_Lib_t * p )
{
    Amap_Nod_t * pNode;
    if ( p->nNodes == p->nNodesAlloc )
    {
Alan Mishchenko committed
140
        p->pNodes = ABC_REALLOC( Amap_Nod_t, p->pNodes, 2*p->nNodesAlloc );
Alan Mishchenko committed
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
        p->nNodesAlloc *= 2;
    }
    pNode = Amap_LibNod( p, p->nNodes );
    memset( pNode, 0, sizeof(Amap_Nod_t) );
    pNode->Id = p->nNodes++;
    Vec_PtrPush( p->vRules, Vec_IntAlloc(8) );
    Vec_PtrPush( p->vRules, Vec_IntAlloc(8) );
    Vec_PtrPush( p->vRulesX, Vec_IntAlloc(8) );
    Vec_PtrPush( p->vRulesX, Vec_IntAlloc(8) );
    return pNode;
}

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

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCreateVar( Amap_Lib_t * p )
{
    Amap_Nod_t * pNode;
    // start the manager
    assert( p->pNodes == NULL );
    p->nNodesAlloc = 256;
Alan Mishchenko committed
170
    p->pNodes = ABC_ALLOC( Amap_Nod_t, p->nNodesAlloc );
Alan Mishchenko committed
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
    // create the first node
    pNode = Amap_LibCreateObj( p );
    p->pNodes->Type = AMAP_OBJ_PI;
    p->pNodes->nSuppSize = 1;
    return 0;
}

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

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCreateNode( Amap_Lib_t * p, int iFan0, int iFan1, int fXor )
{
    Amap_Nod_t * pNode;
    int iFan;
    if ( iFan0 < iFan1 )
    {
        iFan  = iFan0;
        iFan0 = iFan1;
        iFan1 = iFan;
    }
    pNode = Amap_LibCreateObj( p );
    pNode->Type  = fXor? AMAP_OBJ_XOR : AMAP_OBJ_AND;
201
    pNode->nSuppSize = p->pNodes[Abc_Lit2Var(iFan0)].nSuppSize + p->pNodes[Abc_Lit2Var(iFan1)].nSuppSize;
Alan Mishchenko committed
202 203 204 205 206
    pNode->iFan0 = iFan0;
    pNode->iFan1 = iFan1;
if ( p->fVerbose )
printf( "Creating node %5d %c :  iFan0 = %5d%c  iFan1 = %5d%c\n", 
pNode->Id, (fXor?'x':' '), 
207 208
Abc_Lit2Var(iFan0), (Abc_LitIsCompl(iFan0)?'-':'+'), 
Abc_Lit2Var(iFan1), (Abc_LitIsCompl(iFan1)?'-':'+') );
Alan Mishchenko committed
209 210 211 212

    if ( fXor )
    {
        if ( iFan0 == iFan1 )
213
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRulesX, iFan0), (pNode->Id << 16) | iFan1 );
Alan Mishchenko committed
214 215
        else
        {
216 217
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRulesX, iFan0), (pNode->Id << 16) | iFan1 );
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRulesX, iFan1), (pNode->Id << 16) | iFan0 );
Alan Mishchenko committed
218 219 220 221 222
        }
    }
    else
    {
        if ( iFan0 == iFan1 )
223
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRules, iFan0), (pNode->Id << 16) | iFan1 );
Alan Mishchenko committed
224 225
        else
        {
226 227
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRules, iFan0), (pNode->Id << 16) | iFan1 );
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRules, iFan1), (pNode->Id << 16) | iFan0 );
Alan Mishchenko committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        }
    }
    return pNode->Id;
}

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

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCreateMux( Amap_Lib_t * p, int iFan0, int iFan1, int iFan2 )
{
    Amap_Nod_t * pNode;
    pNode = Amap_LibCreateObj( p );
    pNode->Type  = AMAP_OBJ_MUX;
249
    pNode->nSuppSize = p->pNodes[Abc_Lit2Var(iFan0)].nSuppSize + p->pNodes[Abc_Lit2Var(iFan1)].nSuppSize + p->pNodes[Abc_Lit2Var(iFan2)].nSuppSize;
Alan Mishchenko committed
250 251 252 253 254 255
    pNode->iFan0 = iFan0;
    pNode->iFan1 = iFan1;
    pNode->iFan2 = iFan2;
if ( p->fVerbose )
printf( "Creating node %5d %c :  iFan0 = %5d%c  iFan1 = %5d%c  iFan2 = %5d%c\n", 
pNode->Id, 'm', 
256 257 258
Abc_Lit2Var(iFan0), (Abc_LitIsCompl(iFan0)?'-':'+'), 
Abc_Lit2Var(iFan1), (Abc_LitIsCompl(iFan1)?'-':'+'), 
Abc_Lit2Var(iFan2), (Abc_LitIsCompl(iFan2)?'-':'+') );
Alan Mishchenko committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

    Vec_IntPush( p->vRules3, iFan0 );
    Vec_IntPush( p->vRules3, iFan1 );
    Vec_IntPush( p->vRules3, iFan2 );
    Vec_IntPush( p->vRules3, pNode->Id );
    return pNode->Id;
}

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

  Synopsis    [Allocates triangular lookup table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int ** Amap_LibLookupTableAlloc( Vec_Ptr_t * vVec, int fVerbose )
{
    Vec_Int_t * vOne;
    int ** pRes, * pBuffer;
    int i, k, nTotal, nSize, nEntries, Value;
    // count the total size
    nEntries = nSize = Vec_PtrSize( vVec );
285
    Vec_PtrForEachEntry( Vec_Int_t *, vVec, vOne, i )
Alan Mishchenko committed
286
        nEntries += Vec_IntSize(vOne);
Alan Mishchenko committed
287
    pBuffer = ABC_ALLOC( int, nSize * sizeof(void *) + nEntries );
Alan Mishchenko committed
288 289 290
    pRes = (int **)pBuffer;
    pRes[0] = pBuffer + nSize * sizeof(void *);
    nTotal = 0;
291
    Vec_PtrForEachEntry( Vec_Int_t *, vVec, vOne, i )
Alan Mishchenko committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    {
        pRes[i] = pRes[0] + nTotal;
        nTotal += Vec_IntSize(vOne) + 1;
        if ( fVerbose )
        printf( "%d : ", i );
        Vec_IntForEachEntry( vOne, Value, k )
        {
            pRes[i][k] = Value;
            if ( fVerbose )
            printf( "%d(%d) ", Value&0xffff, Value>>16 );
        }
        if ( fVerbose )
        printf( "\n" );
        pRes[i][k] = 0;
    }
    assert( nTotal == nEntries );
    return pRes;
}

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


316 317
ABC_NAMESPACE_IMPL_END