summaryrefslogtreecommitdiffstats
path: root/src/aig/gia/giaDecs.c
blob: 343891d5ee4d3b9049495a4762fc261675484a13 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**CFile****************************************************************

  FileName    [giaDecs.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Calling various decomposition engines.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "aig/gia/gia.h"
#include "misc/util/utilTruth.h"
#include "misc/extra/extra.h"
#include "bool/bdc/bdc.h"
#include "bool/kit/kit.h"

ABC_NAMESPACE_IMPL_START

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

extern void Extra_BitMatrixTransposeP( Vec_Wrd_t * vSimsIn, int nWordsIn, Vec_Wrd_t * vSimsOut, int nWordsOut );
extern Vec_Int_t * Gia_ManResubOne( Vec_Ptr_t * vDivs, int nWords, int nLimit, int nDivsMax, int iChoice, int fUseXor, int fDebug, int fVerbose, word * pFunc, int Depth );

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ResubVarNum( Vec_Int_t * vResub )
{
    if ( Vec_IntSize(vResub) == 1 )
        return Vec_IntEntryLast(vResub) >= 2;
    return Vec_IntEntryLast(vResub)/2 - Vec_IntSize(vResub)/2 - 1;
}
word Gia_ResubToTruth6_rec( Vec_Int_t * vResub, int iNode, int nVars )
{
    assert( iNode >= 0 && nVars <= 6 );
    if ( iNode < nVars )
        return s_Truths6[iNode];
    else
    {
        int iLit0 = Vec_IntEntry( vResub, Abc_Var2Lit(iNode-nVars, 0) );
        int iLit1 = Vec_IntEntry( vResub, Abc_Var2Lit(iNode-nVars, 1) );
        word Res0 = Gia_ResubToTruth6_rec( vResub, Abc_Lit2Var(iLit0)-2, nVars );
        word Res1 = Gia_ResubToTruth6_rec( vResub, Abc_Lit2Var(iLit1)-2, nVars );
        Res0 = Abc_LitIsCompl(iLit0) ? ~Res0 : Res0;
        Res1 = Abc_LitIsCompl(iLit1) ? ~Res1 : Res1;
        return iLit0 > iLit1 ? Res0 ^ Res1 : Res0 & Res1;
    }
}
word Gia_ResubToTruth6( Vec_Int_t * vResub )
{
    word Res;
    int iRoot = Vec_IntEntryLast(vResub);
    if ( iRoot < 2 )
        return iRoot ? ~(word)0 : 0;
    assert( iRoot != 2 && iRoot != 3 );
    Res = Gia_ResubToTruth6_rec( vResub, Abc_Lit2Var(iRoot)-2, Gia_ResubVarNum(vResub) );
    return Abc_LitIsCompl(iRoot) ? ~Res : Res;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wrd_t * Gia_ManDeriveTruths( Gia_Man_t * p, Vec_Wrd_t * vSims, Vec_Wrd_t * vIsfs, Vec_Int_t * vCands, Vec_Int_t * vSet, int nWords )
{
    int nTtWords = Abc_Truth6WordNum(Vec_IntSize(vSet));
    int nFuncs = Vec_WrdSize(vIsfs) / 2 / nWords;
    Vec_Wrd_t * vRes = Vec_WrdStart( 2 * nFuncs * nTtWords );
    Vec_Wrd_t * vIn = Vec_WrdStart( 64*nWords ), * vOut;
    int i, f, m, iObj; word Func;
    assert( Vec_IntSize(vSet) <= 64 );
    Vec_IntForEachEntry( vSet, iObj, i )
        Abc_TtCopy( Vec_WrdEntryP(vIn, i*nWords), Vec_WrdEntryP(vSims, Vec_IntEntry(vCands, iObj)*nWords), nWords, 0 );
    vOut = Vec_WrdStart( Vec_WrdSize(vIn) );
    Extra_BitMatrixTransposeP( vIn, nWords, vOut, 1 );
    for ( f = 0; f < nFuncs; f++ )
    {
        word * pIsf[2]   = { Vec_WrdEntryP(vIsfs, (2*f+0)*nWords), 
                             Vec_WrdEntryP(vIsfs, (2*f+1)*nWords) };
        word * pTruth[2] = { Vec_WrdEntryP(vRes, (2*f+0)*nTtWords), 
                             Vec_WrdEntryP(vRes, (2*f+1)*nTtWords) };
        for ( m = 0; m < 64*nWords; m++ )
        {
            int iMint = (int)Vec_WrdEntry(vOut, m);
            int Value0 = Abc_TtGetBit( pIsf[0], m );
            int Value1 = Abc_TtGetBit( pIsf[1], m );
            if ( !Value0 && !Value1 )
                continue;
            if ( Value0 && Value1 )
                printf( "Internal error: Onset and Offset overlap.\n" );
            assert( !Value0 || !Value1 );
            Abc_TtSetBit( pTruth[Value1], iMint );
        }
        if ( Abc_TtCountOnesVecMask(pTruth[0], pTruth[1], nTtWords, 0) )
            printf( "Verification for function %d failed for %d minterm pairs.\n", f,
                Abc_TtCountOnesVecMask(pTruth[0], pTruth[1], nTtWords, 0) );
    }
    if ( Vec_IntSize(vSet) < 6 )
        Vec_WrdForEachEntry( vRes, Func, i )
            Vec_WrdWriteEntry( vRes, i, Abc_Tt6Stretch(Func, Vec_IntSize(vSet)) );
    Vec_WrdFree( vIn );
    Vec_WrdFree( vOut );
    return vRes;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManCountResub( Vec_Wrd_t * vTruths, int nVars, int fVerbose )
{
    Vec_Int_t * vResub; int nNodes;
    int nTtWords = Abc_Truth6WordNum(nVars);
    int v, nFuncs = Vec_WrdSize(vTruths) / 2 / nTtWords;
    Vec_Wrd_t * vElems = Vec_WrdStartTruthTables( nVars );
    Vec_Ptr_t * vDivs = Vec_PtrAlloc( 2 + nVars );
    assert( Vec_WrdSize(vElems) == nTtWords * nVars );
    assert( nFuncs == 1 );
    Vec_PtrPush( vDivs, Vec_WrdEntryP(vTruths, (2*0+0)*nTtWords) );
    Vec_PtrPush( vDivs, Vec_WrdEntryP(vTruths, (2*0+1)*nTtWords) );
    for ( v = 0; v < nVars; v++ )
        Vec_PtrPush( vDivs, Vec_WrdEntryP(vElems, v*nTtWords) );
    vResub = Gia_ManResubOne( vDivs, nTtWords, 30, 100, 0, 0, 0, fVerbose, NULL, 0 );
    Vec_PtrFree( vDivs );
    Vec_WrdFree( vElems );
    nNodes = Vec_IntSize(vResub) ? Vec_IntSize(vResub)/2 : 999;
    Vec_IntFree( vResub );
    return nNodes;
}
Vec_Int_t * Gia_ManDeriveResub( Vec_Wrd_t * vTruths, int nVars )
{
    Vec_Int_t * vResub;  
    int nTtWords = Abc_Truth6WordNum(nVars);
    int v, nFuncs = Vec_WrdSize(vTruths) / 2 / nTtWords;
    Vec_Wrd_t * vElems = Vec_WrdStartTruthTables( nVars );
    Vec_Ptr_t * vDivs = Vec_PtrAlloc( 2 + nVars );
    assert( Vec_WrdSize(vElems) == nTtWords * nVars );
    assert( nFuncs == 1 );
    Vec_PtrPush( vDivs, Vec_WrdEntryP(vTruths, (2*0+0)*nTtWords) );
    Vec_PtrPush( vDivs, Vec_WrdEntryP(vTruths, (2*0+1)*nTtWords) );
    for ( v = 0; v < nVars; v++ )
        Vec_PtrPush( vDivs, Vec_WrdEntryP(vElems, v*nTtWords) );
    vResub = Gia_ManResubOne( vDivs, nTtWords, 30, 100, 0, 0, 0, 0, NULL, 0 );
    Vec_PtrFree( vDivs );
    Vec_WrdFree( vElems );
    return vResub;
}

int Gia_ManCountBidec( Vec_Wrd_t * vTruths, int nVars, int fVerbose )
{
    int nNodes, nTtWords = Abc_Truth6WordNum(nVars);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    Abc_TtOr( pTruth[0], pTruth[0], pTruth[1], nTtWords );
    nNodes = Bdc_ManBidecNodeNum( pTruth[1], pTruth[0], nVars, fVerbose );
    Abc_TtSharp( pTruth[0], pTruth[0], pTruth[1], nTtWords );
    return nNodes;
}
Vec_Int_t * Gia_ManDeriveBidec( Vec_Wrd_t * vTruths, int nVars )
{
    Vec_Int_t * vRes = NULL;
    int nTtWords = Abc_Truth6WordNum(nVars);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    Abc_TtOr( pTruth[0], pTruth[0], pTruth[1], nTtWords );
    vRes = Bdc_ManBidecResub( pTruth[1], pTruth[0], nVars );
    Abc_TtSharp( pTruth[0], pTruth[0], pTruth[1], nTtWords );
    return vRes;
}

int Gia_ManCountIsop( Vec_Wrd_t * vTruths, int nVars, int fVerbose )
{
    int nTtWords = Abc_Truth6WordNum(nVars);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    int nNodes = Kit_IsopNodeNum( (unsigned *)pTruth[0], (unsigned *)pTruth[1], nVars, NULL );
    return nNodes;
}
Vec_Int_t * Gia_ManDeriveIsop( Vec_Wrd_t * vTruths, int nVars )
{
    Vec_Int_t * vRes = NULL;
    int nTtWords = Abc_Truth6WordNum(nVars);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    vRes = Kit_IsopResub( (unsigned *)pTruth[0], (unsigned *)pTruth[1], nVars, NULL );
    return vRes;
}

int Gia_ManCountBdd( Vec_Wrd_t * vTruths, int nVars, int fVerbose )
{
    extern Gia_Man_t * Gia_TryPermOptNew( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
    int nTtWords = Abc_Truth6WordNum(nVars);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    Gia_Man_t * pGia; int nNodes;

    Abc_TtOr( pTruth[1], pTruth[1], pTruth[0], nTtWords );
    Abc_TtNot( pTruth[0], nTtWords );
    pGia = Gia_TryPermOptNew( pTruth[0], nVars, 1, nTtWords, 50, 0 );
    Abc_TtNot( pTruth[0], nTtWords );
    Abc_TtSharp( pTruth[1], pTruth[1], pTruth[0], nTtWords );

    nNodes = Gia_ManAndNum(pGia);
    Gia_ManStop( pGia );
    return nNodes;
}
Vec_Int_t * Gia_ManDeriveBdd( Vec_Wrd_t * vTruths, int nVars )
{
    extern Vec_Int_t * Gia_ManToGates( Gia_Man_t * p );
    Vec_Int_t * vRes = NULL;
    extern Gia_Man_t * Gia_TryPermOptNew( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
    int nTtWords = Abc_Truth6WordNum(nVars);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    Gia_Man_t * pGia;

    Abc_TtOr( pTruth[1], pTruth[1], pTruth[0], nTtWords );
    Abc_TtNot( pTruth[0], nTtWords );
    pGia = Gia_TryPermOptNew( pTruth[0], nVars, 1, nTtWords, 50, 0 );
    Abc_TtNot( pTruth[0], nTtWords );
    Abc_TtSharp( pTruth[1], pTruth[1], pTruth[0], nTtWords );

    vRes = Gia_ManToGates( pGia );
    Gia_ManStop( pGia );
    return vRes;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gia_ManEvalSolutionOne( Gia_Man_t * p, Vec_Wrd_t * vSims, Vec_Wrd_t * vIsfs, Vec_Int_t * vCands, Vec_Int_t * vSet, int nWords, int fVerbose )
{
    Vec_Wrd_t * vTruths = Gia_ManDeriveTruths( p, vSims, vIsfs, vCands, vSet, nWords );
    int nTtWords = Vec_WrdSize(vTruths)/2, nVars = Vec_IntSize(vSet);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    int nNodesResub = Gia_ManCountResub( vTruths, nVars, 0 );
    int nNodesBidec = nVars > 2 ? Gia_ManCountBidec( vTruths, nVars, 0 ) : 999;
    int nNodesIsop  = nVars > 2 ? Gia_ManCountIsop( vTruths, nVars, 0 ) : 999;
    int nNodesBdd   = nVars > 2 ? Gia_ManCountBdd( vTruths, nVars, 0 ) : 999;
    int nNodesMin   = Abc_MinInt( Abc_MinInt(nNodesResub, nNodesBidec), Abc_MinInt(nNodesIsop, nNodesBdd) );
    if ( fVerbose )
    {
        printf( "Size = %2d  ", nVars );
        printf( "Resub =%3d  ", nNodesResub );
        printf( "Bidec =%3d  ", nNodesBidec );
        printf( "Isop =%3d  ",  nNodesIsop );
        printf( "Bdd =%3d  ",   nNodesBdd );
        Abc_TtIsfPrint( pTruth[0], pTruth[1], nTtWords ); 
        if ( nVars <= 6 )
        {
            printf( "  " );
            Extra_PrintHex( stdout, (unsigned*)pTruth[0], nVars );
            printf( "  " );
            Extra_PrintHex( stdout, (unsigned*)pTruth[1], nVars );
        }
        printf( "\n" );
    }
    Vec_WrdFree( vTruths );
    if ( nNodesMin > 500 )
        return -1;
    if ( nNodesMin == nNodesResub )
        return (nNodesMin << 2) | 0;
    if ( nNodesMin == nNodesBidec )
        return (nNodesMin << 2) | 1;
    if ( nNodesMin == nNodesIsop )
        return (nNodesMin << 2) | 2;
    if ( nNodesMin == nNodesBdd )
        return (nNodesMin << 2) | 3;
    return -1;
}
Vec_Int_t * Gia_ManDeriveSolutionOne( Gia_Man_t * p, Vec_Wrd_t * vSims, Vec_Wrd_t * vIsfs, Vec_Int_t * vCands, Vec_Int_t * vSet, int nWords, int Type )
{
    Vec_Int_t * vRes = NULL;
    Vec_Wrd_t * vTruths = Gia_ManDeriveTruths( p, vSims, vIsfs, vCands, vSet, nWords );
    int nTtWords = Vec_WrdSize(vTruths)/2, nVars = Vec_IntSize(vSet);
    word * pTruth[2] = { Vec_WrdEntryP(vTruths, 0*nTtWords), 
                         Vec_WrdEntryP(vTruths, 1*nTtWords) };
    if ( Type == 0 )
        vRes = Gia_ManDeriveResub( vTruths, nVars );
    else if ( Type == 1 )
        vRes = Gia_ManDeriveBidec( vTruths, nVars );
    else if ( Type == 2 )
        vRes = Gia_ManDeriveIsop( vTruths, nVars );
    else if ( Type == 3 )
        vRes = Gia_ManDeriveBdd( vTruths, nVars );
    if ( vRes && Gia_ResubVarNum(vRes) <= 6 )
    {
        word Func = Gia_ResubToTruth6( vRes );
        assert( !(Func &  pTruth[0][0]) );
        assert( !(pTruth[1][0] & ~Func) );   
    }
    Vec_WrdFree( vTruths );
    return vRes;
}


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


ABC_NAMESPACE_IMPL_END