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

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Frontier representation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.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
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    [Find the next place on the frontier.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Gia_ManFrontFindNext( char * pFront, int nFront, int iFront )
{
    assert( iFront < nFront );
    for ( ; pFront[iFront]; iFront = (iFront + 1) % nFront );
    assert( pFront[iFront] == 0 );
    pFront[iFront] = 1;
    return iFront;
}

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

  Synopsis    [Transforms the frontier manager to its initial state.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFrontTransform( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i, * pFrontToId; // mapping of nodes into frontier variables
    assert( p->nFront > 0 );
70
    pFrontToId = ABC_FALLOC( int, p->nFront );
Alan Mishchenko committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    Gia_ManForEachObj( p, pObj, i )
    {
        if ( Gia_ObjIsCo(pObj) )
        {
            assert( pObj->Value == GIA_NONE );
            pObj->iDiff0 = i - pFrontToId[Gia_ObjDiff0(pObj)];
        }
        else if ( Gia_ObjIsAnd(pObj) )
        {
            assert( (int)pObj->Value < p->nFront );
            pObj->iDiff0 = i - pFrontToId[Gia_ObjDiff0(pObj)];
            pObj->iDiff1 = i - pFrontToId[Gia_ObjDiff1(pObj)];
            pFrontToId[pObj->Value] = i;
        }
        else
        {
            assert( (int)pObj->Value < p->nFront );
            pFrontToId[pObj->Value] = i;
        }
        pObj->Value = 0;
    }
    ABC_FREE( pFrontToId );
}

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

  Synopsis    [Determine the frontier.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
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 140 141 142 143 144 145 146
int Gia_ManCrossCutSimple( Gia_Man_t * p )
{
    Gia_Obj_t * pObj;
    int i, nCutCur = 0, nCutMax = 0;
    Gia_ManCreateValueRefs( p );
    Gia_ManForEachObj( p, pObj, i )
    {
        if ( pObj->Value )
            nCutCur++;
        if ( nCutMax < nCutCur )
            nCutMax = nCutCur;
        if ( Gia_ObjIsAnd(pObj) )
        {
            if ( --Gia_ObjFanin0(pObj)->Value == 0 )
                nCutCur--;
            if ( --Gia_ObjFanin1(pObj)->Value == 0 )
                nCutCur--;
        }
        else if ( Gia_ObjIsCo(pObj) )
        {
            if ( --Gia_ObjFanin0(pObj)->Value == 0 )
                nCutCur--;
        }
    }
//    Gia_ManForEachObj( p, pObj, i )
//        assert( pObj->Value == 0 );
    return nCutMax;
}


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

  Synopsis    [Determine the frontier.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
147 148 149 150 151 152
Gia_Man_t * Gia_ManFront( Gia_Man_t * p )
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj, * pFanin0New, * pFanin1New, * pObjNew;
    char * pFront;    // places used for the frontier
    int i, iLit, nCrossCut = 0, nCrossCutMax = 0;
153
    int nCrossCutMaxInit = Gia_ManCrossCutSimple( p );
154
    int iFront = 0;//, clk = Abc_Clock(); 
Alan Mishchenko committed
155
    // set references for all objects
156
    Gia_ManCreateValueRefs( p );
Alan Mishchenko committed
157 158
    // start the new manager
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
159
    pNew->pName = Abc_UtilStrsav( p->pName );
160
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
Alan Mishchenko committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    pNew->nFront = 1 + (int)((float)1.1 * nCrossCutMaxInit); 
    // start the frontier
    pFront = ABC_CALLOC( char, pNew->nFront );
    // add constant node
    Gia_ManConst0(pNew)->Value = iFront = Gia_ManFrontFindNext( pFront, pNew->nFront, iFront );
    if ( Gia_ObjValue(Gia_ManConst0(p)) == 0 )
        pFront[iFront] = 0;
    else
        nCrossCut = 1;
    // iterate through the objects
    Gia_ManForEachObj1( p, pObj, i )
    {
        if ( Gia_ObjIsCi(pObj) )
        {
            if ( Gia_ObjValue(pObj) && nCrossCutMax < ++nCrossCut )
                nCrossCutMax = nCrossCut;
            // create new node
            iLit = Gia_ManAppendCi( pNew );
179
            pObjNew = Gia_ManObj( pNew, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
180 181 182 183 184 185 186 187 188 189 190 191
            assert( Gia_ObjId(pNew, pObjNew) == Gia_ObjId(p, pObj) );
            pObjNew->Value = iFront = Gia_ManFrontFindNext( pFront, pNew->nFront, iFront );
            // handle CIs without fanout
            if ( Gia_ObjValue(pObj) == 0 )
                pFront[iFront] = 0;
            continue;
        }
        if ( Gia_ObjIsCo(pObj) )
        {
            assert( Gia_ObjValue(pObj) == 0 );
            // create new node
            iLit = Gia_ManAppendCo( pNew, 0 );
192
            pObjNew = Gia_ManObj( pNew, Abc_Lit2Var(iLit) );
Alan Mishchenko committed
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            assert( Gia_ObjId(pNew, pObjNew) == Gia_ObjId(p, pObj) );
            // get the fanin
            pFanin0New = Gia_ManObj( pNew, Gia_ObjFaninId0(pObj, i) );
            assert( pFanin0New->Value != GIA_NONE );
            pObjNew->Value = GIA_NONE;
            pObjNew->iDiff0 = pFanin0New->Value;
            pObjNew->fCompl0 = Gia_ObjFaninC0(pObj);
            // deref the fanin
            if ( --Gia_ObjFanin0(pObj)->Value == 0 )
            {
                pFront[pFanin0New->Value] = 0;
                nCrossCut--;
            }
            continue;
        }
        if ( Gia_ObjValue(pObj) && nCrossCutMax < ++nCrossCut )
            nCrossCutMax = nCrossCut;
        // create new node
        pObjNew = Gia_ManAppendObj( pNew );
        assert( Gia_ObjId(pNew, pObjNew) == Gia_ObjId(p, pObj) );
        // assign the first fanin
        pFanin0New = Gia_ManObj( pNew, Gia_ObjFaninId0(pObj, i) );
        assert( pFanin0New->Value != GIA_NONE );
        pObjNew->iDiff0 = pFanin0New->Value;
        pObjNew->fCompl0 = Gia_ObjFaninC0(pObj);
        // assign the second fanin
        pFanin1New = Gia_ManObj( pNew, Gia_ObjFaninId1(pObj, i) );
        assert( pFanin1New->Value != GIA_NONE );
        pObjNew->iDiff1 = pFanin1New->Value;
        pObjNew->fCompl1 = Gia_ObjFaninC1(pObj);
        // assign the frontier number
        pObjNew->Value = iFront = Gia_ManFrontFindNext( pFront, pNew->nFront, iFront );
        // deref the fanins
        if ( --Gia_ObjFanin0(pObj)->Value == 0 )
        {
            pFront[pFanin0New->Value] = 0;
            nCrossCut--;
        }
        if ( --Gia_ObjFanin1(pObj)->Value == 0 )
        {
            pFront[pFanin1New->Value] = 0;
            nCrossCut--;
        }
        // handle nodes without fanout (choice nodes)
        if ( Gia_ObjValue(pObj) == 0 )
            pFront[iFront] = 0;
    }
    assert( pNew->nObjs == p->nObjs );
241
    assert( nCrossCut == 0 || nCrossCutMax == nCrossCutMaxInit );
Alan Mishchenko committed
242 243 244 245
    for ( i = 0; i < pNew->nFront; i++ )
        assert( pFront[i] == 0 );
    ABC_FREE( pFront );
//printf( "Crosscut = %6d. Frontier = %6d. ", nCrossCutMaxInit, pNew->nFront );
246
//ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
247 248 249 250 251 252 253 254 255 256 257 258 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 285 286 287 288 289 290 291 292 293
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    return pNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManFrontTest( Gia_Man_t * p )
{
    Gia_Man_t * pNew;
    pNew  = Gia_ManFront( p );
    Gia_ManFrontTransform( pNew );
//    Gia_ManCleanValue( p );
//    Gia_ManCleanValue( pNew );
    if ( memcmp( pNew->pObjs, p->pObjs, sizeof(Gia_Obj_t) * p->nObjs ) )
    {
/*
        Gia_Obj_t * pObj, * pObjNew;
        int i;
        Gia_ManForEachObj( p, pObj, i )
        {
            pObjNew = Gia_ManObj( pNew, i );
            printf( "%5d %5d   %5d %5d\n", 
                pObj->iDiff0, pObjNew->iDiff0, 
                pObj->iDiff1, pObjNew->iDiff1 );
        }
*/
        printf( "Verification failed.\n" );
    }
    else
        printf( "Verification successful.\n" );
    Gia_ManStop( pNew );
}

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


294 295
ABC_NAMESPACE_IMPL_END