summaryrefslogtreecommitdiffstats
path: root/src/opt/sbd/sbdSim.c
blob: 3bb44159b2d0ee8c4c2bad16aec5ecfc1069c713 (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
/**CFile****************************************************************

  FileName    [sbdSim.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based optimization using internal don't-cares.]

  Synopsis    [Simulation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "sbdInt.h"

ABC_NAMESPACE_IMPL_START


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

static inline word * Sbd_ObjSims( Gia_Man_t * p, int i ) { return Vec_WrdEntryP( p->vSims,   p->iPatsPi * i );  }
static inline word * Sbd_ObjCtrl( Gia_Man_t * p, int i ) { return Vec_WrdEntryP( p->vSimsPi, p->iPatsPi * i );  }

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

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

  Synopsis    [This does not work.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sbd_GiaSimRoundBack2( Gia_Man_t * p )
{
    int nWords = p->iPatsPi;
    Gia_Obj_t * pObj;
    int w, i, Id;
    // init primary outputs
    Gia_ManForEachCoId( p, Id, i )
        for ( w = 0; w < nWords; w++ )
            Sbd_ObjSims(p, Id)[w] = Gia_ManRandomW(0);
    // transfer to nodes
    Gia_ManForEachCo( p, pObj, i )
    {
        word * pSims = Sbd_ObjSims(p, Gia_ObjId(p, pObj));
        Abc_TtCopy( Sbd_ObjSims(p, Gia_ObjFaninId0p(p, pObj)), pSims, nWords, Gia_ObjFaninC0(pObj) );
    }
    // simulate nodes
    Gia_ManForEachAndReverse( p, pObj, i )
    {
        word * pSims  = Sbd_ObjSims(p, i);
        word * pSims0 = Sbd_ObjSims(p, Gia_ObjFaninId0(pObj, i));
        word * pSims1 = Sbd_ObjSims(p, Gia_ObjFaninId1(pObj, i));
        word Rand = Gia_ManRandomW(0);
        for ( w = 0; w < nWords; w++ )
        {
            pSims0[w] = pSims[w] | Rand;
            pSims1[w] = pSims[w] | ~Rand;
        }
        if ( Gia_ObjFaninC0(pObj) ) Abc_TtNot( pSims0, nWords );
        if ( Gia_ObjFaninC1(pObj) ) Abc_TtNot( pSims1, nWords );
    }
    // primary inputs are initialized
}


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

  Synopsis    [Tries to falsify a sequence of two-literal SAT problems.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sbd_GiaSatOne_rec( Gia_Man_t * p, Gia_Obj_t * pObj, int Value, int fFirst, int iPat )
{
    if ( Gia_ObjIsTravIdCurrent(p, pObj) )
        return (int)pObj->fMark0 == Value;
    Gia_ObjSetTravIdCurrent(p, pObj);
    pObj->fMark0 = Value;
    if ( Gia_ObjIsCi(pObj) )
    {
        word * pSims = Sbd_ObjSims(p, Gia_ObjId(p, pObj));
        if ( Abc_TtGetBit( pSims, iPat ) != Value )
            Abc_TtXorBit( pSims, iPat );
        return 1;
    }
    assert( Gia_ObjIsAnd(pObj) );
    assert( !Gia_ObjIsXor(pObj) );
    if ( Value )
        return Sbd_GiaSatOne_rec( p, Gia_ObjFanin0(pObj), !Gia_ObjFaninC0(pObj), fFirst, iPat ) &&
               Sbd_GiaSatOne_rec( p, Gia_ObjFanin1(pObj), !Gia_ObjFaninC1(pObj), fFirst, iPat );
    if ( fFirst )
        return Sbd_GiaSatOne_rec( p, Gia_ObjFanin0(pObj), Gia_ObjFaninC0(pObj), fFirst, iPat );
    else
        return Sbd_GiaSatOne_rec( p, Gia_ObjFanin1(pObj), Gia_ObjFaninC1(pObj), fFirst, iPat );
}
int Sbd_GiaSatOne( Gia_Man_t * p, Vec_Int_t * vPairs )
{
    int k, n, Var1, Var2, iPat = 0;
    //Gia_ManSetPhase( p );
    Vec_IntForEachEntryDouble( vPairs, Var1, Var2, k )
    {
        Gia_Obj_t * pObj1 = Gia_ManObj( p, Var1 );
        Gia_Obj_t * pObj2 = Gia_ManObj( p, Var2 );
        assert( Var2 > 0 );
        if ( Var1 == 0 )
        {
            for ( n = 0; n < 2; n++ )
            {
                Gia_ManIncrementTravId( p );
                if ( Sbd_GiaSatOne_rec(p, pObj2, !pObj2->fPhase, n, iPat) )
                {
                    iPat++;
                    break;
                }
            }
            printf( "%c", n == 2 ? '.' : 'c' );
        }
        else
        {
            for ( n = 0; n < 2; n++ )
            {
                Gia_ManIncrementTravId( p );
                if ( Sbd_GiaSatOne_rec(p, pObj1, !pObj1->fPhase, n, iPat) && Sbd_GiaSatOne_rec(p, pObj2, pObj2->fPhase, n, iPat) )
                {
                    iPat++;
                    break;
                }
                Gia_ManIncrementTravId( p );
                if ( Sbd_GiaSatOne_rec(p, pObj1, pObj1->fPhase, n, iPat) && Sbd_GiaSatOne_rec(p, pObj2, !pObj2->fPhase, n, iPat) )
                {
                    iPat++;
                    break;
                }
            }
            printf( "%c", n == 2 ? '.' : 'e' );
        }
        if ( iPat == 64 * p->iPatsPi - 1 )
            break;
    }
    printf( "\n" );
    return iPat;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sbd_GiaSimRoundBack( Gia_Man_t * p )
{
    extern void Sbd_GiaSimRound( Gia_Man_t * p, int fTry, Vec_Int_t ** pvMap );
    Vec_Int_t * vReprs = Vec_IntStart( Gia_ManObjNum(p) );
    Vec_Int_t * vPairs = Vec_IntAlloc( 1000 );
    Vec_Int_t * vMap; // maps each node into its class
    int i, nConsts = 0, nClasses = 0, nPats;
    Sbd_GiaSimRound( p, 0, &vMap );
    Gia_ManForEachAndId( p, i )
    {
        if ( Vec_IntEntry(vMap, i) == 0 )
            Vec_IntPushTwo( vPairs, 0, i ), nConsts++;
        else if ( Vec_IntEntry(vReprs, Vec_IntEntry(vMap, i)) == 0 )
            Vec_IntWriteEntry( vReprs, Vec_IntEntry(vMap, i), i );
        else if ( Vec_IntEntry(vReprs, Vec_IntEntry(vMap, i)) != -1 )
        {
            Vec_IntPushTwo( vPairs, Vec_IntEntry(vReprs, Vec_IntEntry(vMap, i)), i );
            Vec_IntWriteEntry( vReprs, Vec_IntEntry(vMap, i), -1 );
            nClasses++;
        }
    }
    Vec_IntFree( vMap );
    Vec_IntFree( vReprs );
    printf( "Constants = %d.   Classes = %d.\n", nConsts, nClasses );

    nPats = Sbd_GiaSatOne( p, vPairs );
    Vec_IntFree( vPairs );

    printf( "Generated %d patterns.\n", nPats );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sbd_GiaSimRound( Gia_Man_t * p, int fTry, Vec_Int_t ** pvMap )
{
    int nWords = p->iPatsPi;
    Vec_Mem_t * vStore;
    Gia_Obj_t * pObj;
    Vec_Int_t * vMap = Vec_IntStart( Gia_ManObjNum(p) );
    int w, i, Id, fCompl, RetValue;
    // init primary inputs
    if ( fTry )
    {
        Sbd_GiaSimRoundBack( p );
        Gia_ManForEachCiId( p, Id, i )
            Sbd_ObjSims(p, Id)[0] <<= 1;
    }
    else
    {
        Gia_ManForEachCiId( p, Id, i )
            for ( w = 0; w < nWords; w++ )
                Sbd_ObjSims(p, Id)[w] = Gia_ManRandomW(0) << !w;
    }
    // simulate internal nodes
    vStore = Vec_MemAlloc( nWords, 16 ); // 2^12 N-word entries per page
    Vec_MemHashAlloc( vStore, 1 << 16 );
    RetValue = Vec_MemHashInsert( vStore, Sbd_ObjSims(p, 0) ); // const zero
    assert( RetValue == 0 );
    Gia_ManForEachAnd( p, pObj, i )
    {
        word * pSims = Sbd_ObjSims(p, i);
        if ( Gia_ObjIsXor(pObj) )
            Abc_TtXor( pSims, 
                Sbd_ObjSims(p, Gia_ObjFaninId0(pObj, i)), 
                Sbd_ObjSims(p, Gia_ObjFaninId1(pObj, i)), 
                nWords, 
                Gia_ObjFaninC0(pObj) ^ Gia_ObjFaninC1(pObj) );
        else
            Abc_TtAndCompl( pSims, 
                Sbd_ObjSims(p, Gia_ObjFaninId0(pObj, i)), Gia_ObjFaninC0(pObj), 
                Sbd_ObjSims(p, Gia_ObjFaninId1(pObj, i)), Gia_ObjFaninC1(pObj), 
                nWords );
        // hash sim info
        fCompl = (int)(pSims[0] & 1);
        if ( fCompl ) Abc_TtNot( pSims, nWords );
        Vec_IntWriteEntry( vMap, i, Vec_MemHashInsert(vStore, pSims) );
        if ( fCompl ) Abc_TtNot( pSims, nWords );
    }
    Gia_ManForEachCo( p, pObj, i )
    {
        word * pSims = Sbd_ObjSims(p, Gia_ObjId(p, pObj));
        Abc_TtCopy( pSims, Sbd_ObjSims(p, Gia_ObjFaninId0p(p, pObj)), nWords, Gia_ObjFaninC0(pObj) );
//        printf( "%d ", Abc_TtCountOnesVec(pSims, nWords) );
        assert( Gia_ObjPhase(pObj) == (int)(pSims[0] & 1) );
    }
//    printf( "\n" );
    Vec_MemHashFree( vStore );
    Vec_MemFree( vStore );
    printf( "Objects = %6d.  Unique = %6d.\n", Gia_ManAndNum(p), Vec_IntCountUnique(vMap) );
    if ( pvMap )
        *pvMap = vMap;
    else
        Vec_IntFree( vMap );
}
void Sbd_GiaSimTest( Gia_Man_t * pGia )
{
    Gia_ManSetPhase( pGia );

    // allocate simulation info
    pGia->iPatsPi = 32;
    pGia->vSims   = Vec_WrdStart( Gia_ManObjNum(pGia) * pGia->iPatsPi );
    pGia->vSimsPi = Vec_WrdStart( Gia_ManObjNum(pGia) * pGia->iPatsPi );

    Gia_ManRandom( 1 );

    Sbd_GiaSimRound( pGia, 0, NULL );
    Sbd_GiaSimRound( pGia, 0, NULL );
    Sbd_GiaSimRound( pGia, 0, NULL );

    printf( "\n" );
    Sbd_GiaSimRound( pGia, 1, NULL );
    printf( "\n" );
    Sbd_GiaSimRound( pGia, 1, NULL );
    printf( "\n" );
    Sbd_GiaSimRound( pGia, 1, NULL );

    Vec_WrdFreeP( &pGia->vSims );
    Vec_WrdFreeP( &pGia->vSimsPi );
}

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


ABC_NAMESPACE_IMPL_END