summaryrefslogtreecommitdiffstats
path: root/src/sat/aig/aigTable.c
blob: e6fe87d6689294a5836e00397ddce788c911e726 (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
/**CFile****************************************************************

  FileName    [aigTable.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [And-Inverter Graph package.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "aig.h"

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

// the hash table 
struct Aig_Table_t_
{
    Aig_Node_t **    pBins;         // the table bins
    int              nBins;         // the size of the table
    int              nEntries;      // the total number of entries in the table
};

// iterators through the entries in the linked lists of nodes
#define Aig_TableBinForEachEntry( pBin, pEnt )                 \
    for ( pEnt = pBin;                                         \
          pEnt;                                                \
          pEnt = Aig_NodeNextH(pEnt) )
#define Aig_TableBinForEachEntrySafe( pBin, pEnt, pEnt2 )      \
    for ( pEnt = pBin,                                         \
          pEnt2 = pEnt? Aig_NodeNextH(pEnt) : NULL;            \
          pEnt;                                                \
          pEnt = pEnt2,                                        \
          pEnt2 = pEnt? Aig_NodeNextH(pEnt) : NULL )

// hash key for the structural hash table
static inline unsigned Abc_HashKey2( Aig_Node_t * p0, Aig_Node_t * p1, int TableSize ) { return ((unsigned)(p0) + (unsigned)(p1) * 12582917) % TableSize; }
//static inline unsigned Abc_HashKey2( Aig_Node_t * p0, Aig_Node_t * p1, int TableSize ) { return ((unsigned)((a)->Id + (b)->Id) * ((a)->Id + (b)->Id + 1) / 2) % TableSize; }

static unsigned int Cudd_PrimeAig( unsigned int  p );

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

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

  Synopsis    [Allocates the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Table_t * Aig_TableCreate( int nSize )
{
    Aig_Table_t * p;
    // allocate the table
    p = ALLOC( Aig_Table_t, 1 );
    memset( p, 0, sizeof(Aig_Table_t) );
    // allocate and clean the bins
    p->nBins = Cudd_PrimeAig(nSize);
    p->pBins = ALLOC( Aig_Node_t *, p->nBins );
    memset( p->pBins, 0, sizeof(Aig_Node_t *) * p->nBins );
    return p;
}

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

  Synopsis    [Deallocates the supergate hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_TableFree( Aig_Table_t * p )
{
    FREE( p->pBins );
    FREE( p );
}

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

  Synopsis    [Returns the number of nodes in the table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Aig_TableNumNodes( Aig_Table_t * p )
{
    return p->nEntries;
}

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

  Synopsis    [Performs canonicization step.]

  Description [The argument nodes can be complemented.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Node_t * Aig_TableLookupNode( Aig_Man_t * pMan, Aig_Node_t * p0, Aig_Node_t * p1 )
{
    Aig_Node_t * pAnd;
    unsigned Key;
    assert( Aig_Regular(p0)->Id < Aig_Regular(p1)->Id );
    // get the hash key for these two nodes
    Key = Abc_HashKey2( p0, p1, pMan->pTable->nBins ); 
    // find the matching node in the table
    Aig_TableBinForEachEntry( pMan->pTable->pBins[Key], pAnd )
        if ( p0 == Aig_NodeChild0(pAnd) && p1 == Aig_NodeChild1(pAnd) )
             return pAnd;
    return NULL;
}

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

  Synopsis    [Performs canonicization step.]

  Description [The argument nodes can be complemented.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Node_t * Aig_TableInsertNode( Aig_Man_t * pMan, Aig_Node_t * p0, Aig_Node_t * p1, Aig_Node_t * pAnd )
{
    unsigned Key;
    assert( Aig_Regular(p0)->Id < Aig_Regular(p1)->Id );
    // check if it is a good time for table resizing
    if ( pMan->pTable->nEntries > 2 * pMan->pTable->nBins )
        Aig_TableResize( pMan );
    // add the node to the corresponding linked list in the table
    Key = Abc_HashKey2( p0, p1, pMan->pTable->nBins );
    pAnd->NextH = pMan->pTable->pBins[Key]? pMan->pTable->pBins[Key]->Id : 0;
    pMan->pTable->pBins[Key] = pAnd;
    pMan->pTable->nEntries++;
    return pAnd;
}


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

  Synopsis    [Deletes an AIG node from the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_TableDeleteNode( Aig_Man_t * pMan, Aig_Node_t * pThis )
{
    Aig_Node_t * pAnd, * pPlace = NULL;
    unsigned Key;
    assert( !Aig_IsComplement(pThis) );
    assert( Aig_NodeIsAnd(pThis) );
    assert( pMan == pThis->pMan );
    // get the hash key for these two nodes
    Key = Abc_HashKey2( Aig_NodeChild0(pThis), Aig_NodeChild1(pThis), pMan->pTable->nBins );
    // find the matching node in the table
    Aig_TableBinForEachEntry( pMan->pTable->pBins[Key], pAnd )
    {
        if ( pThis != pAnd )
        {
            pPlace = pAnd;
            continue;
        }
        if ( pPlace == NULL )
            pMan->pTable->pBins[Key] = Aig_NodeNextH(pThis);
        else
            pPlace->NextH = pThis->NextH;
        break;
    }
    assert( pThis == pAnd );
    pMan->pTable->nEntries--;
}

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

  Synopsis    [Resizes the hash table of AIG nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_TableResize( Aig_Man_t * pMan )
{
    Aig_Node_t ** pBinsNew;
    Aig_Node_t * pEnt, * pEnt2;
    int nBinsNew, Counter, i, clk;
    unsigned Key;

clk = clock();
    // get the new table size
    nBinsNew = Cudd_PrimeCopy( 3 * pMan->pTable->nBins ); 
    // allocate a new array
    pBinsNew = ALLOC( Aig_Node_t *, nBinsNew );
    memset( pBinsNew, 0, sizeof(Aig_Node_t *) * nBinsNew );
    // rehash the entries from the old table
    Counter = 0;
    for ( i = 0; i < pMan->pTable->nBins; i++ )
        Aig_TableBinForEachEntrySafe( pMan->pTable->pBins[i], pEnt, pEnt2 )
        {
            Key = Abc_HashKey2( Aig_NodeChild0(pEnt), Aig_NodeChild1(pEnt), nBinsNew );
            pEnt->NextH = pBinsNew[Key]? pBinsNew[Key]->Id : 0;
            pBinsNew[Key] = pEnt;
            Counter++;
        }
    assert( Counter == pMan->pTable->nEntries );
//    printf( "Increasing the structural table size from %6d to %6d. ", pMan->nBins, nBinsNew );
//    PRT( "Time", clock() - clk );
    // replace the table and the parameters
    free( pMan->pTable->pBins );
    pMan->pTable->pBins = pBinsNew;
    pMan->pTable->nBins = nBinsNew;
}

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

  Synopsis    [Resizes the hash table of AIG nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Aig_TableRehash( Aig_Man_t * pMan )
{
    Aig_Node_t ** pBinsNew;
    Aig_Node_t * pEnt, * pEnt2;
    unsigned Key;
    int Counter, Temp, i;
    // allocate a new array
    pBinsNew = ALLOC( Aig_Node_t *, pMan->pTable->nBins );
    memset( pBinsNew, 0, sizeof(Aig_Node_t *) * pMan->pTable->nBins );
    // rehash the entries from the old table
    Counter = 0;
    for ( i = 0; i < pMan->pTable->nBins; i++ )
        Aig_TableBinForEachEntrySafe( pMan->pTable->pBins[i], pEnt, pEnt2 )
        {
            // swap the fanins if needed
            if ( pEnt->Fans[0].iNode > pEnt->Fans[1].iNode )
            {
                Temp = pEnt->Fans[0].iNode;
                pEnt->Fans[0].iNode = pEnt->Fans[1].iNode;
                pEnt->Fans[1].iNode = Temp;
                Temp = pEnt->Fans[0].fComp;
                pEnt->Fans[0].fComp = pEnt->Fans[1].fComp;
                pEnt->Fans[1].fComp = Temp;
           }
            // rehash the node
            Key = Abc_HashKey2( Aig_NodeChild0(pEnt), Aig_NodeChild1(pEnt), pMan->pTable->nBins );
            pEnt->NextH = pBinsNew[Key]? pBinsNew[Key]->Id : 0;
            pBinsNew[Key] = pEnt;
            Counter++;
        }
    assert( Counter == pMan->pTable->nEntries );
    // replace the table and the parameters
    free( pMan->pTable->pBins );
    pMan->pTable->pBins = pBinsNew;
}

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

  Synopsis    [Returns the smallest prime larger than the number.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned int Cudd_PrimeAig( unsigned int p )
{
    int i,pn;

    p--;
    do {
        p++;
        if (p&1) {
        pn = 1;
        i = 3;
        while ((unsigned) (i * i) <= p) {
        if (p % i == 0) {
            pn = 0;
            break;
        }
        i += 2;
        }
    } else {
        pn = 0;
    }
    } while (!pn);
    return(p);

} /* end of Cudd_Prime */

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