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

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Truth-table-based bi-decomposition engine.]

  Synopsis    [The gateway to bi-decomposition.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - January 30, 2007.]

  Revision    [$Id: bdcCore.c,v 1.00 2007/01/30 00:00:00 alanmi Exp $]

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

#include "bdcInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
30

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

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

Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47 48
  Synopsis    [Accessing contents of the node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Bdc_Fun_t *  Bdc_ManFunc( Bdc_Man_t * p, int i )               { return Bdc_FunWithId(p, i); }
Bdc_Fun_t *  Bdc_ManRoot( Bdc_Man_t * p )                      { return p->pRoot;            }
int          Bdc_ManNodeNum( Bdc_Man_t * p )                   { return p->nNodes;           }
49
int          Bdc_ManAndNum( Bdc_Man_t * p )                    { return p->nNodes-p->nVars-1;}
Alan Mishchenko committed
50 51 52
Bdc_Fun_t *  Bdc_FuncFanin0( Bdc_Fun_t * p )                   { return p->pFan0;            }
Bdc_Fun_t *  Bdc_FuncFanin1( Bdc_Fun_t * p )                   { return p->pFan1;            }
void *       Bdc_FuncCopy( Bdc_Fun_t * p )                     { return p->pCopy;            }
53
int          Bdc_FuncCopyInt( Bdc_Fun_t * p )                  { return p->iCopy;            }
Alan Mishchenko committed
54
void         Bdc_FuncSetCopy( Bdc_Fun_t * p, void * pCopy )    { p->pCopy = pCopy;           }
55
void         Bdc_FuncSetCopyInt( Bdc_Fun_t * p, int iCopy )    { p->iCopy = iCopy;           }
Alan Mishchenko committed
56 57 58

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

Alan Mishchenko committed
59 60 61 62 63 64 65 66 67 68 69 70
  Synopsis    [Allocate resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Bdc_Man_t * Bdc_ManAlloc( Bdc_Par_t * pPars )
{
    Bdc_Man_t * p;
Alan Mishchenko committed
71
    p = ABC_ALLOC( Bdc_Man_t, 1 );
Alan Mishchenko committed
72
    memset( p, 0, sizeof(Bdc_Man_t) );
Alan Mishchenko committed
73
    assert( pPars->nVarsMax > 1 && pPars->nVarsMax < 16 );
Alan Mishchenko committed
74 75 76 77 78
    p->pPars = pPars;
    p->nWords = Kit_TruthWordNum( pPars->nVarsMax );
    p->nDivsLimit = 200;
    // internal nodes
    p->nNodesAlloc = 512;
Alan Mishchenko committed
79
    p->pNodes = ABC_ALLOC( Bdc_Fun_t, p->nNodesAlloc );
Alan Mishchenko committed
80
    // memory
Alan Mishchenko committed
81 82
    p->vMemory = Vec_IntStart( 8 * p->nWords * p->nNodesAlloc );
    Vec_IntClear(p->vMemory);
Alan Mishchenko committed
83 84
    // set up hash table
    p->nTableSize = (1 << p->pPars->nVarsMax);
Alan Mishchenko committed
85
    p->pTable = ABC_ALLOC( Bdc_Fun_t *, p->nTableSize );
Alan Mishchenko committed
86 87 88
    memset( p->pTable, 0, sizeof(Bdc_Fun_t *) * p->nTableSize );
    p->vSpots = Vec_IntAlloc( 256 );
    // truth tables
Alan Mishchenko committed
89
    p->vTruths = Vec_PtrAllocTruthTables( p->pPars->nVarsMax );
Alan Mishchenko committed
90
    p->puTemp1 = ABC_ALLOC( unsigned, 4 * p->nWords );
Alan Mishchenko committed
91 92 93
    p->puTemp2 = p->puTemp1 + p->nWords;
    p->puTemp3 = p->puTemp2 + p->nWords;
    p->puTemp4 = p->puTemp3 + p->nWords;
Alan Mishchenko committed
94 95 96 97 98
    // start the internal ISFs
    p->pIsfOL = &p->IsfOL;  Bdc_IsfStart( p, p->pIsfOL );
    p->pIsfOR = &p->IsfOR;  Bdc_IsfStart( p, p->pIsfOR );
    p->pIsfAL = &p->IsfAL;  Bdc_IsfStart( p, p->pIsfAL );
    p->pIsfAR = &p->IsfAR;  Bdc_IsfStart( p, p->pIsfAR );   
Alan Mishchenko committed
99
    return p; 
Alan Mishchenko committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

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

  Synopsis    [Deallocate resynthesis manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Bdc_ManFree( Bdc_Man_t * p )
{
Alan Mishchenko committed
115 116 117 118 119 120
    if ( p->pPars->fVerbose )
    {
        printf( "Bi-decomposition stats: Calls = %d.  Nodes = %d. Reuse = %d.\n", 
            p->numCalls, p->numNodes, p->numReuse );
        printf( "ANDs = %d.  ORs = %d.  Weak = %d.  Muxes = %d.  Memory = %.2f K\n", 
            p->numAnds, p->numOrs, p->numWeaks, p->numMuxes, 4.0 * Vec_IntSize(p->vMemory) / (1<<10) );
Alan Mishchenko committed
121 122 123 124 125
        ABC_PRT( "Cache", p->timeCache );
        ABC_PRT( "Check", p->timeCheck );
        ABC_PRT( "Muxes", p->timeMuxes );
        ABC_PRT( "Supps", p->timeSupps );
        ABC_PRT( "TOTAL", p->timeTotal );
Alan Mishchenko committed
126
    }
Alan Mishchenko committed
127 128 129
    Vec_IntFree( p->vMemory );
    Vec_IntFree( p->vSpots );
    Vec_PtrFree( p->vTruths );
Alan Mishchenko committed
130 131 132 133
    ABC_FREE( p->puTemp1 );
    ABC_FREE( p->pNodes );
    ABC_FREE( p->pTable );
    ABC_FREE( p );
Alan Mishchenko committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

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

  Synopsis    [Clears the manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Bdc_ManPrepare( Bdc_Man_t * p, Vec_Ptr_t * vDivs )
{
    unsigned * puTruth;
    Bdc_Fun_t * pNode;
    int i;
    Bdc_TableClear( p );
    Vec_IntClear( p->vMemory );
    // add constant 1 and elementary vars
Alan Mishchenko committed
155 156 157 158 159
    p->nNodes = 0;
    p->nNodesNew = - 1 - p->nVars - (vDivs? Vec_PtrSize(vDivs) : 0);
    // add constant 1
    pNode = Bdc_FunNew( p );
    pNode->Type = BDC_TYPE_CONST1;
Alan Mishchenko committed
160 161
    pNode->puFunc = (unsigned *)Vec_IntFetch(p->vMemory, p->nWords); 
    Kit_TruthFill( pNode->puFunc, p->nVars );
Alan Mishchenko committed
162 163 164 165
    pNode->uSupp = 0;
    Bdc_TableAdd( p, pNode );
    // add variables
    for ( i = 0; i < p->nVars; i++ )
Alan Mishchenko committed
166 167 168
    {
        pNode = Bdc_FunNew( p );
        pNode->Type = BDC_TYPE_PI;
169
        pNode->puFunc = (unsigned *)Vec_PtrEntry( p->vTruths, i );
Alan Mishchenko committed
170
        pNode->uSupp = (1 << i);
Alan Mishchenko committed
171 172 173
        Bdc_TableAdd( p, pNode );
    }
    // add the divisors
Alan Mishchenko committed
174
    if ( vDivs )
175
    Vec_PtrForEachEntry( unsigned *, vDivs, puTruth, i )
Alan Mishchenko committed
176 177 178 179 180 181 182 183 184
    {
        pNode = Bdc_FunNew( p );
        pNode->Type = BDC_TYPE_PI;
        pNode->puFunc = puTruth;
        pNode->uSupp = Kit_TruthSupport( puTruth, p->nVars );
        Bdc_TableAdd( p, pNode );
        if ( i == p->nDivsLimit )
            break;
    }
Alan Mishchenko committed
185 186 187 188 189
    assert( p->nNodesNew == 0 );
}

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

190
  Synopsis    [Prints bi-decomposition in a simple format.]
Alan Mishchenko committed
191 192 193 194 195 196 197 198

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
199
void Bdc_ManDecPrintSimple( Bdc_Man_t * p )
Alan Mishchenko committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
    Bdc_Fun_t * pNode;
    int i;
    printf( " 0 : Const 1\n" );
    for ( i = 1; i < p->nNodes; i++ )
    {
        printf( " %d : ", i );
        pNode = p->pNodes + i;
        if ( pNode->Type == BDC_TYPE_PI )
            printf( "PI   " );
        else
        {
            printf( "%s%d &", Bdc_IsComplement(pNode->pFan0)? "-":"", Bdc_FunId(p,Bdc_Regular(pNode->pFan0)) );
            printf( " %s%d   ", Bdc_IsComplement(pNode->pFan1)? "-":"", Bdc_FunId(p,Bdc_Regular(pNode->pFan1)) );
        }
215
//        Extra_PrintBinary( stdout, pNode->puFunc, (1<<p->nVars) );
Alan Mishchenko committed
216 217 218
        printf( "\n" );
    }
    printf( "Root = %s%d.\n", Bdc_IsComplement(p->pRoot)? "-":"", Bdc_FunId(p,Bdc_Regular(p->pRoot)) );
Alan Mishchenko committed
219 220 221 222
}

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

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 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
  Synopsis    [Prints bi-decomposition recursively.]

  Description [This procedure prints bi-decomposition as a factored form.
  In doing so, logic sharing, if present, will be replicated several times.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Bdc_ManDecPrint_rec( Bdc_Man_t * p, Bdc_Fun_t * pNode )
{
    if ( pNode->Type == BDC_TYPE_PI )
        printf( "%c", 'a' + Bdc_FunId(p,pNode) - 1 );
    else if ( pNode->Type == BDC_TYPE_AND )
    {
        Bdc_Fun_t * pNode0 = Bdc_FuncFanin0( pNode );
        Bdc_Fun_t * pNode1 = Bdc_FuncFanin1( pNode );

        if ( Bdc_IsComplement(pNode0) )
            printf( "!" );
        if ( Bdc_IsComplement(pNode0) && Bdc_Regular(pNode0)->Type != BDC_TYPE_PI )
            printf( "(" );
        Bdc_ManDecPrint_rec( p, Bdc_Regular(pNode0) );
        if ( Bdc_IsComplement(pNode0) && Bdc_Regular(pNode0)->Type != BDC_TYPE_PI )
            printf( ")" );

        if ( Bdc_IsComplement(pNode1) )
            printf( "!" );
        if ( Bdc_IsComplement(pNode1) && Bdc_Regular(pNode1)->Type != BDC_TYPE_PI )
            printf( "(" );
        Bdc_ManDecPrint_rec( p, Bdc_Regular(pNode1) );
        if ( Bdc_IsComplement(pNode1) && Bdc_Regular(pNode1)->Type != BDC_TYPE_PI )
            printf( ")" );
    }
    else assert( 0 );
}
void Bdc_ManDecPrint( Bdc_Man_t * p )
{
    Bdc_Fun_t * pRoot = Bdc_Regular(p->pRoot);

    printf( "F = " );
    if ( pRoot->Type == BDC_TYPE_CONST1 ) // constant 0
        printf( "Constant %d", !Bdc_IsComplement(p->pRoot) );
    else if ( pRoot->Type == BDC_TYPE_PI ) // literal
        printf( "%s%d", Bdc_IsComplement(p->pRoot) ? "!" : "", Bdc_FunId(p,pRoot)-1 );
    else
    {
        if ( Bdc_IsComplement(p->pRoot) )
            printf( "!(" );
        Bdc_ManDecPrint_rec( p, pRoot );
        if ( Bdc_IsComplement(p->pRoot) )
            printf( ")" );
    }
    printf( "\n" );
}

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

Alan Mishchenko committed
282 283 284 285 286 287 288 289 290 291 292 293
  Synopsis    [Performs decomposition of one function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Bdc_ManDecompose( Bdc_Man_t * p, unsigned * puFunc, unsigned * puCare, int nVars, Vec_Ptr_t * vDivs, int nNodesMax )
{
    Bdc_Isf_t Isf, * pIsf = &Isf;
294
    clock_t clk = clock();
Alan Mishchenko committed
295
    assert( nVars <= p->pPars->nVarsMax );
Alan Mishchenko committed
296 297 298
    // set current manager parameters
    p->nVars = nVars;
    p->nWords = Kit_TruthWordNum( nVars );
Alan Mishchenko committed
299
    p->nNodesMax = nNodesMax;
Alan Mishchenko committed
300
    Bdc_ManPrepare( p, vDivs );
Alan Mishchenko committed
301 302 303 304 305
    if ( puCare && Kit_TruthIsConst0( puCare, nVars ) )
    {
        p->pRoot = Bdc_Not(p->pNodes);
        return 0;
    }
Alan Mishchenko committed
306 307
    // copy the function
    Bdc_IsfStart( p, pIsf );
Alan Mishchenko committed
308 309 310 311 312 313 314 315 316 317
    if ( puCare )
    {
        Kit_TruthAnd( pIsf->puOn, puCare, puFunc, p->nVars );
        Kit_TruthSharp( pIsf->puOff, puCare, puFunc, p->nVars );
    }
    else
    {
        Kit_TruthCopy( pIsf->puOn, puFunc, p->nVars );
        Kit_TruthNot( pIsf->puOff, puFunc, p->nVars );
    }
Alan Mishchenko committed
318
    Bdc_SuppMinimize( p, pIsf );
Alan Mishchenko committed
319
    // call decomposition
Alan Mishchenko committed
320
    p->pRoot = Bdc_ManDecompose_rec( p, pIsf );
Alan Mishchenko committed
321 322 323
    p->timeTotal += clock() - clk;
    p->numCalls++;
    p->numNodes += p->nNodesNew;
Alan Mishchenko committed
324 325
    if ( p->pRoot == NULL )
        return -1;
Alan Mishchenko committed
326 327 328
    if ( !Bdc_ManNodeVerify( p, pIsf, p->pRoot ) )
        printf( "Bdc_ManDecompose(): Internal verification failed.\n" );
//    assert( Bdc_ManNodeVerify( p, pIsf, p->pRoot ) );
Alan Mishchenko committed
329 330 331
    return p->nNodesNew;
}

Alan Mishchenko committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/**Function*************************************************************

  Synopsis    [Performs decomposition of one function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Bdc_ManDecomposeTest( unsigned uTruth, int nVars )
{
    static int Counter = 0;
    static int Total = 0;
    Bdc_Par_t Pars = {0}, * pPars = &Pars;
    Bdc_Man_t * p;
    int RetValue;
//    unsigned uCare = ~0x888f888f;
    unsigned uCare = ~0;
//    unsigned uFunc =  0x88888888;
//    unsigned uFunc =  0xf888f888;
//    unsigned uFunc =  0x117e117e;
//    unsigned uFunc =  0x018b018b;
    unsigned uFunc = uTruth; 

    pPars->nVarsMax = 8;
    p = Bdc_ManAlloc( pPars );
    RetValue = Bdc_ManDecompose( p, &uFunc, &uCare, nVars, NULL, 1000 );
    Total += RetValue;
    printf( "%5d : Nodes = %5d. Total = %8d.\n", ++Counter, RetValue, Total );
//    Bdc_ManDecPrint( p );
    Bdc_ManFree( p );
}

Alan Mishchenko committed
367 368 369 370 371 372

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


373 374
ABC_NAMESPACE_IMPL_END