21#ifndef ABC__misc__vec__vecMem_h
22#define ABC__misc__vec__vecMem_h
68#define Vec_MemForEachEntry( p, pEntry, i ) \
69 for ( i = 0; (i < Vec_MemEntryNum(p)) && ((pEntry) = Vec_MemReadEntry(p, i)); i++ )
87static inline void Vec_MemAlloc_(
Vec_Mem_t *
p,
int nEntrySize,
int LogPageSze )
90 p->nEntrySize = nEntrySize;
91 p->LogPageSze = LogPageSze;
92 p->PageMask = (1 <<
p->LogPageSze) - 1;
95static inline Vec_Mem_t * Vec_MemAlloc(
int nEntrySize,
int LogPageSze )
99 p->nEntrySize = nEntrySize;
100 p->LogPageSze = LogPageSze;
101 p->PageMask = (1 <<
p->LogPageSze) - 1;
105static inline void Vec_MemFree(
Vec_Mem_t *
p )
108 for ( i = 0; i <=
p->iPage; i++ )
113static inline void Vec_MemFreeP(
Vec_Mem_t **
p )
137static inline void Vec_MemFill(
Vec_Mem_t * pVec,
int nEntries )
140static inline void Vec_MemClean(
Vec_Mem_t * pVec,
int nEntries )
155static inline int Vec_MemEntrySize(
Vec_Mem_t *
p )
157 return p->nEntrySize;
159static inline int Vec_MemEntryNum(
Vec_Mem_t *
p )
163static inline int Vec_MemPageSize(
Vec_Mem_t *
p )
165 return p->LogPageSze;
167static inline int Vec_MemPageNum(
Vec_Mem_t *
p )
183static inline double Vec_MemMemory(
Vec_Mem_t *
p )
185 return (
double)
sizeof(
word) *
p->nEntrySize * (1 <<
p->LogPageSze) * (
p->iPage + 1) + (
double)
sizeof(
word *) *
p->nPageAlloc + (
double)
sizeof(
Vec_Mem_t);
201 assert( i >= 0 && i < p->nEntries );
202 return p->ppPages[i >>
p->LogPageSze] +
p->nEntrySize * (i &
p->PageMask);
207 return Vec_MemReadEntry(
p,
p->nEntries-1 );
209static inline void Vec_MemWriteEntry(
Vec_Mem_t *
p,
int i,
word * pEntry )
211 word * pPlace = Vec_MemReadEntry(
p, i );
212 memmove( pPlace, pEntry,
sizeof(
word) *
p->nEntrySize );
217 if ( i >=
p->nEntries )
219 int k, iPageNew = (i >>
p->LogPageSze);
220 if (
p->iPage < iPageNew )
223 if ( iPageNew >=
p->nPageAlloc )
224 p->ppPages =
ABC_REALLOC(
word *,
p->ppPages, (
p->nPageAlloc =
p->nPageAlloc ? 2 *
p->nPageAlloc : iPageNew + 32) );
226 for ( k =
p->iPage + 1; k <= iPageNew; k++ )
234 return Vec_MemReadEntry(
p, i );
236static inline void Vec_MemSetEntry(
Vec_Mem_t *
p,
int i,
word * pEntry )
238 word * pPlace = Vec_MemGetEntry(
p, i );
239 memmove( pPlace, pEntry,
sizeof(
word) * (
size_t)
p->nEntrySize );
243 word * pPlace = Vec_MemGetEntry(
p,
p->nEntries );
244 memmove( pPlace, pEntry,
sizeof(
word) * (
size_t)
p->nEntrySize );
258static inline void Vec_MemShrink(
Vec_Mem_t *
p,
int nEntriesNew )
260 int i, iPageOld =
p->iPage;
261 assert( nEntriesNew <= p->nEntries );
262 p->nEntries = nEntriesNew;
263 p->iPage = (nEntriesNew >>
p->LogPageSze);
264 for ( i =
p->iPage + 1; i <= iPageOld; i++ )
279static inline void Vec_MemDumpDigit( FILE * pFile,
int HexDigit )
281 assert( HexDigit >= 0 && HexDigit < 16 );
283 fprintf( pFile,
"%d", HexDigit );
285 fprintf( pFile,
"%c",
'A' + HexDigit-10 );
287static inline void Vec_MemDump( FILE * pFile,
Vec_Mem_t * pVec )
291 if ( pFile == stdout )
292 printf(
"Memory vector has %d entries: \n", Vec_MemEntryNum(pVec) );
295 for ( w = pVec->nEntrySize - 1; w >= 0; w-- )
296 for ( d = 15; d >= 0; d-- )
297 Vec_MemDumpDigit( pFile, (
int)(pEntry[w] >> (d<<2)) & 15 );
298 fprintf( pFile,
"\n" );
313static inline void Vec_MemHashAlloc(
Vec_Mem_t *
p,
int nTableSize )
315 assert(
p->vTable == NULL &&
p->vNexts == NULL );
316 p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nTableSize) );
317 p->vNexts = Vec_IntAlloc( nTableSize );
319static inline void Vec_MemHashFree(
Vec_Mem_t *
p )
323 Vec_IntFreeP( &
p->vTable );
324 Vec_IntFreeP( &
p->vNexts );
326static inline unsigned Vec_MemHashKey(
Vec_Mem_t *
p,
word * pEntry )
328 static int s_Primes[8] = { 1699, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
329 int i, nData = 2 *
p->nEntrySize;
330 unsigned * pData = (
unsigned *)pEntry;
332 for ( i = 0; i < nData; i++ )
333 uHash += pData[i] * s_Primes[i & 0x7];
334 return uHash % Vec_IntSize(
p->vTable);
338 int * pSpot = Vec_IntEntryP(
p->vTable, Vec_MemHashKey(
p, pEntry) );
339 for ( ; *pSpot != -1; pSpot = Vec_IntEntryP(
p->vNexts, *pSpot) )
340 if ( !
memcmp( Vec_MemReadEntry(
p, *pSpot), pEntry,
sizeof(
word) *
p->nEntrySize ) )
344static void Vec_MemHashProfile(
Vec_Mem_t *
p )
347 for ( e = 0; e < 1000; e++ )
350 int * pSpot = Vec_IntEntryP(
p->vTable, e );
351 for ( ; *pSpot != -1; pSpot = Vec_IntEntryP(
p->vNexts, *pSpot) )
353 printf(
"%d ", Count );
362 Vec_IntFill(
p->vTable, Abc_PrimeCudd(2 * Vec_IntSize(
p->vTable)), -1 );
363 Vec_IntClear(
p->vNexts );
366 pSpot = Vec_MemHashLookup(
p, pEntry );
368 *pSpot = Vec_IntSize(
p->vNexts);
369 Vec_IntPush(
p->vNexts, -1 );
371 assert(
p->nEntries == Vec_IntSize(
p->vNexts) );
376 if (
p->nEntries > Vec_IntSize(
p->vTable) )
377 Vec_MemHashResize(
p );
378 pSpot = Vec_MemHashLookup(
p, pEntry );
381 *pSpot = Vec_IntSize(
p->vNexts);
382 Vec_IntPush(
p->vNexts, -1 );
383 Vec_MemPush(
p, pEntry );
384 assert(
p->nEntries == Vec_IntSize(
p->vNexts) );
385 return Vec_IntSize(
p->vNexts) - 1;
400static inline Vec_Mem_t * Vec_MemAllocForTTSimple(
int nVars )
402 int nWords = (nVars <= 6 ? 1 : (1 << (nVars - 6)));
404 Vec_MemHashAlloc( vTtMem, 10000 );
407static inline Vec_Mem_t * Vec_MemAllocForTT(
int nVars,
int fCompl )
409 int Value,
nWords = (nVars <= 6 ? 1 : (1 << (nVars - 6)));
412 Vec_MemHashAlloc( vTtMem, 10000 );
414 Value = Vec_MemHashInsert( vTtMem, uTruth );
assert( Value == 0 );
419 Value = Vec_MemHashInsert( vTtMem, uTruth );
assert( Value == 1 );
423static inline Vec_Mem_t * Vec_MemAllocWithTTs(
int nVars )
425 static word sTruths6[6] = {
433 Vec_Mem_t * vTtMem = Vec_MemAllocForTTSimple( nVars );
434 int i, k,
nWords = nVars <= 6 ? 1 : (1 << (nVars - 6));
436 int Value = Vec_MemHashInsert( vTtMem, pTruth );
assert( Value == 0 );
437 for ( i = 0; i < nVars; i++ ) {
439 for ( k = 0; k <
nWords; k++ )
440 pTruth[k] = sTruths6[i];
442 for ( k = 0; k <
nWords; k++ )
443 pTruth[k] = (k & (1 << (i-6))) ? ~(
word)0 : 0;
444 Value = Vec_MemHashInsert( vTtMem, pTruth );
assert( Value == 1+i );
449static inline void Vec_MemAddMuxTT(
Vec_Mem_t *
p,
int nVars )
451 int Value,
nWords = (nVars <= 6 ? 1 : (1 << (nVars - 6)));
454 Value = Vec_MemHashInsert(
p, uTruth );
assert( Value == 2 );
457static inline void Vec_MemDumpTruthTables(
Vec_Mem_t *
p,
char * pName,
int nLutSize )
460 char pFileName[1000];
461 sprintf( pFileName,
"tt_%s_%02d.txt", pName ? pName : NULL, nLutSize );
462 pFile = pName ? fopen( pFileName,
"wb" ) : stdout;
463 Vec_MemDump( pFile,
p );
464 if ( pFile != stdout )
466 printf(
"Dumped %d %d-var truth tables into file \"%s\" (%.2f MB).\n",
467 Vec_MemEntryNum(
p), nLutSize, pName ? pFileName :
"stdout",
468 8.0 * Vec_MemEntryNum(
p) * Vec_MemEntrySize(
p) / (1 << 20) );
#define ABC_ALLOC(type, num)
#define ABC_REALLOC(type, obj, num)
#define ABC_CALLOC(type, num)
#define ABC_CONST(number)
PARAMETERS ///.
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
unsigned __int64 word
DECLARATIONS ///.
typedefABC_NAMESPACE_IMPL_START struct Vec_Mem_t_ Vec_Mem_t
DECLARATIONS ///.
#define Vec_MemForEachEntry(p, pEntry, i)
MACRO DEFINITIONS ///.