1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/**CFile****************************************************************
FileName [ FxchSCHashTable.c ]
PackageName [ Fast eXtract with Cube Hashing (FXCH) ]
Synopsis [ Sub-cubes hash table implementation ]
Author [ Bruno Schmitt - boschmitt at inf.ufrgs.br ]
Affiliation [ UFRGS ]
Date [ Ver. 1.0. Started - March 6, 2016. ]
Revision []
***********************************************************************/
#include "Fxch.h"
ABC_NAMESPACE_IMPL_START
//#ifdef _WIN32
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
//#endif
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
static inline void MurmurHash3_x86_32 ( const void* key,
int len,
uint32_t seed,
void* out )
{
const uint8_t* data = (const uint8_t*)key;
const int nblocks = len / 4;
uint32_t h1 = seed;
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
const uint8_t * tail;
uint32_t k1;
//----------
// body
const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
int i;
for(i = -nblocks; i; i++)
{
uint32_t k1 = blocks[i];
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >> (32 - 13));
h1 = h1*5+0xe6546b64;
}
//----------
// tail
tail = (const uint8_t*)(data + nblocks*4);
k1 = 0;
switch(len & 3)
{
case 3: k1 ^= tail[2] << 16;
case 2: k1 ^= tail[1] << 8;
case 1: k1 ^= tail[0];
k1 *= c1; k1 = (k1 << 15) | (k1 >> (32 - 15)); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len;
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
*(uint32_t*)out = h1;
}
Fxch_SCHashTable_t* Fxch_SCHashTableCreate( Fxch_Man_t* pFxchMan,
Vec_Int_t* vCubeLinks,
int nEntries )
{
Fxch_SCHashTable_t* pSCHashTable = ABC_CALLOC( Fxch_SCHashTable_t, 1 );
int nBits = Abc_Base2Log( nEntries + 1 );
pSCHashTable->pFxchMan = pFxchMan;
pSCHashTable->SizeMask = (1 << nBits) - 1;
pSCHashTable->vCubeLinks = vCubeLinks;
pSCHashTable->pBins = ABC_CALLOC( Fxch_SCHashTable_Entry_t, pSCHashTable->SizeMask + 1 );
return pSCHashTable;
}
void Fxch_SCHashTableDelete( Fxch_SCHashTable_t* pSCHashTable )
{
Vec_IntFree( pSCHashTable->vCubeLinks );
Vec_IntErase( &pSCHashTable->vSubCube0 );
Vec_IntErase( &pSCHashTable->vSubCube1 );
ABC_FREE( pSCHashTable->pBins );
ABC_FREE( pSCHashTable );
}
static inline Fxch_SCHashTable_Entry_t* Fxch_SCHashTableBin( Fxch_SCHashTable_t* pSCHashTable,
unsigned int SubCubeID )
{
return pSCHashTable->pBins + (SubCubeID & pSCHashTable->SizeMask);
}
static inline Fxch_SCHashTable_Entry_t* Fxch_SCHashTableEntry( Fxch_SCHashTable_t* pSCHashTable,
unsigned int iEntry )
{
if ( ( iEntry > 0 ) && ( iEntry < ( pSCHashTable->SizeMask + 1 ) ) )
return pSCHashTable->pBins + iEntry;
return NULL;
}
static inline void Fxch_SCHashTableInsertLink( Fxch_SCHashTable_t* pSCHashTable,
unsigned int iEntry0,
unsigned int iEntry1 )
{
Fxch_SCHashTable_Entry_t* pEntry0 = Fxch_SCHashTableEntry( pSCHashTable, iEntry0 ),
* pEntry1 = Fxch_SCHashTableEntry( pSCHashTable, iEntry1 ),
* pEntry0Next = Fxch_SCHashTableEntry( pSCHashTable, pEntry0->iNext );
assert( pEntry0Next->iPrev == iEntry0 );
pEntry1->iNext = pEntry0->iNext;
pEntry0->iNext = iEntry1;
pEntry1->iPrev = iEntry0;
pEntry0Next->iPrev = iEntry1;
}
static inline void Fxch_SCHashTableRemoveLink( Fxch_SCHashTable_t* pSCHashTable,
int iEntry0,
int iEntry1 )
{
Fxch_SCHashTable_Entry_t* pEntry0 = Fxch_SCHashTableEntry( pSCHashTable, iEntry0 ),
* pEntry1 = Fxch_SCHashTableEntry( pSCHashTable, iEntry1 ),
* pEntry1Next = Fxch_SCHashTableEntry( pSCHashTable, pEntry1->iNext );
assert( (int)pEntry0->iNext == iEntry1 );
assert( (int)pEntry1->iPrev == iEntry0 );
assert( (int)pEntry1Next->iPrev == iEntry1 );
pEntry0->iNext = pEntry1->iNext;
pEntry1->iNext = 0;
pEntry1Next->iPrev = pEntry1->iPrev;
pEntry1->iPrev = 0;
}
static inline int Fxch_SCHashTableEntryCompare( Fxch_SCHashTable_t* pSCHashTable,
Vec_Wec_t* vCubes,
Fxch_SubCube_t* pSCData0,
Fxch_SubCube_t* pSCData1 )
{
Vec_Int_t* vCube0 = Vec_WecEntry( vCubes, pSCData0->iCube ),
* vCube1 = Vec_WecEntry( vCubes, pSCData1->iCube );
if ( !Vec_IntSize( vCube0 ) ||
!Vec_IntSize( vCube1 ) ||
Vec_IntEntry( vCube0, 0 ) != Vec_IntEntry( vCube1, 0 ) ||
pSCData0->Id != pSCData1->Id )
return 0;
Vec_IntClear( &pSCHashTable->vSubCube0 );
Vec_IntClear( &pSCHashTable->vSubCube1 );
if ( pSCData0->iLit1 == 0 && pSCData1->iLit1 == 0 &&
Vec_IntEntry( vCube0, pSCData0->iLit0 ) == Abc_LitNot( Vec_IntEntry( vCube1, pSCData1->iLit0 ) ) )
return 0;
if ( pSCData0->iLit1 > 0 && pSCData1->iLit1 > 0 &&
( Vec_IntEntry( vCube0, pSCData0->iLit0 ) == Vec_IntEntry( vCube1, pSCData1->iLit0 ) ||
Vec_IntEntry( vCube0, pSCData0->iLit0 ) == Vec_IntEntry( vCube1, pSCData1->iLit1 ) ||
Vec_IntEntry( vCube0, pSCData0->iLit1 ) == Vec_IntEntry( vCube1, pSCData1->iLit0 ) ||
Vec_IntEntry( vCube0, pSCData0->iLit1 ) == Vec_IntEntry( vCube1, pSCData1->iLit1 ) ) )
return 0;
if ( pSCData0->iLit0 > 0 )
Vec_IntAppendSkip( &pSCHashTable->vSubCube0, vCube0, pSCData0->iLit0 );
else
Vec_IntAppend( &pSCHashTable->vSubCube0, vCube0 );
if ( pSCData1->iLit0 > 0 )
Vec_IntAppendSkip( &pSCHashTable->vSubCube1, vCube1, pSCData1->iLit0 );
else
Vec_IntAppend( &pSCHashTable->vSubCube1, vCube1 );
if ( pSCData0->iLit1 > 0)
Vec_IntDrop( &pSCHashTable->vSubCube0,
pSCData0->iLit0 < pSCData0->iLit1 ? pSCData0->iLit1 - 1 : pSCData0->iLit1 );
if ( pSCData1->iLit1 > 0 )
Vec_IntDrop( &pSCHashTable->vSubCube1,
pSCData1->iLit0 < pSCData1->iLit1 ? pSCData1->iLit1 - 1 : pSCData1->iLit1 );
Vec_IntDrop( &pSCHashTable->vSubCube0, 0 );
Vec_IntDrop( &pSCHashTable->vSubCube1, 0 );
return Vec_IntEqual( &pSCHashTable->vSubCube0, &pSCHashTable->vSubCube1 );
}
int Fxch_SCHashTableInsert( Fxch_SCHashTable_t* pSCHashTable,
Vec_Wec_t* vCubes,
unsigned int SubCubeID,
unsigned int iSubCube,
unsigned int iCube,
unsigned int iLit0,
unsigned int iLit1,
char fUpdate )
{
unsigned int BinID;
unsigned int iNewEntry;
Fxch_SCHashTable_Entry_t* pBin,* pNewEntry;
Fxch_SCHashTable_Entry_t* pEntry;
unsigned int iEntry;
char Pairs = 0,
fStart = 1;
MurmurHash3_x86_32( ( void* ) &SubCubeID, sizeof( int ), 0x9747b28c, &BinID);
iNewEntry = ( unsigned int )( Vec_IntEntry( pSCHashTable->vCubeLinks, iCube ) ) + iSubCube;
pBin = Fxch_SCHashTableBin( pSCHashTable, BinID );
pNewEntry = Fxch_SCHashTableEntry( pSCHashTable, iNewEntry );
assert( pNewEntry->Used == 0 );
pNewEntry->SCData.Id = SubCubeID;
pNewEntry->SCData.iCube = iCube;
pNewEntry->SCData.iLit0 = iLit0;
pNewEntry->SCData.iLit1 = iLit1;
pNewEntry->Used = 1;
pSCHashTable->nEntries++;
if ( pBin->iTable == 0 )
{
pBin->iTable = iNewEntry;
pNewEntry->iNext = iNewEntry;
pNewEntry->iPrev = iNewEntry;
return 0;
}
for ( iEntry = pBin->iTable; iEntry != pBin->iTable || fStart; iEntry = pEntry->iNext, fStart = 0 )
{
int Base;
int iNewDiv;
pEntry = Fxch_SCHashTableBin( pSCHashTable, iEntry );
if ( !Fxch_SCHashTableEntryCompare( pSCHashTable, vCubes, &( pEntry->SCData ), &( pNewEntry->SCData ) ) )
continue;
if ( pEntry->SCData.iLit0 == 0 )
{
printf("[FXCH] SCC detected\n");
continue;
}
if ( pNewEntry->SCData.iLit0 == 0 )
{
printf("[FXCH] SCC detected\n");
continue;
}
if ( pEntry->SCData.iCube < pNewEntry->SCData.iCube )
Base = Fxch_DivCreate( pSCHashTable->pFxchMan, &( pEntry->SCData ), &( pNewEntry->SCData ) );
else
Base = Fxch_DivCreate( pSCHashTable->pFxchMan, &( pNewEntry->SCData ), &( pEntry->SCData ) );
if ( Base < 0 )
continue;
iNewDiv = Fxch_DivAdd( pSCHashTable->pFxchMan, fUpdate, 0, Base );
Vec_WecPush( pSCHashTable->pFxchMan->vDivCubePairs, iNewDiv, pEntry->SCData.iCube );
Vec_WecPush( pSCHashTable->pFxchMan->vDivCubePairs, iNewDiv, pNewEntry->SCData.iCube );
Pairs++;
}
assert( iEntry == (unsigned int)( pBin->iTable ) );
pEntry = Fxch_SCHashTableBin( pSCHashTable, iEntry );
Fxch_SCHashTableInsertLink( pSCHashTable, pEntry->iPrev, iNewEntry );
return Pairs;
}
int Fxch_SCHashTableRemove( Fxch_SCHashTable_t* pSCHashTable,
Vec_Wec_t* vCubes,
unsigned int SubCubeID,
unsigned int iSubCube,
unsigned int iCube,
unsigned int iLit0,
unsigned int iLit1,
char fUpdate )
{
unsigned int BinID;
unsigned int iEntry;
Fxch_SCHashTable_Entry_t* pBin,* pEntry;
Fxch_SCHashTable_Entry_t* pNextEntry;
int iNextEntry,
Pairs = 0,
fStart = 1;
MurmurHash3_x86_32( ( void* ) &SubCubeID, sizeof( int ), 0x9747b28c, &BinID);
iEntry = ( unsigned int )( Vec_IntEntry( pSCHashTable->vCubeLinks, iCube ) ) + iSubCube;
pBin = Fxch_SCHashTableBin( pSCHashTable, BinID );
pEntry = Fxch_SCHashTableEntry( pSCHashTable, iEntry );
assert( pEntry->Used == 1 );
assert( pEntry->SCData.iCube == iCube );
if ( pEntry->iNext == iEntry )
{
assert( pEntry->iPrev == iEntry );
pBin->iTable = 0;
pEntry->iNext = 0;
pEntry->iPrev = 0;
pEntry->Used = 0;
return 0;
}
for ( iNextEntry = (int)pEntry->iNext; iNextEntry != (int)iEntry; iNextEntry = pNextEntry->iNext, fStart = 0 )
{
int Base,
iDiv;
pNextEntry = Fxch_SCHashTableBin( pSCHashTable, iNextEntry );
if ( !Fxch_SCHashTableEntryCompare( pSCHashTable, vCubes, &( pEntry->SCData ), &( pNextEntry->SCData ) )
|| pEntry->SCData.iLit0 == 0
|| pNextEntry->SCData.iLit0 == 0 )
continue;
if ( pNextEntry->SCData.iCube < pEntry->SCData.iCube )
Base = Fxch_DivCreate( pSCHashTable->pFxchMan, &( pNextEntry->SCData ), &( pEntry->SCData ) );
else
Base = Fxch_DivCreate( pSCHashTable->pFxchMan, &( pEntry->SCData ), &( pNextEntry->SCData ) );
if ( Base < 0 )
continue;
iDiv = Fxch_DivRemove( pSCHashTable->pFxchMan, fUpdate, 0, Base );
int i,
iCube0,
iCube1;
Vec_Int_t* vDivCubePairs = Vec_WecEntry( pSCHashTable->pFxchMan->vDivCubePairs, iDiv );
Vec_IntForEachEntryDouble( vDivCubePairs, iCube0, iCube1, i )
if ( ( iCube0 == (int)pNextEntry->SCData.iCube && iCube1 == (int)pEntry->SCData.iCube ) ||
( iCube0 == (int)pEntry->SCData.iCube && iCube1 == (int)pNextEntry->SCData.iCube ) )
{
Vec_IntDrop( vDivCubePairs, i+1 );
Vec_IntDrop( vDivCubePairs, i );
}
if ( Vec_IntSize( vDivCubePairs ) == 0 )
Vec_IntErase( vDivCubePairs );
Pairs++;
}
if ( pBin->iTable == iEntry )
pBin->iTable = ( pEntry->iNext != iEntry ) ? pEntry->iNext : 0;
pEntry->Used = 0;
Fxch_SCHashTableRemoveLink( pSCHashTable, pEntry->iPrev, iEntry );
return Pairs;
}
unsigned int Fxch_SCHashTableMemory( Fxch_SCHashTable_t* pHashTable )
{
unsigned int Memory = sizeof ( Fxch_SCHashTable_t );
Memory += Vec_IntMemory( pHashTable->vCubeLinks );
Memory += sizeof( Fxch_SubCube_t ) * ( pHashTable->SizeMask + 1 );
return Memory;
}
void Fxch_SCHashTablePrint( Fxch_SCHashTable_t* pHashTable )
{
int Memory;
printf( "SubCube Hash Table at %p\n", ( void* )pHashTable );
printf("%20s %20s\n", "nEntries",
"Memory Usage (MB)" );
Memory = Fxch_SCHashTableMemory( pHashTable );
printf("%20d %18.2f\n", pHashTable->nEntries,
( ( double ) Memory / 1048576 ) );
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|