abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
satTruth.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [satTruth.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT solver.]
8 
9  Synopsis [Truth table computation package.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: satTruth.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
18 
19 ***********************************************************************/
20 
21 #include "satTruth.h"
22 #include "misc/vec/vecSet.h"
23 
25 
26 
27 ////////////////////////////////////////////////////////////////////////
28 /// DECLARATIONS ///
29 ////////////////////////////////////////////////////////////////////////
30 
31 ////////////////////////////////////////////////////////////////////////
32 /// DECLARATIONS ///
33 ////////////////////////////////////////////////////////////////////////
34 
35 struct Tru_Man_t_
36 {
37  int nVars; // the number of variables
38  int nWords; // the number of words in the truth table
39  int nEntrySize; // the size of one entry in 'int'
40  int nTableSize; // hash table size
41  int * pTable; // hash table
42  Vec_Set_t * pMem; // memory for truth tables
43  word * pZero; // temporary truth table
44  int hIthVars[16]; // variable handles
46 };
47 
48 typedef struct Tru_One_t_ Tru_One_t; // 16 bytes minimum
49 struct Tru_One_t_
50 {
51  int Handle; // support
52  int Next; // next one in the table
53  word pTruth[0]; // truth table
54 };
55 
56 static inline Tru_One_t * Tru_ManReadOne( Tru_Man_t * p, int h ) { return h ? (Tru_One_t *)Vec_SetEntry(p->pMem, h) : NULL; }
57 
58 ////////////////////////////////////////////////////////////////////////
59 /// FUNCTION DEFINITIONS ///
60 ////////////////////////////////////////////////////////////////////////
61 
62 /**Function*************************************************************
63 
64  Synopsis [Returns the hash key.]
65 
66  Description []
67 
68  SideEffects []
69 
70  SeeAlso []
71 
72 ***********************************************************************/
73 static inline unsigned Tru_ManHash( word * pTruth, int nWords, int nBins, int * pPrimes )
74 {
75  int i;
76  unsigned uHash = 0;
77  for ( i = 0; i < nWords; i++ )
78  uHash ^= pTruth[i] * pPrimes[i & 0x7];
79  return uHash % nBins;
80 }
81 
82 /**Function*************************************************************
83 
84  Synopsis [Returns the given record.]
85 
86  Description []
87 
88  SideEffects []
89 
90  SeeAlso []
91 
92 ***********************************************************************/
93 int * Tru_ManLookup( Tru_Man_t * p, word * pTruth )
94 {
95  static int s_Primes[10] = { 1291, 1699, 2357, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
96  Tru_One_t * pEntry;
97  int * pSpot;
98  assert( (pTruth[0] & 1) == 0 );
99  pSpot = p->pTable + Tru_ManHash( pTruth, p->nWords, p->nTableSize, s_Primes );
100  for ( pEntry = Tru_ManReadOne(p, *pSpot); pEntry; pSpot = &pEntry->Next, pEntry = Tru_ManReadOne(p, *pSpot) )
101  if ( Tru_ManEqual(pEntry->pTruth, pTruth, p->nWords) )
102  return pSpot;
103  return pSpot;
104 }
105 
106 /**Function*************************************************************
107 
108  Synopsis [Returns the given record.]
109 
110  Description []
111 
112  SideEffects []
113 
114  SeeAlso []
115 
116 ***********************************************************************/
118 {
119  Tru_One_t * pThis;
120  int * pTableOld, * pSpot;
121  int nTableSizeOld, iNext, Counter, i;
122  assert( p->pTable != NULL );
123  // replace the table
124  pTableOld = p->pTable;
125  nTableSizeOld = p->nTableSize;
126  p->nTableSize = 2 * p->nTableSize + 1;
127  p->pTable = ABC_CALLOC( int, p->nTableSize );
128  // rehash the entries from the old table
129  Counter = 0;
130  for ( i = 0; i < nTableSizeOld; i++ )
131  for ( pThis = Tru_ManReadOne(p, pTableOld[i]),
132  iNext = (pThis? pThis->Next : 0);
133  pThis; pThis = Tru_ManReadOne(p, iNext),
134  iNext = (pThis? pThis->Next : 0) )
135  {
136  assert( pThis->Handle );
137  pThis->Next = 0;
138  pSpot = Tru_ManLookup( p, pThis->pTruth );
139  assert( *pSpot == 0 ); // should not be there
140  *pSpot = pThis->Handle;
141  Counter++;
142  }
143  assert( Counter == Vec_SetEntryNum(p->pMem) );
144  ABC_FREE( pTableOld );
145 }
146 
147 /**Function*************************************************************
148 
149  Synopsis [Adds entry to the hash table.]
150 
151  Description []
152 
153  SideEffects []
154 
155  SeeAlso []
156 
157 ***********************************************************************/
158 int Tru_ManInsert( Tru_Man_t * p, word * pTruth )
159 {
160  int fCompl, * pSpot;
161  if ( Tru_ManEqual0(pTruth, p->nWords) )
162  return 0;
163  if ( Tru_ManEqual1(pTruth, p->nWords) )
164  return 1;
165  p->nTableLookups++;
166  if ( Vec_SetEntryNum(p->pMem) > 2 * p->nTableSize )
167  Tru_ManResize( p );
168  fCompl = pTruth[0] & 1;
169  if ( fCompl )
170  Tru_ManNot( pTruth, p->nWords );
171  pSpot = Tru_ManLookup( p, pTruth );
172  if ( *pSpot == 0 )
173  {
174  Tru_One_t * pEntry;
175  *pSpot = Vec_SetAppend( p->pMem, NULL, p->nEntrySize );
176  assert( (*pSpot & 1) == 0 );
177  pEntry = Tru_ManReadOne( p, *pSpot );
178  Tru_ManCopy( pEntry->pTruth, pTruth, p->nWords );
179  pEntry->Handle = *pSpot;
180  pEntry->Next = 0;
181  }
182  if ( fCompl )
183  Tru_ManNot( pTruth, p->nWords );
184  return *pSpot ^ fCompl;
185 }
186 
187 /**Function*************************************************************
188 
189  Synopsis [Start the truth table logging.]
190 
191  Description []
192 
193  SideEffects []
194 
195  SeeAlso []
196 
197 ***********************************************************************/
198 Tru_Man_t * Tru_ManAlloc( int nVars )
199 {
200  word Masks[6] =
201  {
202  ABC_CONST(0xAAAAAAAAAAAAAAAA),
203  ABC_CONST(0xCCCCCCCCCCCCCCCC),
204  ABC_CONST(0xF0F0F0F0F0F0F0F0),
205  ABC_CONST(0xFF00FF00FF00FF00),
206  ABC_CONST(0xFFFF0000FFFF0000),
207  ABC_CONST(0xFFFFFFFF00000000)
208  };
209  Tru_Man_t * p;
210  int i, w;
211  assert( nVars > 0 && nVars <= 16 );
212  p = ABC_CALLOC( Tru_Man_t, 1 );
213  p->nVars = nVars;
214  p->nWords = (nVars < 6) ? 1 : (1 << (nVars-6));
215  p->nEntrySize = (sizeof(Tru_One_t) + p->nWords * sizeof(word))/sizeof(int);
216  p->nTableSize = 8147;
217  p->pTable = ABC_CALLOC( int, p->nTableSize );
218  p->pMem = Vec_SetAlloc( 16 );
219  // initialize truth tables
220  p->pZero = ABC_ALLOC( word, p->nWords );
221  for ( i = 0; i < nVars; i++ )
222  {
223  for ( w = 0; w < p->nWords; w++ )
224  if ( i < 6 )
225  p->pZero[w] = Masks[i];
226  else if ( w & (1 << (i-6)) )
227  p->pZero[w] = ~(word)0;
228  else
229  p->pZero[w] = 0;
230  p->hIthVars[i] = Tru_ManInsert( p, p->pZero );
231  assert( !i || p->hIthVars[i] > p->hIthVars[i-1] );
232  }
233  Tru_ManClear( p->pZero, p->nWords );
234  return p;
235 }
236 
237 /**Function*************************************************************
238 
239  Synopsis [Stop the truth table logging.]
240 
241  Description []
242 
243  SideEffects []
244 
245  SeeAlso []
246 
247 ***********************************************************************/
249 {
250  printf( "Lookups = %d. Entries = %d.\n", p->nTableLookups, Vec_SetEntryNum(p->pMem) );
251  Vec_SetFree( p->pMem );
252  ABC_FREE( p->pZero );
253  ABC_FREE( p->pTable );
254  ABC_FREE( p );
255 }
256 
257 /**Function*************************************************************
258 
259  Synopsis [Returns elementary variable.]
260 
261  Description []
262 
263  SideEffects []
264 
265  SeeAlso []
266 
267 ***********************************************************************/
268 word * Tru_ManVar( Tru_Man_t * p, int v )
269 {
270  assert( v >= 0 && v < p->nVars );
271  return Tru_ManReadOne( p, p->hIthVars[v] )->pTruth;
272 }
273 
274 /**Function*************************************************************
275 
276  Synopsis [Returns stored truth table]
277 
278  Description []
279 
280  SideEffects []
281 
282  SeeAlso []
283 
284 ***********************************************************************/
286 {
287  assert( (h & 1) == 0 );
288  if ( h == 0 )
289  return p->pZero;
290  return Tru_ManReadOne( p, h )->pTruth;
291 }
292 
293 ////////////////////////////////////////////////////////////////////////
294 /// END OF FILE ///
295 ////////////////////////////////////////////////////////////////////////
296 
297 
299 
static word * Tru_ManNot(word *pOut, int nWords)
Definition: satTruth.h:60
void Tru_ManFree(Tru_Man_t *p)
Definition: satTruth.c:248
Tru_Man_t * Tru_ManAlloc(int nVars)
FUNCTION DECLARATIONS ///.
Definition: satTruth.c:198
Vec_Set_t * pMem
Definition: satTruth.c:42
static void Vec_SetFree(Vec_Set_t *p)
Definition: vecSet.h:176
DECLARATIONS ///.
Definition: satTruth.c:35
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static Vec_Set_t * Vec_SetAlloc(int nPageSize)
Definition: vecSet.h:128
int nVars
Definition: satTruth.c:37
int * pTable
Definition: satTruth.c:41
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static word * Vec_SetEntry(Vec_Set_t *p, int h)
Definition: vecSet.h:70
void Tru_ManResize(Tru_Man_t *p)
Definition: satTruth.c:117
static int Vec_SetAppend(Vec_Set_t *p, int *pArray, int nSize)
Definition: vecSet.h:213
word pTruth[0]
Definition: satTruth.c:53
int nWords
Definition: abcNpn.c:127
static int Vec_SetEntryNum(Vec_Set_t *p)
Definition: vecSet.h:71
word * pZero
Definition: satTruth.c:43
static int s_Primes[MAX_PRIMES]
Definition: fxuPair.c:30
struct Tru_One_t_ Tru_One_t
Definition: satTruth.c:48
int nTableLookups
Definition: satTruth.c:45
static int Tru_ManEqual0(word *pOut, int nWords)
Definition: satTruth.h:55
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
int nTableSize
Definition: satTruth.c:40
static Tru_One_t * Tru_ManReadOne(Tru_Man_t *p, int h)
Definition: satTruth.c:56
int * Tru_ManLookup(Tru_Man_t *p, word *pTruth)
Definition: satTruth.c:93
static int Counter
typedefABC_NAMESPACE_HEADER_START struct Tru_Man_t_ Tru_Man_t
INCLUDES ///.
Definition: satTruth.h:44
static word * Tru_ManCopy(word *pOut, word *pIn, int nWords)
Definition: satTruth.h:57
word * Tru_ManFunc(Tru_Man_t *p, int h)
Definition: satTruth.c:285
int Tru_ManInsert(Tru_Man_t *p, word *pTruth)
Definition: satTruth.c:158
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static int Tru_ManEqual1(word *pOut, int nWords)
Definition: satTruth.h:56
typedefABC_NAMESPACE_HEADER_START struct Vec_Set_t_ Vec_Set_t
INCLUDES ///.
Definition: vecSet.h:49
static word * Tru_ManClear(word *pOut, int nWords)
Definition: satTruth.h:58
static unsigned Tru_ManHash(word *pTruth, int nWords, int nBins, int *pPrimes)
FUNCTION DEFINITIONS ///.
Definition: satTruth.c:73
#define ABC_CONST(number)
PARAMETERS ///.
Definition: abc_global.h:206
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
int Next
Definition: satTruth.c:52
int Handle
Definition: satTruth.c:51
int hIthVars[16]
Definition: satTruth.c:44
#define assert(ex)
Definition: util_old.h:213
int nWords
Definition: satTruth.c:38
static int Tru_ManEqual(word *pOut, word *pIn, int nWords)
GLOBAL VARIABLES ///.
Definition: satTruth.h:54
int nEntrySize
Definition: satTruth.c:39
word * Tru_ManVar(Tru_Man_t *p, int v)
Definition: satTruth.c:268