summaryrefslogtreecommitdiffstats
path: root/src/opt/dau/dauNpn.c
blob: 7767f596c4c7c77549d654ecdd4d348d44c7f162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
93
94
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
127
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
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
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
/**CFile****************************************************************

  FileName    [dau.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware unmapping.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "dauInt.h"
#include "misc/util/utilTruth.h"
#include "misc/extra/extra.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define USE4VARS 1

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////
 
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dau_TruthEnum()
{
    int fUseTable = 1;
    abctime clk = Abc_Clock();
#ifdef USE4VARS
    int nVars   = 4;
    int nSizeW  = 1 << 14;
    char * pFileName = "tableW14.data";
#else
    int nVars   = 5;
    int nSizeW  = 1 << 30;
    char * pFileName = "tableW30.data";
#endif
    int nPerms  = Extra_Factorial( nVars );
    int nMints  = 1 << nVars;
    int * pPerm = Extra_PermSchedule( nVars );
    int * pComp = Extra_GreyCodeSchedule( nVars );
    word nFuncs = ((word)1 << (((word)1 << nVars)-1));
    word * pPres = ABC_CALLOC( word, 1 << ((1<<nVars)-7) );
    unsigned * pTable = fUseTable ? (unsigned *)ABC_CALLOC(word, nSizeW) : NULL;
    Vec_Int_t * vNpns = Vec_IntAlloc( 1000 );
    word tMask = Abc_Tt6Mask( 1 << nVars );
    word tTemp, tCur;
    int i, k;
    if ( pPres == NULL )
    {
        printf( "Cannot alloc memory for marks.\n" );
        return;
    }
    if ( pTable == NULL )
        printf( "Cannot alloc memory for table.\n" );
    for ( tCur = 0; tCur < nFuncs; tCur++ )
    {
        if ( (tCur & 0xFFFF) == 0 )
            printf( "Finished %08x\n", (int)tCur ), fflush(stdout);
        if ( Abc_TtGetBit(pPres, (int)tCur) )
            continue;
        //Extra_PrintBinary( stdout, (unsigned *)&tCur, 16 ); printf( " %04x\n", (int)tCur );
        //Dau_DsdPrintFromTruth( &tCur, 4 ); printf( "\n" );
        Vec_IntPush( vNpns, (int)tCur );
        tTemp = tCur;
        for ( i = 0; i < nPerms; i++ )
        {
            for ( k = 0; k < nMints; k++ )
            {
                if ( tCur < nFuncs )
                {
                    if ( pTable ) pTable[(int)tCur] = tTemp;
                    Abc_TtSetBit( pPres, (int)tCur );
                }
                if ( (tMask & ~tCur) < nFuncs )
                {
                    if ( pTable ) pTable[(int)(tMask & ~tCur)] = tTemp;
                    Abc_TtSetBit( pPres, (int)(tMask & ~tCur) );
                }
                tCur = Abc_Tt6Flip( tCur, pComp[k] );
            }
            tCur = Abc_Tt6SwapAdjacent( tCur, pPerm[i] );
        }
        assert( tTemp == tCur );
    }
    printf( "Computed %d NPN classes of %d variables.  ", Vec_IntSize(vNpns), nVars );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    fflush(stdout);
    Vec_IntFree( vNpns );
    ABC_FREE( pPres );
    ABC_FREE( pPerm );
    ABC_FREE( pComp );
    // write into file
    if ( pTable )
    {
        FILE * pFile = fopen( pFileName, "wb" );
        int RetValue = fwrite( pTable, 8, nSizeW, pFile );
        fclose( pFile );
        ABC_FREE( pTable );
    }
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned * Dau_ReadFile( char * pFileName, int nSizeW )
{
    FILE * pFile = fopen( pFileName, "rb" );
    unsigned * p = (unsigned *)ABC_CALLOC(word, nSizeW);
    int RetValue = fread( p, sizeof(word), nSizeW, pFile );
    fclose( pFile );
    return p;
}
void Dau_AddFunction( word tCur, int nVars, unsigned * pTable, Vec_Int_t * vNpns )
{
    int Digit  = (1 << nVars)-1;
    word tMask = Abc_Tt6Mask( 1 << nVars );
    word tNorm = (tCur >> Digit) & 1 ? ~tCur : tCur;
    unsigned t = (unsigned)tNorm & tMask;
    unsigned tRep  = pTable[t];
    unsigned tRep2 = pTable[tRep & tMask];
    assert( ((tNorm >> Digit) & 1) == 0 );
    assert( (tRep & tMask) == (tRep2 & tMask) );
    if ( (tRep2 >> 31) == 0 ) // first time
    {
        Vec_IntPush( vNpns, tRep2 );
        pTable[tRep2] |= (1 << 31);
    }
}
void Dau_NetworkEnum()
{
    abctime clk = Abc_Clock();
    int Limit = 1;
#ifdef USE4VARS
    int nVars = 4;
    int nSizeW  = 1 << 14;
    char * pFileName = "tableW14.data";
#else
    int nVars = 5;
    int nSizeW  = 1 << 30;
    char * pFileName = "tableW30.data";
#endif
    unsigned * pTable = Dau_ReadFile( pFileName, nSizeW );
    Vec_Int_t * vNpns = Vec_IntAlloc( 1000 );
    int i, v, g, k, m, Entry, Count = 1, iPrev = 0, iLast = 1;
    unsigned Inv = (unsigned)Abc_Tt6Mask(1 << (nVars-1));
    pTable[0]   |= (1 << 31);
    pTable[Inv] |= (1 << 31);
    Vec_IntPushTwo( vNpns, 0, Inv );
    Vec_IntForEachEntryStart( vNpns, Entry, i, 1 )
    {
        word uTruth = (((word)Entry) << 32) | (word)Entry;
        int nSupp = Abc_TtSupportSize( &uTruth, nVars );
        //printf( "Exploring function %4d with %d vars: ", i, nSupp );
        //printf( " %04x\n", (int)uTruth );
        //Dau_DsdPrintFromTruth( &uTruth, 4 );
        for ( v = 0; v < nSupp; v++ )
        {
            word Cof0 = Abc_Tt6Cofactor0( uTruth, nVars-1-v );
            word Cof1 = Abc_Tt6Cofactor1( uTruth, nVars-1-v );
            for ( g = 0; g < Limit; g++ )
            {
                if ( nSupp < nVars )
                {
                    word tGate = g ? s_Truths6[nVars-1-v] ^ s_Truths6[nVars-1-nSupp] : s_Truths6[nVars-1-v] & s_Truths6[nVars-1-nSupp];
                    word tCur  = (tGate & Cof1) | (~tGate & Cof0);
                    Dau_AddFunction( tCur, nVars, pTable, vNpns );
                }
            }
            for ( g = 0; g < Limit; g++ )
            {
                // add one cross bar
                for ( k = 0; k < nSupp; k++ ) if ( k != v )
                {
                    word tGate = g ? s_Truths6[nVars-1-v] ^ s_Truths6[nVars-1-k] : s_Truths6[nVars-1-v] & s_Truths6[nVars-1-k];
                    word tCur  = (tGate & Cof1) | (~tGate & Cof0);
                    Dau_AddFunction( tCur, nVars, pTable, vNpns );
                    if ( g == 0 ) // skip XOR
                    {
                        word tGate = s_Truths6[nVars-1-v] & ~s_Truths6[nVars-1-k];
                        word tCur  = (tGate & Cof1) | (~tGate & Cof0);
                        Dau_AddFunction( tCur, nVars, pTable, vNpns );
                    }
                }
            }
            for ( g = 0; g < Limit; g++ )
            {
                // add two cross bars
                for ( k = 0;   k < nSupp; k++ ) if ( k != v )
                for ( m = k+1; m < nSupp; m++ ) if ( m != v )
                {
                    word tGate = g ? s_Truths6[nVars-1-m] ^ s_Truths6[nVars-1-k] : s_Truths6[nVars-1-m] & s_Truths6[nVars-1-k];
                    word tCur  = (tGate & Cof1) | (~tGate & Cof0);
                    Dau_AddFunction( tCur, nVars, pTable, vNpns );
                    if ( g == 0 ) // skip XOR
                    {
                        tGate =  s_Truths6[nVars-1-m] & ~s_Truths6[nVars-1-k];
                        tCur  = (tGate & Cof1) | (~tGate & Cof0);
                        Dau_AddFunction( tCur, nVars, pTable, vNpns );

                        tGate = ~s_Truths6[nVars-1-m] &  s_Truths6[nVars-1-k];
                        tCur  = (tGate & Cof1) | (~tGate & Cof0);
                        Dau_AddFunction( tCur, nVars, pTable, vNpns );

                        tGate = ~s_Truths6[nVars-1-m] & ~s_Truths6[nVars-1-k];
                        tCur  = (tGate & Cof1) | (~tGate & Cof0);
                        Dau_AddFunction( tCur, nVars, pTable, vNpns );
                    }
                }
            }
        }
        if ( i == iLast )
        {
            //printf("Finished %d nodes with %d functions.\n", Count++, Vec_IntSize(vNpns) );
            iPrev = iLast;
            iLast = Vec_IntSize(vNpns)-1;
            printf("Finished %d nodes with %d functions.  ", Count++, iLast - iPrev );
            Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
            fflush(stdout);
        }
    }
    Vec_IntFree( vNpns );
    ABC_FREE( pTable );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    fflush(stdout);
}
void Dau_NetworkEnumTest()
{
    Dau_TruthEnum();
    Dau_NetworkEnum();
}


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


ABC_NAMESPACE_IMPL_END