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
|
/**CFile****************************************************************
FileName [satTruth.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [SAT solver.]
Synopsis [Truth table computation package.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: satTruth.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
***********************************************************************/
#include "satTruth.h"
#include "misc/vec/vecSet.h"
ABC_NAMESPACE_IMPL_START
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
struct Tru_Man_t_
{
int nVars; // the number of variables
int nWords; // the number of words in the truth table
int nEntrySize; // the size of one entry in 'int'
int nTableSize; // hash table size
int * pTable; // hash table
Vec_Set_t * pMem; // memory for truth tables
word * pZero; // temporary truth table
int hIthVars[16]; // variable handles
int nTableLookups;
};
typedef struct Tru_One_t_ Tru_One_t; // 16 bytes minimum
struct Tru_One_t_
{
int Handle; // support
int Next; // next one in the table
word pTruth[0]; // truth table
};
static inline Tru_One_t * Tru_ManReadOne( Tru_Man_t * p, int h ) { return h ? (Tru_One_t *)Vec_SetEntry(p->pMem, h) : NULL; }
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Returns the hash key.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline unsigned Tru_ManHash( word * pTruth, int nWords, int nBins, int * pPrimes )
{
int i;
unsigned uHash = 0;
for ( i = 0; i < nWords; i++ )
uHash ^= pTruth[i] * pPrimes[i & 0x7];
return uHash % nBins;
}
/**Function*************************************************************
Synopsis [Returns the given record.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int * Tru_ManLookup( Tru_Man_t * p, word * pTruth )
{
static int s_Primes[10] = { 1291, 1699, 2357, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
Tru_One_t * pEntry;
int * pSpot;
assert( (pTruth[0] & 1) == 0 );
pSpot = p->pTable + Tru_ManHash( pTruth, p->nWords, p->nTableSize, s_Primes );
for ( pEntry = Tru_ManReadOne(p, *pSpot); pEntry; pSpot = &pEntry->Next, pEntry = Tru_ManReadOne(p, *pSpot) )
if ( Tru_ManEqual(pEntry->pTruth, pTruth, p->nWords) )
return pSpot;
return pSpot;
}
/**Function*************************************************************
Synopsis [Returns the given record.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Tru_ManResize( Tru_Man_t * p )
{
Tru_One_t * pThis;
int * pTableOld, * pSpot;
int nTableSizeOld, iNext, Counter, i;
assert( p->pTable != NULL );
// replace the table
pTableOld = p->pTable;
nTableSizeOld = p->nTableSize;
p->nTableSize = 2 * p->nTableSize + 1;
p->pTable = ABC_CALLOC( int, p->nTableSize );
// rehash the entries from the old table
Counter = 0;
for ( i = 0; i < nTableSizeOld; i++ )
for ( pThis = Tru_ManReadOne(p, pTableOld[i]),
iNext = (pThis? pThis->Next : 0);
pThis; pThis = Tru_ManReadOne(p, iNext),
iNext = (pThis? pThis->Next : 0) )
{
assert( pThis->Handle );
pThis->Next = 0;
pSpot = Tru_ManLookup( p, pThis->pTruth );
assert( *pSpot == 0 ); // should not be there
*pSpot = pThis->Handle;
Counter++;
}
assert( Counter == Vec_SetEntryNum(p->pMem) );
ABC_FREE( pTableOld );
}
/**Function*************************************************************
Synopsis [Adds entry to the hash table.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Tru_ManInsert( Tru_Man_t * p, word * pTruth )
{
int fCompl, * pSpot;
if ( Tru_ManEqual0(pTruth, p->nWords) )
return 0;
if ( Tru_ManEqual1(pTruth, p->nWords) )
return 1;
p->nTableLookups++;
if ( Vec_SetEntryNum(p->pMem) > 2 * p->nTableSize )
Tru_ManResize( p );
fCompl = pTruth[0] & 1;
if ( fCompl )
Tru_ManNot( pTruth, p->nWords );
pSpot = Tru_ManLookup( p, pTruth );
if ( *pSpot == 0 )
{
Tru_One_t * pEntry;
*pSpot = Vec_SetAppend( p->pMem, NULL, p->nEntrySize );
assert( (*pSpot & 1) == 0 );
pEntry = Tru_ManReadOne( p, *pSpot );
Tru_ManCopy( pEntry->pTruth, pTruth, p->nWords );
pEntry->Handle = *pSpot;
pEntry->Next = 0;
}
if ( fCompl )
Tru_ManNot( pTruth, p->nWords );
return *pSpot ^ fCompl;
}
/**Function*************************************************************
Synopsis [Start the truth table logging.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Tru_Man_t * Tru_ManAlloc( int nVars )
{
word Masks[6] =
{
ABC_CONST(0xAAAAAAAAAAAAAAAA),
ABC_CONST(0xCCCCCCCCCCCCCCCC),
ABC_CONST(0xF0F0F0F0F0F0F0F0),
ABC_CONST(0xFF00FF00FF00FF00),
ABC_CONST(0xFFFF0000FFFF0000),
ABC_CONST(0xFFFFFFFF00000000)
};
Tru_Man_t * p;
int i, w;
assert( nVars > 0 && nVars <= 16 );
p = ABC_CALLOC( Tru_Man_t, 1 );
p->nVars = nVars;
p->nWords = (nVars < 6) ? 1 : (1 << (nVars-6));
p->nEntrySize = (sizeof(Tru_One_t) + p->nWords * sizeof(word))/sizeof(int);
p->nTableSize = 8147;
p->pTable = ABC_CALLOC( int, p->nTableSize );
p->pMem = Vec_SetAlloc( 16 );
// initialize truth tables
p->pZero = ABC_ALLOC( word, p->nWords );
for ( i = 0; i < nVars; i++ )
{
for ( w = 0; w < p->nWords; w++ )
if ( i < 6 )
p->pZero[w] = Masks[i];
else if ( w & (1 << (i-6)) )
p->pZero[w] = ~(word)0;
else
p->pZero[w] = 0;
p->hIthVars[i] = Tru_ManInsert( p, p->pZero );
assert( !i || p->hIthVars[i] > p->hIthVars[i-1] );
}
Tru_ManClear( p->pZero, p->nWords );
return p;
}
/**Function*************************************************************
Synopsis [Stop the truth table logging.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Tru_ManFree( Tru_Man_t * p )
{
printf( "Lookups = %d. Entries = %d.\n", p->nTableLookups, Vec_SetEntryNum(p->pMem) );
Vec_SetFree( p->pMem );
ABC_FREE( p->pZero );
ABC_FREE( p->pTable );
ABC_FREE( p );
}
/**Function*************************************************************
Synopsis [Returns elementary variable.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
word * Tru_ManVar( Tru_Man_t * p, int v )
{
assert( v >= 0 && v < p->nVars );
return Tru_ManReadOne( p, p->hIthVars[v] )->pTruth;
}
/**Function*************************************************************
Synopsis [Returns stored truth table]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
word * Tru_ManFunc( Tru_Man_t * p, int h )
{
assert( (h & 1) == 0 );
if ( h == 0 )
return p->pZero;
return Tru_ManReadOne( p, h )->pTruth;
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|