abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
vecMem.h
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [vecMem.h]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Resizable arrays.]
8 
9  Synopsis [Resizable array of memory pieces.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - July 20, 2012.]
16 
17  Revision [$Id: vecMem.h,v 1.00 2012/07/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__misc__vec__vecMem_h
22 #define ABC__misc__vec__vecMem_h
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 /// INCLUDES ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 #include <stdio.h>
30 
32 
33 /*
34  This vector stores pieces of memory of the given size.
35  It is useful for representing truth tables and any other objects
36  of the fixed size. It is better that Extra_MmFixed because the
37  entry IDs can be used as handles to retrieve memory pieces without
38  the need for an array of pointers from entry IDs into memory pieces
39  (this can save 8(4) bytes per object on a 64(32)-bit platform).
40 */
41 
42 ////////////////////////////////////////////////////////////////////////
43 /// PARAMETERS ///
44 ////////////////////////////////////////////////////////////////////////
45 
46 ////////////////////////////////////////////////////////////////////////
47 /// BASIC TYPES ///
48 ////////////////////////////////////////////////////////////////////////
49 
50 typedef struct Vec_Mem_t_ Vec_Mem_t;
51 struct Vec_Mem_t_
52 {
53  int nEntrySize; // entry size (in terms of 8-byte words)
54  int nEntries; // number of entries currently used
55  int LogPageSze; // log2 of page size (in terms of entries)
56  int PageMask; // page mask
57  int nPageAlloc; // number of pages currently allocated
58  int iPage; // the number of a page currently used
59  word ** ppPages; // memory pages
60  Vec_Int_t * vTable; // hash table
61  Vec_Int_t * vNexts; // next pointers
62 };
63 
64 ////////////////////////////////////////////////////////////////////////
65 /// MACRO DEFINITIONS ///
66 ////////////////////////////////////////////////////////////////////////
67 
68 #define Vec_MemForEachEntry( p, pEntry, i ) \
69  for ( i = 0; (i < Vec_MemEntryNum(p)) && ((pEntry) = Vec_MemReadEntry(p, i)); i++ )
70 
71 ////////////////////////////////////////////////////////////////////////
72 /// FUNCTION DEFINITIONS ///
73 ////////////////////////////////////////////////////////////////////////
74 
75 /**Function*************************************************************
76 
77  Synopsis [Allocates a memory vector.]
78 
79  Description [Entry size is in terms of 8-byte words. Page size is log2
80  of the number of entries on one page.]
81 
82  SideEffects []
83 
84  SeeAlso []
85 
86 ***********************************************************************/
87 static inline Vec_Mem_t * Vec_MemAlloc( int nEntrySize, int LogPageSze )
88 {
89  Vec_Mem_t * p;
90  p = ABC_CALLOC( Vec_Mem_t, 1 );
91  p->nEntrySize = nEntrySize;
92  p->LogPageSze = LogPageSze;
93  p->PageMask = (1 << p->LogPageSze) - 1;
94  p->iPage = -1;
95  return p;
96 }
97 static inline void Vec_MemFree( Vec_Mem_t * p )
98 {
99  int i;
100  for ( i = 0; i <= p->iPage; i++ )
101  ABC_FREE( p->ppPages[i] );
102  ABC_FREE( p->ppPages );
103  ABC_FREE( p );
104 }
105 static inline void Vec_MemFreeP( Vec_Mem_t ** p )
106 {
107  if ( *p == NULL )
108  return;
109  Vec_MemFree( *p );
110  *p = NULL;
111 }
112 static inline Vec_Mem_t * Vec_MemDup( Vec_Mem_t * pVec )
113 {
114  Vec_Mem_t * p = NULL;
115  return p;
116 }
117 
118 /**Function*************************************************************
119 
120  Synopsis [Duplicates the integer array.]
121 
122  Description []
123 
124  SideEffects []
125 
126  SeeAlso []
127 
128 ***********************************************************************/
129 static inline void Vec_MemFill( Vec_Mem_t * pVec, int nEntries )
130 {
131 }
132 static inline void Vec_MemClean( Vec_Mem_t * pVec, int nEntries )
133 {
134 }
135 
136 /**Function*************************************************************
137 
138  Synopsis []
139 
140  Description []
141 
142  SideEffects []
143 
144  SeeAlso []
145 
146 ***********************************************************************/
147 static inline int Vec_MemEntrySize( Vec_Mem_t * p )
148 {
149  return p->nEntrySize;
150 }
151 static inline int Vec_MemEntryNum( Vec_Mem_t * p )
152 {
153  return p->nEntries;
154 }
155 static inline int Vec_MemPageSize( Vec_Mem_t * p )
156 {
157  return p->LogPageSze;
158 }
159 static inline int Vec_MemPageNum( Vec_Mem_t * p )
160 {
161  return p->iPage+1;
162 }
163 
164 /**Function*************************************************************
165 
166  Synopsis []
167 
168  Description []
169 
170  SideEffects []
171 
172  SeeAlso []
173 
174 ***********************************************************************/
175 static inline double Vec_MemMemory( Vec_Mem_t * p )
176 {
177  return (double)sizeof(word) * p->nEntrySize * (1 << p->LogPageSze) * (p->iPage + 1) + (double)sizeof(word *) * p->nPageAlloc + (double)sizeof(Vec_Mem_t);
178 }
179 
180 /**Function*************************************************************
181 
182  Synopsis []
183 
184  Description []
185 
186  SideEffects []
187 
188  SeeAlso []
189 
190 ***********************************************************************/
191 static inline word * Vec_MemReadEntry( Vec_Mem_t * p, int i )
192 {
193  assert( i >= 0 && i < p->nEntries );
194  return p->ppPages[i >> p->LogPageSze] + p->nEntrySize * (i & p->PageMask);
195 }
196 static inline word * Vec_MemReadEntryLast( Vec_Mem_t * p )
197 {
198  assert( p->nEntries > 0 );
199  return Vec_MemReadEntry( p, p->nEntries-1 );
200 }
201 static inline void Vec_MemWriteEntry( Vec_Mem_t * p, int i, word * pEntry )
202 {
203  word * pPlace = Vec_MemReadEntry( p, i );
204  memmove( pPlace, pEntry, sizeof(word) * p->nEntrySize );
205 }
206 static inline word * Vec_MemGetEntry( Vec_Mem_t * p, int i )
207 {
208  assert( i >= 0 );
209  if ( i >= p->nEntries )
210  {
211  int k, iPageNew = (i >> p->LogPageSze);
212  if ( p->iPage < iPageNew )
213  {
214  // realloc page pointers if needed
215  if ( iPageNew >= p->nPageAlloc )
216  p->ppPages = ABC_REALLOC( word *, p->ppPages, (p->nPageAlloc = p->nPageAlloc ? 2 * p->nPageAlloc : iPageNew + 32) );
217  // allocate new pages if needed
218  for ( k = p->iPage + 1; k <= iPageNew; k++ )
219  p->ppPages[k] = ABC_ALLOC( word, p->nEntrySize * (1 << p->LogPageSze) );
220  // update page counter
221  p->iPage = iPageNew;
222  }
223  // update entry counter
224  p->nEntries = i + 1;
225  }
226  return Vec_MemReadEntry( p, i );
227 }
228 static inline void Vec_MemSetEntry( Vec_Mem_t * p, int i, word * pEntry )
229 {
230  word * pPlace = Vec_MemGetEntry( p, i );
231  memmove( pPlace, pEntry, sizeof(word) * p->nEntrySize );
232 }
233 static inline void Vec_MemPush( Vec_Mem_t * p, word * pEntry )
234 {
235  word * pPlace = Vec_MemGetEntry( p, p->nEntries );
236  memmove( pPlace, pEntry, sizeof(word) * p->nEntrySize );
237 }
238 
239 /**Function*************************************************************
240 
241  Synopsis []
242 
243  Description []
244 
245  SideEffects []
246 
247  SeeAlso []
248 
249 ***********************************************************************/
250 static inline void Vec_MemShrink( Vec_Mem_t * p, int nEntriesNew )
251 {
252  int i, iPageOld = p->iPage;
253  assert( nEntriesNew <= p->nEntries );
254  p->nEntries = nEntriesNew;
255  p->iPage = (nEntriesNew >> p->LogPageSze);
256  for ( i = p->iPage + 1; i <= iPageOld; i++ )
257  ABC_FREE( p->ppPages[i] );
258 }
259 
260 /**Function*************************************************************
261 
262  Synopsis []
263 
264  Description []
265 
266  SideEffects []
267 
268  SeeAlso []
269 
270 ***********************************************************************/
271 static inline void Vec_MemDumpDigit( FILE * pFile, int HexDigit )
272 {
273  assert( HexDigit >= 0 && HexDigit < 16 );
274  if ( HexDigit < 10 )
275  fprintf( pFile, "%d", HexDigit );
276  else
277  fprintf( pFile, "%c", 'A' + HexDigit-10 );
278 }
279 static inline void Vec_MemDump( FILE * pFile, Vec_Mem_t * pVec )
280 {
281  word * pEntry;
282  int i, w, d;
283  if ( pFile == stdout )
284  printf( "Memory vector has %d entries: \n", Vec_MemEntryNum(pVec) );
285  Vec_MemForEachEntry( pVec, pEntry, i )
286  {
287  for ( w = pVec->nEntrySize - 1; w >= 0; w-- )
288  for ( d = 15; d >= 0; d-- )
289  Vec_MemDumpDigit( pFile, (int)(pEntry[w] >> (d<<2)) & 15 );
290  fprintf( pFile, "\n" );
291  }
292 }
293 
294 /**Function*************************************************************
295 
296  Synopsis [Hashing entries in the memory vector.]
297 
298  Description []
299 
300  SideEffects []
301 
302  SeeAlso []
303 
304 ***********************************************************************/
305 static inline void Vec_MemHashAlloc( Vec_Mem_t * p, int nTableSize )
306 {
307  assert( p->vTable == NULL && p->vNexts == NULL );
308  p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nTableSize) );
309  p->vNexts = Vec_IntAlloc( nTableSize );
310 }
311 static inline void Vec_MemHashFree( Vec_Mem_t * p )
312 {
313  if ( p == NULL )
314  return;
315  Vec_IntFreeP( &p->vTable );
316  Vec_IntFreeP( &p->vNexts );
317 }
318 static inline unsigned Vec_MemHashKey( Vec_Mem_t * p, word * pEntry )
319 {
320  static int s_Primes[8] = { 1699, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
321  int i, nData = 2 * p->nEntrySize;
322  unsigned * pData = (unsigned *)pEntry;
323  unsigned uHash = 0;
324  for ( i = 0; i < nData; i++ )
325  uHash += pData[i] * s_Primes[i & 0x7];
326  return uHash % Vec_IntSize(p->vTable);
327 }
328 static int * Vec_MemHashLookup( Vec_Mem_t * p, word * pEntry )
329 {
330  int * pSpot = Vec_IntEntryP( p->vTable, Vec_MemHashKey(p, pEntry) );
331  for ( ; *pSpot != -1; pSpot = Vec_IntEntryP(p->vNexts, *pSpot) )
332  if ( !memcmp( Vec_MemReadEntry(p, *pSpot), pEntry, sizeof(word) * p->nEntrySize ) ) // equal
333  return pSpot;
334  return pSpot;
335 }
336 static void Vec_MemHashResize( Vec_Mem_t * p )
337 {
338  word * pEntry;
339  int i, * pSpot;
340  Vec_IntFill( p->vTable, Abc_PrimeCudd(2 * Vec_IntSize(p->vTable)), -1 );
341  Vec_IntClear( p->vNexts );
342  Vec_MemForEachEntry( p, pEntry, i )
343  {
344  pSpot = Vec_MemHashLookup( p, pEntry );
345  assert( *pSpot == -1 );
346  *pSpot = Vec_IntSize(p->vNexts);
347  Vec_IntPush( p->vNexts, -1 );
348  }
349  assert( p->nEntries == Vec_IntSize(p->vNexts) );
350 }
351 static int Vec_MemHashInsert( Vec_Mem_t * p, word * pEntry )
352 {
353  int * pSpot;
354  if ( p->nEntries > Vec_IntSize(p->vTable) )
355  Vec_MemHashResize( p );
356  pSpot = Vec_MemHashLookup( p, pEntry );
357  if ( *pSpot != -1 )
358  return *pSpot;
359  *pSpot = Vec_IntSize(p->vNexts);
360  Vec_IntPush( p->vNexts, -1 );
361  Vec_MemPush( p, pEntry );
362  assert( p->nEntries == Vec_IntSize(p->vNexts) );
363  return Vec_IntSize(p->vNexts) - 1;
364 }
365 
366 
367 /**Function*************************************************************
368 
369  Synopsis [Allocates memory vector for storing truth tables.]
370 
371  Description []
372 
373  SideEffects []
374 
375  SeeAlso []
376 
377 ***********************************************************************/
378 static inline Vec_Mem_t * Vec_MemAllocForTT( int nVars, int fCompl )
379 {
380  int Value, nWords = (nVars <= 6 ? 1 : (1 << (nVars - 6)));
381  word * uTruth = ABC_ALLOC( word, nWords );
382  Vec_Mem_t * vTtMem = Vec_MemAlloc( nWords, 12 );
383  Vec_MemHashAlloc( vTtMem, 10000 );
384  memset( uTruth, 0x00, sizeof(word) * nWords );
385  Value = Vec_MemHashInsert( vTtMem, uTruth ); assert( Value == 0 );
386  if ( fCompl )
387  memset( uTruth, 0x55, sizeof(word) * nWords );
388  else
389  memset( uTruth, 0xAA, sizeof(word) * nWords );
390  Value = Vec_MemHashInsert( vTtMem, uTruth ); assert( Value == 1 );
391  ABC_FREE( uTruth );
392  return vTtMem;
393 }
394 static inline void Vec_MemAddMuxTT( Vec_Mem_t * p, int nVars )
395 {
396  int Value, nWords = (nVars <= 6 ? 1 : (1 << (nVars - 6)));
397  word * uTruth = ABC_ALLOC( word, nWords );
398  memset( uTruth, 0xCA, sizeof(word) * nWords );
399  Value = Vec_MemHashInsert( p, uTruth ); assert( Value == 2 );
400  ABC_FREE( uTruth );
401 }
402 static inline void Vec_MemDumpTruthTables( Vec_Mem_t * p, char * pName, int nLutSize )
403 {
404  FILE * pFile;
405  char pFileName[1000];
406  sprintf( pFileName, "tt_%s_%02d.txt", pName ? pName : NULL, nLutSize );
407  pFile = pName ? fopen( pFileName, "wb" ) : stdout;
408  Vec_MemDump( pFile, p );
409  if ( pFile != stdout )
410  fclose( pFile );
411  printf( "Dumped %d %d-var truth tables into file \"%s\" (%.2f MB).\n",
412  Vec_MemEntryNum(p), nLutSize, pName ? pFileName : "stdout",
413  8.0 * Vec_MemEntryNum(p) * Vec_MemEntrySize(p) / (1 << 20) );
414 }
415 
417 
418 #endif
419 
420 ////////////////////////////////////////////////////////////////////////
421 /// END OF FILE ///
422 ////////////////////////////////////////////////////////////////////////
423 
char * memset()
int nEntries
Definition: vecMem.h:54
static int Abc_PrimeCudd(unsigned int p)
Definition: abc_global.h:383
static void Vec_MemAddMuxTT(Vec_Mem_t *p, int nVars)
Definition: vecMem.h:394
static void Vec_MemShrink(Vec_Mem_t *p, int nEntriesNew)
Definition: vecMem.h:250
word ** ppPages
Definition: vecMem.h:59
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
static void Vec_MemHashAlloc(Vec_Mem_t *p, int nTableSize)
Definition: vecMem.h:305
#define Vec_MemForEachEntry(p, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecMem.h:68
static Vec_Mem_t * Vec_MemAlloc(int nEntrySize, int LogPageSze)
FUNCTION DEFINITIONS ///.
Definition: vecMem.h:87
static unsigned Vec_MemHashKey(Vec_Mem_t *p, word *pEntry)
Definition: vecMem.h:318
static void Vec_MemHashResize(Vec_Mem_t *p)
Definition: vecMem.h:336
static int Vec_MemEntrySize(Vec_Mem_t *p)
Definition: vecMem.h:147
typedefABC_NAMESPACE_IMPL_START struct Vec_Mem_t_ Vec_Mem_t
DECLARATIONS ///.
Definition: utilMem.c:35
static int Vec_MemEntryNum(Vec_Mem_t *p)
Definition: vecMem.h:151
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static void Vec_MemClean(Vec_Mem_t *pVec, int nEntries)
Definition: vecMem.h:132
static Vec_Int_t * Vec_IntStartFull(int nSize)
Definition: vecInt.h:119
int nWords
Definition: abcNpn.c:127
int iPage
Definition: vecMem.h:58
char * memmove()
static void Vec_MemFill(Vec_Mem_t *pVec, int nEntries)
Definition: vecMem.h:129
Vec_Int_t * vTable
Definition: vecMem.h:60
int PageMask
Definition: vecMem.h:56
int LogPageSze
Definition: vecMem.h:55
static void Vec_MemWriteEntry(Vec_Mem_t *p, int i, word *pEntry)
Definition: vecMem.h:201
static int s_Primes[MAX_PRIMES]
Definition: fxuPair.c:30
int nEntrySize
Definition: vecMem.h:53
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static Vec_Mem_t * Vec_MemAllocForTT(int nVars, int fCompl)
Definition: vecMem.h:378
static void Vec_MemFreeP(Vec_Mem_t **p)
Definition: vecMem.h:105
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static void Vec_IntFill(Vec_Int_t *p, int nSize, int Fill)
Definition: bblif.c:356
static double Vec_MemMemory(Vec_Mem_t *p)
Definition: vecMem.h:175
int memcmp()
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
char * sprintf()
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Definition: abc_global.h:105
static void Vec_IntFreeP(Vec_Int_t **p)
Definition: vecInt.h:289
static void Vec_MemDumpTruthTables(Vec_Mem_t *p, char *pName, int nLutSize)
Definition: vecMem.h:402
#define ABC_NAMESPACE_HEADER_END
Definition: abc_global.h:106
static Vec_Mem_t * Vec_MemDup(Vec_Mem_t *pVec)
Definition: vecMem.h:112
int nPageAlloc
Definition: vecMem.h:57
static word * Vec_MemGetEntry(Vec_Mem_t *p, int i)
Definition: vecMem.h:206
static void Vec_MemDump(FILE *pFile, Vec_Mem_t *pVec)
Definition: vecMem.h:279
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
static word * Vec_MemReadEntryLast(Vec_Mem_t *p)
Definition: vecMem.h:196
#define ABC_FREE(obj)
Definition: abc_global.h:232
static void Vec_MemFree(Vec_Mem_t *p)
Definition: vecMem.h:97
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
static void Vec_MemSetEntry(Vec_Mem_t *p, int i, word *pEntry)
Definition: vecMem.h:228
typedefABC_NAMESPACE_HEADER_START struct Vec_Mem_t_ Vec_Mem_t
INCLUDES ///.
Definition: vecMem.h:50
static int Vec_MemHashInsert(Vec_Mem_t *p, word *pEntry)
Definition: vecMem.h:351
static void Vec_MemHashFree(Vec_Mem_t *p)
Definition: vecMem.h:311
#define assert(ex)
Definition: util_old.h:213
static int * Vec_IntEntryP(Vec_Int_t *p, int i)
Definition: vecInt.h:417
static int Vec_MemPageSize(Vec_Mem_t *p)
Definition: vecMem.h:155
static void Vec_MemDumpDigit(FILE *pFile, int HexDigit)
Definition: vecMem.h:271
static word * Vec_MemReadEntry(Vec_Mem_t *p, int i)
Definition: vecMem.h:191
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
static int * Vec_MemHashLookup(Vec_Mem_t *p, word *pEntry)
Definition: vecMem.h:328
Vec_Int_t * vNexts
Definition: vecMem.h:61
static int Vec_MemPageNum(Vec_Mem_t *p)
Definition: vecMem.h:159
static void Vec_MemPush(Vec_Mem_t *p, word *pEntry)
Definition: vecMem.h:233