00001
00021 #include "ivy.h"
00022
00026
00027
00028 static unsigned Ivy_Hash( Ivy_Obj_t * pObj, int TableSize )
00029 {
00030 unsigned Key = Ivy_ObjIsExor(pObj) * 1699;
00031 Key ^= Ivy_ObjFaninId0(pObj) * 7937;
00032 Key ^= Ivy_ObjFaninId1(pObj) * 2971;
00033 Key ^= Ivy_ObjFaninC0(pObj) * 911;
00034 Key ^= Ivy_ObjFaninC1(pObj) * 353;
00035 Key ^= Ivy_ObjInit(pObj) * 911;
00036 return Key % TableSize;
00037 }
00038
00039
00040 static int * Ivy_TableFind( Ivy_Man_t * p, Ivy_Obj_t * pObj )
00041 {
00042 int i;
00043 assert( Ivy_ObjIsHash(pObj) );
00044 for ( i = Ivy_Hash(pObj, p->nTableSize); p->pTable[i]; i = (i+1) % p->nTableSize )
00045 if ( p->pTable[i] == pObj->Id )
00046 break;
00047 return p->pTable + i;
00048 }
00049
00050 static void Ivy_TableResize( Ivy_Man_t * p );
00051 static unsigned int Cudd_PrimeAig( unsigned int p );
00052
00056
00068 Ivy_Obj_t * Ivy_TableLookup( Ivy_Man_t * p, Ivy_Obj_t * pObj )
00069 {
00070 Ivy_Obj_t * pEntry;
00071 int i;
00072 assert( !Ivy_IsComplement(pObj) );
00073 if ( !Ivy_ObjIsHash(pObj) )
00074 return NULL;
00075 assert( Ivy_ObjIsLatch(pObj) || Ivy_ObjFaninId0(pObj) > 0 );
00076 assert( Ivy_ObjFaninId1(pObj) == 0 || Ivy_ObjFaninId0(pObj) < Ivy_ObjFaninId1(pObj) );
00077 if ( Ivy_ObjFanin0(pObj)->nRefs == 0 || (Ivy_ObjChild1(pObj) && Ivy_ObjFanin1(pObj)->nRefs == 0) )
00078 return NULL;
00079 for ( i = Ivy_Hash(pObj, p->nTableSize); p->pTable[i]; i = (i+1) % p->nTableSize )
00080 {
00081 pEntry = Ivy_ManObj( p, p->pTable[i] );
00082 if ( Ivy_ObjChild0(pEntry) == Ivy_ObjChild0(pObj) &&
00083 Ivy_ObjChild1(pEntry) == Ivy_ObjChild1(pObj) &&
00084 Ivy_ObjInit(pEntry) == Ivy_ObjInit(pObj) &&
00085 Ivy_ObjType(pEntry) == Ivy_ObjType(pObj) )
00086 return pEntry;
00087 }
00088 return NULL;
00089 }
00090
00102 void Ivy_TableInsert( Ivy_Man_t * p, Ivy_Obj_t * pObj )
00103 {
00104 int * pPlace;
00105 assert( !Ivy_IsComplement(pObj) );
00106 if ( !Ivy_ObjIsHash(pObj) )
00107 return;
00108 if ( (pObj->Id & 63) == 0 )
00109 {
00110 if ( p->nTableSize < 2 * Ivy_ManHashObjNum(p) )
00111 Ivy_TableResize( p );
00112 }
00113 pPlace = Ivy_TableFind( p, pObj );
00114 assert( *pPlace == 0 );
00115 *pPlace = pObj->Id;
00116 }
00117
00129 void Ivy_TableDelete( Ivy_Man_t * p, Ivy_Obj_t * pObj )
00130 {
00131 Ivy_Obj_t * pEntry;
00132 int i, * pPlace;
00133 assert( !Ivy_IsComplement(pObj) );
00134 if ( !Ivy_ObjIsHash(pObj) )
00135 return;
00136 pPlace = Ivy_TableFind( p, pObj );
00137 assert( *pPlace == pObj->Id );
00138 *pPlace = 0;
00139
00140 i = pPlace - p->pTable;
00141 for ( i = (i+1) % p->nTableSize; p->pTable[i]; i = (i+1) % p->nTableSize )
00142 {
00143 pEntry = Ivy_ManObj( p, p->pTable[i] );
00144 p->pTable[i] = 0;
00145 Ivy_TableInsert( p, pEntry );
00146 }
00147 }
00148
00162 void Ivy_TableUpdate( Ivy_Man_t * p, Ivy_Obj_t * pObj, int ObjIdNew )
00163 {
00164 int * pPlace;
00165 assert( !Ivy_IsComplement(pObj) );
00166 if ( !Ivy_ObjIsHash(pObj) )
00167 return;
00168 pPlace = Ivy_TableFind( p, pObj );
00169 assert( *pPlace == pObj->Id );
00170 *pPlace = ObjIdNew;
00171 }
00172
00184 int Ivy_TableCountEntries( Ivy_Man_t * p )
00185 {
00186 int i, Counter = 0;
00187 for ( i = 0; i < p->nTableSize; i++ )
00188 Counter += (p->pTable[i] != 0);
00189 return Counter;
00190 }
00191
00203 void Ivy_TableResize( Ivy_Man_t * p )
00204 {
00205 int * pTableOld, * pPlace;
00206 int nTableSizeOld, Counter, nEntries, e, clk;
00207 clk = clock();
00208
00209 pTableOld = p->pTable;
00210 nTableSizeOld = p->nTableSize;
00211
00212 p->nTableSize = Cudd_PrimeAig( 5 * Ivy_ManHashObjNum(p) );
00213 p->pTable = ALLOC( int, p->nTableSize );
00214 memset( p->pTable, 0, sizeof(int) * p->nTableSize );
00215
00216 Counter = 0;
00217 for ( e = 0; e < nTableSizeOld; e++ )
00218 {
00219 if ( pTableOld[e] == 0 )
00220 continue;
00221 Counter++;
00222
00223 pPlace = Ivy_TableFind( p, Ivy_ManObj(p, pTableOld[e]) );
00224 assert( *pPlace == 0 );
00225 *pPlace = pTableOld[e];
00226 }
00227 nEntries = Ivy_ManHashObjNum(p);
00228
00229
00230
00231
00232 free( pTableOld );
00233 }
00234
00246 void Ivy_TableProfile( Ivy_Man_t * p )
00247 {
00248 int i, Counter = 0;
00249 for ( i = 0; i < p->nTableSize; i++ )
00250 {
00251 if ( p->pTable[i] )
00252 Counter++;
00253 else if ( Counter )
00254 {
00255 printf( "%d ", Counter );
00256 Counter = 0;
00257 }
00258 }
00259 }
00260
00272 unsigned int Cudd_PrimeAig( unsigned int p)
00273 {
00274 int i,pn;
00275
00276 p--;
00277 do {
00278 p++;
00279 if (p&1) {
00280 pn = 1;
00281 i = 3;
00282 while ((unsigned) (i * i) <= p) {
00283 if (p % i == 0) {
00284 pn = 0;
00285 break;
00286 }
00287 i += 2;
00288 }
00289 } else {
00290 pn = 0;
00291 }
00292 } while (!pn);
00293 return(p);
00294
00295 }
00296
00300
00301