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

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware AIG rewriting package.]

  Synopsis    [Computation of practically used NN-classes of 4-input cuts.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "rwr.h"

23 24 25
ABC_NAMESPACE_IMPL_START


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

Alan Mishchenko committed
30 31
typedef struct Rwr_Man4_t_ Rwr_Man4_t;
struct Rwr_Man4_t_
Alan Mishchenko committed
32 33 34 35 36 37 38 39 40
{
    // internal lookups
    int                nFuncs;           // the number of four-var functions
    unsigned short *   puCanons;         // canonical forms
    int *              pnCounts;         // the counters of functions in each class
    int                nConsidered;      // the number of nodes considered
    int                nClasses;         // the number of NN classes
};

Alan Mishchenko committed
41 42 43 44 45 46 47 48 49 50
typedef struct Rwr_Man5_t_ Rwr_Man5_t;
struct Rwr_Man5_t_
{
    // internal lookups
    stmm_table *       tTableNN;         // the NN canonical forms
    stmm_table *       tTableNPN;        // the NPN canonical forms
};

static Rwr_Man4_t * s_pManRwrExp4 = NULL;
static Rwr_Man5_t * s_pManRwrExp5 = NULL;
Alan Mishchenko committed
51 52

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
53
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
54 55 56 57 58 59 60 61 62 63 64 65 66
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Collects stats about 4-var functions appearing in netlists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
67
void Rwt_Man4ExploreStart()
Alan Mishchenko committed
68
{
Alan Mishchenko committed
69
    Rwr_Man4_t * p;
Alan Mishchenko committed
70
    p = ABC_ALLOC( Rwr_Man4_t, 1 );
Alan Mishchenko committed
71
    memset( p, 0, sizeof(Rwr_Man4_t) );
Alan Mishchenko committed
72 73
    // canonical forms
    p->nFuncs    = (1<<16);
Alan Mishchenko committed
74
    // canonical forms, phases, perms
Alan Mishchenko committed
75
    Extra_Truth4VarNPN( &p->puCanons, NULL, NULL, NULL );
Alan Mishchenko committed
76
    // counters
Alan Mishchenko committed
77
    p->pnCounts  = ABC_ALLOC( int, p->nFuncs );
Alan Mishchenko committed
78
    memset( p->pnCounts, 0, sizeof(int) * p->nFuncs );
Alan Mishchenko committed
79
    s_pManRwrExp4 = p;
Alan Mishchenko committed
80 81 82 83 84 85 86 87 88 89 90 91 92
}

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

  Synopsis    [Collects stats about 4-var functions appearing in netlists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
93
void Rwt_Man4ExploreCount( unsigned uTruth )
Alan Mishchenko committed
94 95
{
    assert( uTruth < (1<<16) );
Alan Mishchenko committed
96
    s_pManRwrExp4->pnCounts[ s_pManRwrExp4->puCanons[uTruth] ]++;    
Alan Mishchenko committed
97 98 99 100 101 102 103 104 105 106 107 108 109
}

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

  Synopsis    [Collects stats about 4-var functions appearing in netlists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
110
void Rwt_Man4ExplorePrint()
Alan Mishchenko committed
111 112 113 114 115 116 117 118
{
    FILE * pFile;
    int i, CountMax, CountWrite, nCuts, nClasses;
    int * pDistrib;
    int * pReprs;
    // find the max number of occurences
    nCuts = nClasses = 0;
    CountMax = 0;
Alan Mishchenko committed
119
    for ( i = 0; i < s_pManRwrExp4->nFuncs; i++ )
Alan Mishchenko committed
120
    {
Alan Mishchenko committed
121 122 123 124
        if ( CountMax < s_pManRwrExp4->pnCounts[i] )
            CountMax = s_pManRwrExp4->pnCounts[i];
        nCuts += s_pManRwrExp4->pnCounts[i];
        if ( s_pManRwrExp4->pnCounts[i] > 0 )
Alan Mishchenko committed
125 126 127 128 129
            nClasses++;
    }
    printf( "Number of cuts considered       = %8d.\n", nCuts );
    printf( "Classes occurring at least once = %8d.\n", nClasses );
    // print the distribution of classes
Alan Mishchenko committed
130 131
    pDistrib = ABC_ALLOC( int, CountMax + 1 );
    pReprs   = ABC_ALLOC( int, CountMax + 1 );
Alan Mishchenko committed
132
    memset( pDistrib, 0, sizeof(int)*(CountMax + 1) );
Alan Mishchenko committed
133
    for ( i = 0; i < s_pManRwrExp4->nFuncs; i++ )
Alan Mishchenko committed
134
    {
Alan Mishchenko committed
135 136
        pDistrib[ s_pManRwrExp4->pnCounts[i] ]++;
        pReprs[ s_pManRwrExp4->pnCounts[i] ] = i;
Alan Mishchenko committed
137 138 139 140 141 142 143 144 145 146
    }

    printf( "Occurence = %6d.  Num classes = %4d.  \n", 0, 2288-nClasses );
    for ( i = 1; i <= CountMax; i++ )
        if ( pDistrib[i] )
        {
            printf( "Occurence = %6d.  Num classes = %4d.  Repr = ", i, pDistrib[i] );
            Extra_PrintBinary( stdout, (unsigned*)&(pReprs[i]), 16 ); 
            printf( "\n" );
        }
Alan Mishchenko committed
147 148
    ABC_FREE( pDistrib );
    ABC_FREE( pReprs );
Alan Mishchenko committed
149 150
    // write into a file all classes above limit (5)
    CountWrite = 0;
Alan Mishchenko committed
151 152 153
    pFile = fopen( "npnclass_stats4.txt", "w" );
    for ( i = 0; i < s_pManRwrExp4->nFuncs; i++ )
        if ( s_pManRwrExp4->pnCounts[i] > 0 )
Alan Mishchenko committed
154
        {
155
            Extra_PrintHex( pFile, (unsigned *)&i, 4 );
Alan Mishchenko committed
156 157
            fprintf( pFile, " %10d\n", s_pManRwrExp4->pnCounts[i] );
//            fprintf( pFile, "%d ", i );
Alan Mishchenko committed
158 159 160
            CountWrite++;
        }
    fclose( pFile );
Alan Mishchenko committed
161
    printf( "%d classes written into file \"%s\".\n", CountWrite, "npnclass_stats4.txt" );
Alan Mishchenko committed
162 163
}

Alan Mishchenko committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180



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

  Synopsis    [Collects stats about 4-var functions appearing in netlists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwt_Man5ExploreStart()
{
    Rwr_Man5_t * p;
Alan Mishchenko committed
181
    p = ABC_ALLOC( Rwr_Man5_t, 1 );
Alan Mishchenko committed
182
    memset( p, 0, sizeof(Rwr_Man5_t) );
183 184
    p->tTableNN  = stmm_init_table( st__numcmp, st__numhash );
    p->tTableNPN = stmm_init_table( st__numcmp, st__numhash );
Alan Mishchenko committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    s_pManRwrExp5 = p;

//Extra_PrintHex( stdout, Extra_TruthCanonNPN( 0x0000FFFF, 5 ), 5 );
//printf( "\n" );
}

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

  Synopsis    [Collects stats about 4-var functions appearing in netlists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwt_Man5ExploreCount( unsigned uTruth )
{
    int * pCounter;
Alan Mishchenko committed
205
    if ( !stmm_find_or_add( s_pManRwrExp5->tTableNN, (char *)(ABC_PTRUINT_T)uTruth, (char***)&pCounter ) )
Alan Mishchenko committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        *pCounter = 0;
    (*pCounter)++;
}

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

  Synopsis    [Collects stats about 4-var functions appearing in netlists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Rwt_Man5ExplorePrint()
{
    FILE * pFile;
    stmm_generator * gen;
    int i, CountMax, nCuts, Counter;
    int * pDistrib;
    unsigned * pReprs;
    unsigned uTruth, uTruthC;
229
    abctime clk = Abc_Clock();
Alan Mishchenko committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    Vec_Int_t * vClassesNN, * vClassesNPN;

    // find the max number of occurences
    nCuts = 0;
    CountMax = 0;
    stmm_foreach_item( s_pManRwrExp5->tTableNN, gen, (char **)&uTruth, (char **)&Counter )
    {
        nCuts += Counter;
        if ( CountMax < Counter )
            CountMax = Counter;
    }
    printf( "Number of cuts considered       = %8d.\n", nCuts );
    printf( "Classes occurring at least once = %8d.\n", stmm_count(s_pManRwrExp5->tTableNN) );
    printf( "The largest number of occurence = %8d.\n", CountMax );

    // print the distribution of classes
Alan Mishchenko committed
246 247
    pDistrib = ABC_ALLOC( int, CountMax + 1 );
    pReprs   = ABC_ALLOC( unsigned, CountMax + 1 );
Alan Mishchenko committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    memset( pDistrib, 0, sizeof(int)*(CountMax + 1) );
    stmm_foreach_item( s_pManRwrExp5->tTableNN, gen, (char **)&uTruth, (char **)&Counter )
    {
        assert( Counter <= CountMax );
        pDistrib[ Counter ]++;
        pReprs[ Counter ] = uTruth;
    }

    for ( i = 1; i <= CountMax; i++ )
        if ( pDistrib[i] )
        {
            printf( "Occurence = %6d.  Num classes = %4d.  Repr = ", i, pDistrib[i] );
            Extra_PrintBinary( stdout, pReprs + i, 32 ); 
            printf( "\n" );
        }
Alan Mishchenko committed
263 264
    ABC_FREE( pDistrib );
    ABC_FREE( pReprs );
Alan Mishchenko committed
265 266 267 268 269 270 271 272 273 274 275 276


    // put them into an array
    vClassesNN = Vec_IntAlloc( stmm_count(s_pManRwrExp5->tTableNN) );
    stmm_foreach_item( s_pManRwrExp5->tTableNN, gen, (char **)&uTruth, NULL )
        Vec_IntPush( vClassesNN, (int)uTruth );
    Vec_IntSortUnsigned( vClassesNN );

    // write into a file all classes
    pFile = fopen( "nnclass_stats5.txt", "w" );
    Vec_IntForEachEntry( vClassesNN, uTruth, i )
    {
Alan Mishchenko committed
277
        if ( !stmm_lookup( s_pManRwrExp5->tTableNN, (char *)(ABC_PTRUINT_T)uTruth, (char **)&Counter ) )
Alan Mishchenko committed
278 279 280
        {
            assert( 0 );
        }
281
        Extra_PrintHex( pFile, &uTruth, 5 );
Alan Mishchenko committed
282 283 284 285 286 287
        fprintf( pFile, " %10d\n", Counter );
    }
    fclose( pFile );
    printf( "%d classes written into file \"%s\".\n", vClassesNN->nSize, "nnclass_stats5.txt" );


288
clk = Abc_Clock();
Alan Mishchenko committed
289 290 291 292 293
    // how many NPN classes exist?
    Vec_IntForEachEntry( vClassesNN, uTruth, i )
    {
        int * pCounter;
        uTruthC = Extra_TruthCanonNPN( uTruth, 5 );
Alan Mishchenko committed
294
        if ( !stmm_find_or_add( s_pManRwrExp5->tTableNPN, (char *)(ABC_PTRUINT_T)uTruthC, (char***)&pCounter ) )
Alan Mishchenko committed
295
            *pCounter = 0;
Alan Mishchenko committed
296
        if ( !stmm_lookup( s_pManRwrExp5->tTableNN, (char *)(ABC_PTRUINT_T)uTruth, (char **)&Counter ) )
Alan Mishchenko committed
297 298 299 300 301 302
        {
            assert( 0 );
        }
        (*pCounter) += Counter;
    }
    printf( "The numbe of NPN classes = %d.\n", stmm_count(s_pManRwrExp5->tTableNPN) );
303
ABC_PRT( "Computing NPN classes", Abc_Clock() - clk );
Alan Mishchenko committed
304 305 306 307 308 309 310 311 312 313 314

    // put them into an array
    vClassesNPN = Vec_IntAlloc( stmm_count(s_pManRwrExp5->tTableNPN) );
    stmm_foreach_item( s_pManRwrExp5->tTableNPN, gen, (char **)&uTruth, NULL )
        Vec_IntPush( vClassesNPN, (int)uTruth );
    Vec_IntSortUnsigned( vClassesNPN );

    // write into a file all classes
    pFile = fopen( "npnclass_stats5.txt", "w" );
    Vec_IntForEachEntry( vClassesNPN, uTruth, i )
    {
Alan Mishchenko committed
315
        if ( !stmm_lookup( s_pManRwrExp5->tTableNPN, (char *)(ABC_PTRUINT_T)uTruth, (char **)&Counter ) )
Alan Mishchenko committed
316 317 318
        {
            assert( 0 );
        }
319
        Extra_PrintHex( pFile, &uTruth, 5 );
Alan Mishchenko committed
320 321 322 323 324 325 326 327 328 329
        fprintf( pFile, " %10d\n", Counter );
    }
    fclose( pFile );
    printf( "%d classes written into file \"%s\".\n", vClassesNPN->nSize, "npnclass_stats5.txt" );


    // can they be uniquely characterized?

}

Alan Mishchenko committed
330 331 332 333 334
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


335 336
ABC_NAMESPACE_IMPL_END