summaryrefslogtreecommitdiffstats
path: root/src/opt/cut/cutTable.c
blob: 5dfaca7bd798a5b623e91669d9e64e9392dd4dd3 (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
/**CFile****************************************************************

  FileName    [cutTable.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [K-feasible cut computation package.]

  Synopsis    [Hashing cuts to prevent duplication.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "cutInt.h"

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

struct Cut_HashTableStruct_t_
{
    int                  nBins;
    Cut_Cut_t **         pBins;
    int                  nEntries;
    int *                pPlaces;
    int                  nPlaces;
    int                  timeLookup;
};

// iterator through all the cuts of the list
#define Cut_TableListForEachCut( pList, pCut )               \
    for ( pCut = pList;                                      \
          pCut;                                              \
          pCut = pCut->pData )
#define Cut_TableListForEachCutSafe( pList, pCut, pCut2 )    \
    for ( pCut = pList,                                      \
          pCut2 = pCut? pCut->pData: NULL;                   \
          pCut;                                              \
          pCut = pCut2,                                      \
          pCut2 = pCut? pCut->pData: NULL )

// primes used to compute the hash key
static int s_HashPrimes[10] = { 109, 499, 557, 619, 631, 709, 797, 881, 907, 991 };

// hashing function
static inline unsigned Cut_HashKey( Cut_Cut_t * pCut )
{
    unsigned i, uRes = pCut->nLeaves * s_HashPrimes[9];
    for ( i = 0; i < pCut->nLeaves + pCut->fSeq; i++ )
        uRes += s_HashPrimes[i] * pCut->pLeaves[i];
    return uRes;
}

// hashing function
static inline int Cut_CompareTwo( Cut_Cut_t * pCut1, Cut_Cut_t * pCut2 )
{
    unsigned i;
    if ( pCut1->nLeaves != pCut2->nLeaves )
        return 1;
    for ( i = 0; i < pCut1->nLeaves; i++ )
        if ( pCut1->pLeaves[i] != pCut2->pLeaves[i] )
            return 1;
    return 0;
}

static void Cut_TableResize( Cut_HashTable_t * pTable );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Starts the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cut_HashTable_t * Cut_TableStart( int Size )
{
    Cut_HashTable_t * pTable;
    pTable = ALLOC( Cut_HashTable_t, 1 );
    memset( pTable, 0, sizeof(Cut_HashTable_t) );
    // allocate the table
    pTable->nBins = Cudd_PrimeCopy( Size );
    pTable->pBins = ALLOC( Cut_Cut_t *, pTable->nBins );
    memset( pTable->pBins, 0, sizeof(Cut_Cut_t *) * pTable->nBins );
    pTable->pPlaces = ALLOC( int, pTable->nBins );
    return pTable;
}

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

  Synopsis    [Stops the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cut_TableStop( Cut_HashTable_t * pTable )
{
    FREE( pTable->pPlaces );
    free( pTable->pBins );
    free( pTable );
}

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

  Synopsis    [Check the existence of a cut in the lookup table]

  Description [Returns 1 if the entry is found.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cut_TableLookup( Cut_HashTable_t * pTable, Cut_Cut_t * pCut, int fStore )
{
    Cut_Cut_t * pEnt;
    unsigned Key;
    int clk = clock();

    Key = Cut_HashKey(pCut) % pTable->nBins; 
    Cut_TableListForEachCut( pTable->pBins[Key], pEnt )
    {
        if ( !Cut_CompareTwo( pEnt, pCut ) )
        {
pTable->timeLookup += clock() - clk;
            return 1;
        }
    }
    if ( pTable->nEntries > 2 * pTable->nBins )
    {
        Cut_TableResize( pTable );
        Key = Cut_HashKey(pCut) % pTable->nBins; 
    }
    // remember the place
    if ( fStore && pTable->pBins[Key] == NULL )
        pTable->pPlaces[ pTable->nPlaces++ ] = Key;
    // add the cut to the table
    pCut->pData = pTable->pBins[Key];
    pTable->pBins[Key] = pCut;
    pTable->nEntries++;
pTable->timeLookup += clock() - clk;
    return 0;
}


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

  Synopsis    [Stops the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cut_TableClear( Cut_HashTable_t * pTable )
{
    int i;
    assert( pTable->nPlaces <= pTable->nBins );
    for ( i = 0; i < pTable->nPlaces; i++ )
    {
        assert( pTable->pBins[ pTable->pPlaces[i] ] );
        pTable->pBins[ pTable->pPlaces[i] ] = NULL;
    }
    pTable->nPlaces = 0;
    pTable->nEntries = 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cut_TableResize( Cut_HashTable_t * pTable )
{
    Cut_Cut_t ** pBinsNew;
    Cut_Cut_t * pEnt, * pEnt2;
    int nBinsNew, Counter, i, clk;
    unsigned Key;

clk = clock();
    // get the new table size
    nBinsNew = Cudd_PrimeCopy( 3 * pTable->nBins ); 
    // allocate a new array
    pBinsNew = ALLOC( Cut_Cut_t *, nBinsNew );
    memset( pBinsNew, 0, sizeof(Cut_Cut_t *) * nBinsNew );
    // rehash the entries from the old table
    Counter = 0;
    for ( i = 0; i < pTable->nBins; i++ )
        Cut_TableListForEachCutSafe( pTable->pBins[i], pEnt, pEnt2 )
        {
            Key = Cut_HashKey(pEnt) % nBinsNew;
            pEnt->pData   = pBinsNew[Key];
            pBinsNew[Key] = pEnt;
            Counter++;
        }
    assert( Counter == 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( pTable->pBins );
    pTable->pBins = pBinsNew;
    pTable->nBins = nBinsNew;
}

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

  Synopsis    [Stops the hash table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cut_TableReadTime( Cut_HashTable_t * pTable )
{
    if ( pTable == NULL )
        return 0;
    return pTable->timeLookup;
}

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