From 7d0921330b1f4e789901b4c2450920e7c412f95f Mon Sep 17 00:00:00 2001 From: Alan Mishchenko Date: Mon, 8 May 2006 08:01:00 -0700 Subject: Version abc60508 --- src/misc/nm/nm.h | 69 +++++++++++++ src/misc/nm/nmApi.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++ src/misc/nm/nmInt.h | 86 +++++++++++++++++ src/misc/nm/nmTable.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 680 insertions(+) create mode 100644 src/misc/nm/nm.h create mode 100644 src/misc/nm/nmApi.c create mode 100644 src/misc/nm/nmInt.h create mode 100644 src/misc/nm/nmTable.c (limited to 'src/misc/nm') diff --git a/src/misc/nm/nm.h b/src/misc/nm/nm.h new file mode 100644 index 00000000..5b17aaed --- /dev/null +++ b/src/misc/nm/nm.h @@ -0,0 +1,69 @@ +/**CFilextern e**************************************************************** + + FileName [nm.h] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [Name manager.] + + Synopsis [External declarations.] + + Author [Alan Mishchenko] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: nm.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#ifndef __NM_H__ +#define __NM_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +//////////////////////////////////////////////////////////////////////// +/// INCLUDES /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// PARAMETERS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// BASIC TYPES /// +//////////////////////////////////////////////////////////////////////// + +typedef struct Nm_Man_t_ Nm_Man_t; + +//////////////////////////////////////////////////////////////////////// +/// MACRO DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +/*=== nmApi.c ==========================================================*/ +extern Nm_Man_t * Nm_ManCreate( int nSize ); +extern void Nm_ManFree( Nm_Man_t * p ); +extern int Nm_ManNumEntries( Nm_Man_t * p ); +extern char * Nm_ManStoreIdName( Nm_Man_t * p, int ObjId, char * pName, char * pSuffix ); +extern char * Nm_ManCreateUniqueName( Nm_Man_t * p, int ObjId ); +extern char * Nm_ManFindNameById( Nm_Man_t * p, int ObjId ); +extern int Nm_ManFindIdByName( Nm_Man_t * p, char * pName, int * pSecond ); +extern void Nm_ManPrintTables( Nm_Man_t * p ); + +#ifdef __cplusplus +} +#endif + +#endif + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// + diff --git a/src/misc/nm/nmApi.c b/src/misc/nm/nmApi.c new file mode 100644 index 00000000..72ec24e6 --- /dev/null +++ b/src/misc/nm/nmApi.c @@ -0,0 +1,262 @@ +/**CFile**************************************************************** + + FileName [nmApi.c] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [Name manager.] + + Synopsis [APIs of the name manager.] + + Author [Alan Mishchenko] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: nmApi.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#include "nmInt.h" + +//////////////////////////////////////////////////////////////////////// +/// DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// + +/**Function************************************************************* + + Synopsis [Allocates the name manager.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +Nm_Man_t * Nm_ManCreate( int nSize ) +{ + Nm_Man_t * p; + // allocate the table + p = ALLOC( Nm_Man_t, 1 ); + memset( p, 0, sizeof(Nm_Man_t) ); + // set the parameters + p->nSizeFactor = 3; // determined how much larger the table should be compared to data in it + p->nGrowthFactor = 3; // determined how much the table grows after resizing + // allocate and clean the bins + p->nBins = Cudd_PrimeNm(p->nSizeFactor*nSize); + p->pBinsI2N = ALLOC( Nm_Entry_t *, p->nBins ); + p->pBinsN2I = ALLOC( Nm_Entry_t *, p->nBins ); + memset( p->pBinsI2N, 0, sizeof(Nm_Entry_t *) * p->nBins ); + memset( p->pBinsN2I, 0, sizeof(Nm_Entry_t *) * p->nBins ); + // start the memory manager + p->pMem = Extra_MmFlexStart(); + return p; +} + +/**Function************************************************************* + + Synopsis [Deallocates the name manager.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +void Nm_ManFree( Nm_Man_t * p ) +{ + Extra_MmFlexStop( p->pMem, 0 ); + FREE( p->pBinsI2N ); + FREE( p->pBinsN2I ); + FREE( p ); +} + +/**Function************************************************************* + + Synopsis [Returns the number of objects with names.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Nm_ManNumEntries( Nm_Man_t * p ) +{ + return p->nEntries; +} + +/**Function************************************************************* + + Synopsis [Creates a new entry in the name manager.] + + Description [Returns 1 if the entry with the given object ID + already exists in the name manager.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +char * Nm_ManStoreIdName( Nm_Man_t * p, int ObjId, char * pName, char * pSuffix ) +{ + Nm_Entry_t * pEntry, * pEntry2; + int RetValue, nEntrySize; + if ( pEntry = Nm_ManTableLookupId(p, ObjId) ) + { + if ( strcmp(pEntry->Name, pName) == 0 ) + printf( "Nm_ManStoreIdName(): Entry with the same ID and name already exists.\n" ); + else + printf( "Nm_ManStoreIdName(): Entry with the same ID and different name already exists.\n" ); + return NULL; + } + if ( pSuffix == NULL && (pEntry = Nm_ManTableLookupName(p, pName, &pEntry2)) && pEntry2 ) + { + printf( "Nm_ManStoreIdName(): Two entries with the same name already exist.\n" ); + return NULL; + } + // create the entry + nEntrySize = sizeof(Nm_Entry_t) + strlen(pName) + (pSuffix?strlen(pSuffix):0) + 1; + nEntrySize = (nEntrySize / 4 + ((nEntrySize % 4) > 0)) * 4; + pEntry = (Nm_Entry_t *)Extra_MmFlexEntryFetch( p->pMem, nEntrySize ); + pEntry->ObjId = ObjId; + sprintf( pEntry->Name, "%s%s", pName, pSuffix? pSuffix : "" ); + // add the entry to the hash table + RetValue = Nm_ManTableAdd( p, pEntry ); + assert( RetValue == 1 ); + return pEntry->Name; +} + + +/**Function************************************************************* + + Synopsis [Finds a unique name for the node.] + + Description [If the name exists, tries appending numbers to it until + it becomes unique.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +char * Nm_ManCreateUniqueName( Nm_Man_t * p, int ObjId ) +{ + static char NameStr[1000]; + Nm_Entry_t * pEntry; + int i; + if ( pEntry = Nm_ManTableLookupId(p, ObjId) ) + return pEntry->Name; + sprintf( NameStr, "[%d]", ObjId ); + for ( i = 1; Nm_ManTableLookupName(p, NameStr, NULL); i++ ) + sprintf( NameStr, "[%d]_%d", ObjId, i ); + return Nm_ManStoreIdName( p, ObjId, NameStr, NULL ); +} + +/**Function************************************************************* + + Synopsis [Returns name of the object if the ID is known.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +char * Nm_ManFindNameById( Nm_Man_t * p, int ObjId ) +{ + Nm_Entry_t * pEntry; + if ( pEntry = Nm_ManTableLookupId(p, ObjId) ) + return pEntry->Name; + return NULL; +} + +/**Function************************************************************* + + Synopsis [Returns ID of the object if its name is known.] + + Description [This procedure may return two IDs because POs and latches + may have the same name (the only allowed case of name duplication).] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Nm_ManFindIdByName( Nm_Man_t * p, char * pName, int * pSecond ) +{ + Nm_Entry_t * pEntry, * pEntry2; + if ( pEntry = Nm_ManTableLookupName(p, pName, &pEntry2) ) + { + if ( pSecond ) + *pSecond = pEntry2? pEntry2->ObjId : -1; + return pEntry->ObjId; + } + return -1; +} + + +/**Function************************************************************* + + Synopsis [Prints distribution of entries in the bins.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +void Nm_ManPrintTables( Nm_Man_t * p ) +{ + int i, Counter; + + // rehash the entries from the old table + Counter = 0; + printf( "Int2Name: " ); + for ( i = 0; i < p->nBins; i++ ) + { + if ( Counter == 0 && p->pBinsI2N[i] == NULL ) + continue; + if ( p->pBinsI2N[i] ) + Counter++; + else + { + printf( "%d ", Counter ); + Counter = 0; + } + } + printf( "\n" ); + + // rehash the entries from the old table + Counter = 0; + printf( "Name2Int: " ); + for ( i = 0; i < p->nBins; i++ ) + { + if ( Counter == 0 && p->pBinsN2I[i] == NULL ) + continue; + if ( p->pBinsN2I[i] ) + Counter++; + else + { + printf( "%d ", Counter ); + Counter = 0; + } + } + printf( "\n" ); +} + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// + + diff --git a/src/misc/nm/nmInt.h b/src/misc/nm/nmInt.h new file mode 100644 index 00000000..d0475c23 --- /dev/null +++ b/src/misc/nm/nmInt.h @@ -0,0 +1,86 @@ +/**CFile**************************************************************** + + FileName [nmInt.h] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [Name manager.] + + Synopsis [Internal declarations.] + + Author [Alan Mishchenko] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: nmInt.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#ifndef __NM_INT_H__ +#define __NM_INT_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +//////////////////////////////////////////////////////////////////////// +/// INCLUDES /// +//////////////////////////////////////////////////////////////////////// + +#include "extra.h" +#include "nm.h" + +//////////////////////////////////////////////////////////////////////// +/// PARAMETERS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// BASIC TYPES /// +//////////////////////////////////////////////////////////////////////// + +typedef struct Nm_Entry_t_ Nm_Entry_t; +struct Nm_Entry_t_ +{ + int ObjId; // object ID + char Name[0]; // name of the object +}; + +struct Nm_Man_t_ +{ + Nm_Entry_t ** pBinsI2N; // mapping IDs into names + Nm_Entry_t ** pBinsN2I; // mapping names into IDs + int nBins; // the number of bins in tables + int nEntries; // the number of entries + int nSizeFactor; // determined how much larger the table should be + int nGrowthFactor; // determined how much the table grows after resizing + Extra_MmFlex_t * pMem; // memory manager for entries (and names) +}; + +//////////////////////////////////////////////////////////////////////// +/// MACRO DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +/*=== nmTable.c ==========================================================*/ +extern int Nm_ManTableAdd( Nm_Man_t * p, Nm_Entry_t * pEntry ); +extern int Nm_ManTableDelete( Nm_Man_t * p, Nm_Entry_t * pEntry ); +extern Nm_Entry_t * Nm_ManTableLookupId( Nm_Man_t * p, int ObjId ); +extern Nm_Entry_t * Nm_ManTableLookupName( Nm_Man_t * p, char * pName, Nm_Entry_t ** ppSecond ); +extern unsigned int Cudd_PrimeNm( unsigned int p ); + +#ifdef __cplusplus +} +#endif + +#endif + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// + + diff --git a/src/misc/nm/nmTable.c b/src/misc/nm/nmTable.c new file mode 100644 index 00000000..86c520d8 --- /dev/null +++ b/src/misc/nm/nmTable.c @@ -0,0 +1,263 @@ +/**CFile**************************************************************** + + FileName [nmTable.c] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [Name manager.] + + Synopsis [Hash table for the name manager.] + + Author [Alan Mishchenko] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: nmTable.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#include "nmInt.h" + +//////////////////////////////////////////////////////////////////////// +/// DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +// hashing for integers +static unsigned Nm_HashNumber( int Num, int TableSize ) +{ + unsigned Key = 0; + Key ^= ( Num & 0xFF) * 7937; + Key ^= ((Num >> 8) & 0xFF) * 2971; + Key ^= ((Num >> 16) & 0xFF) * 911; + Key ^= ((Num >> 24) & 0xFF) * 353; + return Key % TableSize; +} + +// hashing for strings +static unsigned Nm_HashString( char * pName, int TableSize ) +{ + static int s_Primes[10] = { + 1291, 1699, 2357, 4177, 5147, + 5647, 6343, 7103, 7873, 8147 + }; + unsigned i, Key = 0; + for ( i = 0; pName[i] != '\0'; i++ ) + Key ^= s_Primes[i%10]*pName[i]; + return Key % TableSize; +} + +static void Nm_ManResize( Nm_Man_t * p ); + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// + +/**Function************************************************************* + + Synopsis [Adds an entry to two hash tables.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Nm_ManTableAdd( Nm_Man_t * p, Nm_Entry_t * pEntry ) +{ + int i; + // resize the tables if needed + if ( p->nEntries * p->nSizeFactor > p->nBins ) + { +// Nm_ManPrintTables( p ); + Nm_ManResize( p ); + } + // hash it by ID + for ( i = Nm_HashNumber(pEntry->ObjId, p->nBins); p->pBinsI2N[i]; i = (i+1) % p->nBins ) + if ( p->pBinsI2N[i] == pEntry ) + return 0; + assert( p->pBinsI2N[i] == NULL ); + p->pBinsI2N[i] = pEntry; + // hash it by Name + for ( i = Nm_HashString(pEntry->Name, p->nBins); p->pBinsN2I[i]; i = (i+1) % p->nBins ) + if ( p->pBinsN2I[i] == pEntry ) + return 0; + assert( p->pBinsN2I[i] == NULL ); + p->pBinsN2I[i] = pEntry; + // report successfully added entry + p->nEntries++; + return 1; +} + +/**Function************************************************************* + + Synopsis [Deletes the entry from two hash tables.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Nm_ManTableDelete( Nm_Man_t * p, Nm_Entry_t * pEntry ) +{ + assert( 0 ); + return 0; +} + +/**Function************************************************************* + + Synopsis [Looks up the entry by ID.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +Nm_Entry_t * Nm_ManTableLookupId( Nm_Man_t * p, int ObjId ) +{ + int i; + for ( i = Nm_HashNumber(ObjId, p->nBins); p->pBinsI2N[i]; i = (i+1) % p->nBins ) + if ( p->pBinsI2N[i]->ObjId == ObjId ) + return p->pBinsI2N[i]; + return NULL; +} + +/**Function************************************************************* + + Synopsis [Looks up the entry by name. May return two entries.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +Nm_Entry_t * Nm_ManTableLookupName( Nm_Man_t * p, char * pName, Nm_Entry_t ** ppSecond ) +{ + Nm_Entry_t * pFirst, * pSecond; + int i; + pFirst = pSecond = NULL; + for ( i = Nm_HashString(pName, p->nBins); p->pBinsN2I[i]; i = (i+1) % p->nBins ) + if ( strcmp(p->pBinsN2I[i]->Name, pName) == 0 ) + { + if ( pFirst == NULL ) + pFirst = p->pBinsN2I[i]; + else if ( pSecond == NULL ) + pSecond = p->pBinsN2I[i]; + else + assert( 0 ); // name appears more than 2 times + } + // save the names + if ( ppSecond ) + *ppSecond = pSecond; + return pFirst; +} + + +/**Function************************************************************* + + Synopsis [Resizes the table.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +void Nm_ManResize( Nm_Man_t * p ) +{ + Nm_Entry_t ** pBinsNewI2N, ** pBinsNewN2I, * pEntry; + int nBinsNew, Counter, e, i, clk; + +clk = clock(); + // get the new table size + nBinsNew = Cudd_PrimeCopy( p->nGrowthFactor * p->nBins ); + // allocate a new array + pBinsNewI2N = ALLOC( Nm_Entry_t *, nBinsNew ); + pBinsNewN2I = ALLOC( Nm_Entry_t *, nBinsNew ); + memset( pBinsNewI2N, 0, sizeof(Nm_Entry_t *) * nBinsNew ); + memset( pBinsNewN2I, 0, sizeof(Nm_Entry_t *) * nBinsNew ); + // rehash the entries from the old table + Counter = 0; + for ( e = 0; e < p->nBins; e++ ) + { + pEntry = p->pBinsI2N[e]; + if ( pEntry == NULL ) + continue; + Counter++; + + // hash it by ID + for ( i = Nm_HashNumber(pEntry->ObjId, nBinsNew); pBinsNewI2N[i]; i = (i+1) % nBinsNew ) + if ( pBinsNewI2N[i] == pEntry ) + assert( 0 ); + assert( pBinsNewI2N[i] == NULL ); + pBinsNewI2N[i] = pEntry; + // hash it by Name + for ( i = Nm_HashString(pEntry->Name, nBinsNew); pBinsNewN2I[i]; i = (i+1) % nBinsNew ) + if ( pBinsNewN2I[i] == pEntry ) + assert( 0 ); + assert( pBinsNewN2I[i] == NULL ); + pBinsNewN2I[i] = pEntry; + } + assert( Counter == p->nEntries ); +// printf( "Increasing the structural table size from %6d to %6d. ", p->nBins, nBinsNew ); +// PRT( "Time", clock() - clk ); + // replace the table and the parameters + free( p->pBinsI2N ); + free( p->pBinsN2I ); + p->pBinsI2N = pBinsNewI2N; + p->pBinsN2I = pBinsNewN2I; + p->nBins = nBinsNew; +} + + + +/**Function************************************************************* + + Synopsis [Returns the smallest prime larger than the number.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +unsigned int Cudd_PrimeNm( unsigned int p) +{ + int i,pn; + + p--; + do { + p++; + if (p&1) { + pn = 1; + i = 3; + while ((unsigned) (i * i) <= p) { + if (p % i == 0) { + pn = 0; + break; + } + i += 2; + } + } else { + pn = 0; + } + } while (!pn); + return(p); + +} /* end of Cudd_Prime */ + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// + + -- cgit v1.2.3