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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
/**CFile****************************************************************
FileName [abcNpnSave.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Network and node package.]
Synopsis [Interface with the FPGA mapping package.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - November 21, 2006.]
Revision [$Id: abcNpnSave.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]
***********************************************************************/
#include "base/abc/abc.h"
#include "aig/aig/aig.h"
ABC_NAMESPACE_IMPL_START
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
typedef struct Npn_Obj_t_ Npn_Obj_t;
typedef struct Npn_Man_t_ Npn_Man_t;
struct Npn_Obj_t_
{
word uTruth; // truth table
int Count; // occurrences
int iNext; // next entry
};
struct Npn_Man_t_
{
Npn_Obj_t * pBuffer; // all NPN entries
int * pBins; // hash table
int nBins; // hash table size
int nBufferSize; // buffer size
int nEntries; // entry count
};
static inline Npn_Obj_t * Npn_ManObj( Npn_Man_t * p, int i ) { assert( i < p->nBufferSize ); return i ? p->pBuffer + i : NULL; }
static inline int Npn_ManObjNum( Npn_Man_t * p, Npn_Obj_t * pObj ) { assert( p->pBuffer < pObj ); return pObj - p->pBuffer; }
static word Truth[8] = {
ABC_CONST(0xAAAAAAAAAAAAAAAA),
ABC_CONST(0xCCCCCCCCCCCCCCCC),
ABC_CONST(0xF0F0F0F0F0F0F0F0),
ABC_CONST(0xFF00FF00FF00FF00),
ABC_CONST(0xFFFF0000FFFF0000),
ABC_CONST(0xFFFFFFFF00000000),
ABC_CONST(0x0000000000000000),
ABC_CONST(0xFFFFFFFFFFFFFFFF)
};
static Npn_Man_t * pNpnMan = NULL;
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_TruthPermute_rec( char * pStr, int mid, int end )
{
static int count = 0;
char * pTemp = Abc_UtilStrsav(pStr);
char e;
int i;
if ( mid == end )
{
printf( "%03d: %s\n", count++, pTemp );
return ;
}
for ( i = mid; i <= end; i++ )
{
e = pTemp[mid];
pTemp[mid] = pTemp[i];
pTemp[i] = e;
Npn_TruthPermute_rec( pTemp, mid + 1, end );
e = pTemp[mid];
pTemp[mid] = pTemp[i];
pTemp[i] = e;
}
ABC_FREE( pTemp );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Npn_TruthHasVar( word t, int v )
{
return ((t & Truth[v]) >> (1<<v)) != (t & ~Truth[v]);
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Npn_TruthSupport( word t )
{
int v, Supp = 0;
for ( v = 0; v < 6; v++ )
if ( Npn_TruthHasVar( t, v ) )
Supp |= (1 << v);
return Supp;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Npn_TruthSuppSize( word t, int nVars )
{
int v, nSupp = 0;
assert( nVars <= 6 );
for ( v = 0; v < nVars; v++ )
if ( Npn_TruthHasVar( t, v ) )
nSupp++;
return nSupp;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Npn_TruthIsMinBase( word t )
{
int Supp = Npn_TruthSupport(t);
return (Supp & (Supp+1)) == 0;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
word Npn_TruthPadWord( word uTruth, int nVars )
{
if ( nVars == 6 )
return uTruth;
if ( nVars <= 5 )
uTruth = ((uTruth & ABC_CONST(0x00000000FFFFFFFF)) << 32) | (uTruth & ABC_CONST(0x00000000FFFFFFFF));
if ( nVars <= 4 )
uTruth = ((uTruth & ABC_CONST(0x0000FFFF0000FFFF)) << 16) | (uTruth & ABC_CONST(0x0000FFFF0000FFFF));
if ( nVars <= 3 )
uTruth = ((uTruth & ABC_CONST(0x00FF00FF00FF00FF)) << 8) | (uTruth & ABC_CONST(0x00FF00FF00FF00FF));
if ( nVars <= 2 )
uTruth = ((uTruth & ABC_CONST(0x0F0F0F0F0F0F0F0F)) << 4) | (uTruth & ABC_CONST(0x0F0F0F0F0F0F0F0F));
if ( nVars <= 1 )
uTruth = ((uTruth & ABC_CONST(0x3333333333333333)) << 2) | (uTruth & ABC_CONST(0x3333333333333333));
if ( nVars == 0 )
uTruth = ((uTruth & ABC_CONST(0x5555555555555555)) << 1) | (uTruth & ABC_CONST(0x5555555555555555));
return uTruth;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Npn_TruthCountOnes( word t )
{
t = (t & ABC_CONST(0x5555555555555555)) + ((t>> 1) & ABC_CONST(0x5555555555555555));
t = (t & ABC_CONST(0x3333333333333333)) + ((t>> 2) & ABC_CONST(0x3333333333333333));
t = (t & ABC_CONST(0x0F0F0F0F0F0F0F0F)) + ((t>> 4) & ABC_CONST(0x0F0F0F0F0F0F0F0F));
t = (t & ABC_CONST(0x00FF00FF00FF00FF)) + ((t>> 8) & ABC_CONST(0x00FF00FF00FF00FF));
t = (t & ABC_CONST(0x0000FFFF0000FFFF)) + ((t>>16) & ABC_CONST(0x0000FFFF0000FFFF));
return (t & ABC_CONST(0x00000000FFFFFFFF)) + (t>>32);
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline word Npn_TruthChangePhase( word t, int v )
{
return ((t & Truth[v]) >> (1<<v)) | ((t & ~Truth[v]) << (1<<v));
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline word Npn_TruthSwapAdjacentVars( word t, int v )
{
static word PMasks[5][3] = {
{ ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) },
{ ABC_CONST(0xC3C3C3C3C3C3C3C3), ABC_CONST(0x0C0C0C0C0C0C0C0C), ABC_CONST(0x3030303030303030) },
{ ABC_CONST(0xF00FF00FF00FF00F), ABC_CONST(0x00F000F000F000F0), ABC_CONST(0x0F000F000F000F00) },
{ ABC_CONST(0xFF0000FFFF0000FF), ABC_CONST(0x0000FF000000FF00), ABC_CONST(0x00FF000000FF0000) },
{ ABC_CONST(0xFFFF00000000FFFF), ABC_CONST(0x00000000FFFF0000), ABC_CONST(0x0000FFFF00000000) }
};
assert( v < 6 );
return (t & PMasks[v][0]) | ((t & PMasks[v][1]) << (1 << v)) | ((t & PMasks[v][2]) >> (1 << v));
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline word Npn_TruthCanon( word t, int nVars, int * pPhase )
{
int fUsePolarity = 0;
int fUsePermutation = 0;
char Temp, pSigs[13], pCanonPerm[6];
int v, fChange, CanonPhase = 0;
assert( nVars < 7 );
pSigs[12] = Npn_TruthCountOnes( t );
if ( pSigs[12] > 32 )
{
t = ~t;
pSigs[12] = 64 - pSigs[12];
CanonPhase |= (1 << 6);
}
if ( fUsePolarity || fUsePermutation )
{
for ( v = 0; v < nVars; v++ )
{
pCanonPerm[v] = v;
pSigs[2*v+1] = Npn_TruthCountOnes( t & Truth[v] );
pSigs[2*v] = pSigs[12] - pSigs[2*v+1];
}
}
if ( fUsePolarity )
{
for ( v = 0; v < nVars; v++ )
{
if ( pSigs[2*v] >= pSigs[2*v+1] )
continue;
CanonPhase |= (1 << v);
Temp = pSigs[2*v];
pSigs[2*v] = pSigs[2*v+1];
pSigs[2*v+1] = Temp;
t = Npn_TruthChangePhase( t, v );
}
}
if ( fUsePermutation )
{
do {
fChange = 0;
for ( v = 0; v < nVars-1; v++ )
{
if ( fUsePolarity )
{
if ( pSigs[2*v] >= pSigs[2*(v+1)] )
continue;
}
else
{
if ( Abc_MinInt(pSigs[2*v],pSigs[2*v+1]) >= Abc_MinInt(pSigs[2*(v+1)],pSigs[2*(v+1)+1]) )
continue;
}
fChange = 1;
Temp = pCanonPerm[v];
pCanonPerm[v] = pCanonPerm[v+1];
pCanonPerm[v+1] = Temp;
Temp = pSigs[2*v];
pSigs[2*v] = pSigs[2*(v+1)];
pSigs[2*(v+1)] = Temp;
Temp = pSigs[2*v+1];
pSigs[2*v+1] = pSigs[2*(v+1)+1];
pSigs[2*(v+1)+1] = Temp;
t = Npn_TruthSwapAdjacentVars( t, v );
}
} while ( fChange );
}
if ( pPhase )
{
*pPhase = 0;
for ( v = 0; v < nVars; v++ )
*pPhase |= (pCanonPerm[v] << (4 * v));
*pPhase |= (CanonPhase << 24);
}
return t;
}
/**Function*************************************************************
Synopsis [Computes the hash key.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Npn_ManHash( Npn_Man_t * p, word uTruth )
{
word Key = (uTruth * (word)101) ^ (uTruth * (word)733) ^ (uTruth * (word)1777);
return (int)(Key % (word)p->nBins);
}
/**Function*************************************************************
Synopsis [Resizes the table.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManResize( Npn_Man_t * p )
{
Npn_Obj_t * pEntry, * pNext;
int * pBinsOld, * ppPlace;
int nBinsOld, Counter, i;
abctime clk;
assert( p->pBins != NULL );
clk = Abc_Clock();
// save the old Bins
pBinsOld = p->pBins;
nBinsOld = p->nBins;
// get the new Bins
p->nBins = Abc_PrimeCudd( 3 * nBinsOld );
p->pBins = ABC_CALLOC( int, p->nBins );
// rehash the entries from the old table
Counter = 1;
for ( i = 0; i < nBinsOld; i++ )
for ( pEntry = Npn_ManObj(p, pBinsOld[i]),
pNext = pEntry ? Npn_ManObj(p, pEntry->iNext) : NULL;
pEntry;
pEntry = pNext,
pNext = pEntry ? Npn_ManObj(p, pEntry->iNext) : NULL )
{
// get the place where this entry goes
ppPlace = p->pBins + Npn_ManHash( p, pEntry->uTruth );
// add the entry to the list
pEntry->iNext = *ppPlace;
*ppPlace = Npn_ManObjNum( p, pEntry );
Counter++;
}
assert( Counter == p->nEntries );
ABC_FREE( pBinsOld );
//ABC_PRT( "Hash table resizing time", Abc_Clock() - clk );
}
/**Function*************************************************************
Synopsis [Adds one entry to the table.]
Description [Increments ref counter by 1.]
SideEffects []
SeeAlso []
***********************************************************************/
Npn_Obj_t * Npn_ManAdd( Npn_Man_t * p, word uTruth )
{
Npn_Obj_t * pEntry;
int * pPlace, Key = Npn_ManHash( p, uTruth );
// resize the link storage if needed
if ( p->nEntries == p->nBufferSize )
{
p->nBufferSize *= 2;
p->pBuffer = ABC_REALLOC( Npn_Obj_t, p->pBuffer, p->nBufferSize );
}
// find the entry
for ( pEntry = Npn_ManObj(p, p->pBins[Key]),
pPlace = p->pBins + Key;
pEntry;
pPlace = &pEntry->iNext,
pEntry = Npn_ManObj(p, pEntry->iNext) )
if ( pEntry->uTruth == uTruth )
{
pEntry->Count++;
return pEntry;
}
// create new entry
*pPlace = p->nEntries;
assert( p->nEntries < p->nBufferSize );
pEntry = Npn_ManObj( p, p->nEntries++ );
pEntry->uTruth = uTruth;
pEntry->Count = 1;
pEntry->iNext = 0;
// resize the table if needed
if ( p->nEntries > 3 * p->nBins )
Npn_ManResize( p );
return pEntry;
}
/**Function*************************************************************
Synopsis [Fills table from file.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManRead( Npn_Man_t * p, char * pFileName )
{
char pBuffer[1000];
char * pToken;
Npn_Obj_t * pEntry;
unsigned Truth[2];
word uTruth;
FILE * pFile = fopen( pFileName, "r" );
if ( pFile == NULL )
{
Abc_Print( -1, "Cannot open NPN function file \"%s\".\n", pFileName );
return;
}
// read lines from the file
while ( fgets( pBuffer, 1000, pFile ) != NULL )
{
pToken = strtok( pBuffer, " \t\n" );
if ( pToken == NULL )
continue;
if ( pToken[0] == '#' )
continue;
if ( strlen(pToken) != 16 )
{
Abc_Print( 0, "Skipping token %s that does not look like a 16-digit hex number.\n" );
continue;
}
// extract truth table
Extra_ReadHexadecimal( Truth, pToken, 6 );
uTruth = (((word)Truth[1]) << 32) | (word)Truth[0];
// add truth table
pEntry = Npn_ManAdd( p, uTruth );
assert( pEntry->Count == 1 );
// read area
pToken = strtok( NULL, " \t\n" );
pEntry->Count = atoi(pToken);
}
fclose( pFile );
}
/**Function*************************************************************
Synopsis [Comparison procedure for two entries.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static int Npn_ManCompareEntries( Npn_Obj_t ** pp1, Npn_Obj_t ** pp2 )
{
if ( (*pp1)->Count > (*pp2)->Count )
return -1;
if ( (*pp1)->Count < (*pp2)->Count )
return 1;
return 0;
}
/**Function*************************************************************
Synopsis [Adds one entry to the table.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManWrite( Npn_Man_t * p, char * pFileName )
{
Vec_Ptr_t * vEntries;
Npn_Obj_t * pEntry;
FILE * pFile = fopen( pFileName, "w" );
int i;
if ( pFile == NULL )
{
Abc_Print( -1, "Cannot open NPN function file \"%s\".\n", pFileName );
return;
}
vEntries = Vec_PtrAlloc( p->nEntries );
for ( i = 0; i < p->nBins; i++ )
for ( pEntry = Npn_ManObj(p, p->pBins[i]); pEntry; pEntry = Npn_ManObj(p, pEntry->iNext) )
Vec_PtrPush( vEntries, pEntry );
Vec_PtrSort( vEntries, (int (*)())Npn_ManCompareEntries );
Vec_PtrForEachEntry( Npn_Obj_t *, vEntries, pEntry, i )
{
Extra_PrintHexadecimal( pFile, (unsigned *)&pEntry->uTruth, 6 );
fprintf( pFile, " %d %d\n", pEntry->Count, Npn_TruthSuppSize(pEntry->uTruth, 6) );
}
fclose( pFile );
Vec_PtrFree( vEntries );
}
/**Function*************************************************************
Synopsis [Creates the manager.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Npn_Man_t * Npn_ManStart( char * pFileName )
{
Npn_Man_t * p;
p = ABC_CALLOC( Npn_Man_t, 1 );
if ( pFileName == NULL )
{
p->nBufferSize = 1000000;
p->nBufferSize = 100;
p->pBuffer = ABC_ALLOC( Npn_Obj_t, p->nBufferSize );
p->nBins = Abc_PrimeCudd( p->nBufferSize / 2 );
p->pBins = ABC_CALLOC( int, p->nBins );
p->nEntries = 1;
}
else
{
FILE * pFile = fopen( pFileName, "r" );
if ( pFile == NULL )
{
Abc_Print( -1, "Cannot open NPN function file \"%s\".\n", pFileName );
return NULL;
}
fclose( pFile );
p->nBufferSize = 4 * ( Extra_FileSize(pFileName) / 20 );
p->pBuffer = ABC_ALLOC( Npn_Obj_t, p->nBufferSize );
p->nBins = Abc_PrimeCudd( p->nBufferSize / 2 );
p->pBins = ABC_CALLOC( int, p->nBins );
p->nEntries = 1;
Npn_ManRead( p, pFileName );
}
return p;
}
/**Function*************************************************************
Synopsis [Deletes the manager.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManStop( Npn_Man_t * p )
{
ABC_FREE( p->pBuffer );
ABC_FREE( p->pBins );
ABC_FREE( p );
}
/**Function*************************************************************
Synopsis [Cleans the manager.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManClean()
{
if ( pNpnMan != NULL )
{
Npn_ManStop( pNpnMan );
pNpnMan = NULL;
}
}
/**Function*************************************************************
Synopsis [Loads functions from a file.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManLoad( char * pFileName )
{
// Npn_TruthPermute_rec( "012345", 0, 5 );
if ( pNpnMan != NULL )
{
Abc_Print( 1, "Removing old table with %d entries.\n", pNpnMan->nEntries );
Npn_ManStop( pNpnMan );
}
pNpnMan = Npn_ManStart( pFileName );
Abc_Print( 1, "Created new table with %d entries from file \"%s\".\n", pNpnMan->nEntries, pFileName );
}
/**Function*************************************************************
Synopsis [Saves functions into a file.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManSave( char * pFileName )
{
if ( pNpnMan == NULL )
{
Abc_Print( 1, "There is no table with entries.\n" );
return;
}
Npn_ManWrite( pNpnMan, pFileName );
Abc_Print( 1, "Dumped table with %d entries from file \"%s\".\n", pNpnMan->nEntries, pFileName );
}
/**Function*************************************************************
Synopsis [Saves one function into storage.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Npn_ManSaveOne( unsigned * puTruth, int nVars )
{
word uTruth = (((word)puTruth[1]) << 32) | (word)puTruth[0];
assert( nVars >= 0 && nVars <= 6 );
if ( pNpnMan == NULL )
{
Abc_Print( 1, "Creating new table with 0 entries.\n" );
pNpnMan = Npn_ManStart( NULL );
}
// skip truth tables that do not depend on some vars
if ( !Npn_TruthIsMinBase( uTruth ) )
return;
// extend truth table to look like 6-input
uTruth = Npn_TruthPadWord( uTruth, nVars );
// semi(!)-NPN-canonize the truth table
uTruth = Npn_TruthCanon( uTruth, 6, NULL );
// add to storage
Npn_ManAdd( pNpnMan, uTruth );
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|