summaryrefslogtreecommitdiffstats
path: root/src/aig/gia/giaMinLut.c
blob: 13c9a3cea9a62b1ff1c6610bc422f86bebc7d121 (plain)
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/**CFile****************************************************************

  FileName    [giaMinLut.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Collapsing AIG.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: giaMinLut.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/

#include "gia.h"
#include "giaAig.h"
#include "base/main/mainInt.h"
#include "opt/sfm/sfm.h"

#ifdef ABC_USE_CUDD
#include "bdd/extrab/extraBdd.h"
#include "bdd/dsd/dsd.h"
#endif

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

extern Abc_Ntk_t * Abc_NtkFromAigPhase( Aig_Man_t * pMan );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Vec_WrdReadText( char * pFileName, Vec_Wrd_t ** pvSimI, Vec_Wrd_t ** pvSimO, int nIns, int nOuts )
{
    int i, nSize, iLine, nLines, nWords;
    char pLine[1000];
    Vec_Wrd_t * vSimI, * vSimO;
    FILE * pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" for reading.\n", pFileName );
        return;
    }
    fseek( pFile, 0, SEEK_END );  
    nSize = ftell( pFile );  
    if ( nSize % (nIns + nOuts + 1) > 0 )
    {
        printf( "Cannot read file with simulation data that is not aligned at 8 bytes (remainder = %d).\n", nSize % (nIns + nOuts + 1) );
        fclose( pFile );
        return;
    }
    rewind( pFile );
    nLines = nSize / (nIns + nOuts + 1);
    nWords = (nLines + 63)/64;
    vSimI  = Vec_WrdStart( nIns *nWords );
    vSimO  = Vec_WrdStart( nOuts*nWords );
    for ( iLine = 0; fgets( pLine, 1000, pFile ); iLine++ )
    {
        for ( i = 0; i < nIns; i++ )
            if ( pLine[nIns-1-i] == '1' )
                Abc_TtXorBit( Vec_WrdArray(vSimI) + i*nWords, iLine );
            else assert( pLine[nIns-1-i] == '0' );
        for ( i = 0; i < nOuts; i++ )
            if ( pLine[nIns+nOuts-1-i] == '1' )
                Abc_TtXorBit( Vec_WrdArray(vSimO) + i*nWords, iLine );
            else assert( pLine[nIns+nOuts-1-i] == '0' );
    }
    fclose( pFile );
    *pvSimI = vSimI;
    *pvSimO = vSimO;
    printf( "Read %d words of simulation data for %d inputs and %d outputs (padded %d zero-patterns).\n", nWords, nIns, nOuts, nWords*64-nLines );
}
int Vec_WrdReadText2( char * pFileName, Vec_Wrd_t ** pvSimI )
{
    int i, nSize, iLine, nLines, nWords, nIns;
    char pLine[1000];
    Vec_Wrd_t * vSimI;
    FILE * pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" for reading.\n", pFileName );
        return 0;
    }
    if ( !fgets(pLine, 1000, pFile) || (nIns = strlen(pLine)-1) < 1 )
    {
        printf( "Cannot find the number of inputs in file \"%s\".\n", pFileName );
        fclose( pFile );
        return 0;
    }
    fseek( pFile, 0, SEEK_END );  
    nSize = ftell( pFile );  
    if ( nSize % (nIns + 1) > 0 )
    {
        printf( "Cannot read file with simulation data that is not aligned at 8 bytes (remainder = %d).\n", nSize % (nIns + 1) );
        fclose( pFile );
        return 0;
    }
    rewind( pFile );
    nLines = nSize / (nIns + 1);
    nWords = (nLines + 63)/64;
    vSimI  = Vec_WrdStart( nIns *nWords );
    for ( iLine = 0; fgets( pLine, 1000, pFile ); iLine++ )
    {
        for ( i = 0; i < nIns; i++ )
            if ( pLine[nIns-1-i] == '1' )
                Abc_TtXorBit( Vec_WrdArray(vSimI) + i*nWords, iLine );
            else assert( pLine[nIns-1-i] == '0' );
    }
    fclose( pFile );
    *pvSimI = vSimI;
    printf( "Read %d words of simulation data for %d inputs (padded to 64-bit boundary with %d zero-patterns).\n", nWords, nIns, nWords*64-nLines );
    return nIns;
}
Vec_Int_t * Vec_WrdReadNumsOut( char * pFileName, int fVerbose )
{
    char pLine[1000]; 
    Vec_Int_t * vNums; int iLine;
    FILE * pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" for reading.\n", pFileName );
        return NULL;
    }
    vNums = Vec_IntAlloc( 1000 );
    for ( iLine = 0; fgets( pLine, 1000, pFile ); iLine++ )
        Vec_IntPush( vNums, atoi(pLine) );
    fclose( pFile );
    if ( fVerbose )
    printf( "Finished reading %d output values from file \"%s\".\n", Vec_IntSize(vNums), pFileName );
    return vNums;
}
Vec_Wrd_t * Vec_WrdReadTextOut( char * pFileName, int nOuts )
{
    int i, iLine, nLines, nWords;
    Vec_Wrd_t * vSimO;
    Vec_Int_t * vNums = Vec_WrdReadNumsOut( pFileName, 1 );
    if ( vNums == NULL )
        return NULL;
    nLines = Vec_IntSize(vNums);
    nWords = (nLines + 63)/64;
    vSimO  = Vec_WrdStart( nOuts*nWords );
    Vec_IntForEachEntry( vNums, i, iLine )
        Abc_TtXorBit( Vec_WrdArray(vSimO) + i*nWords, iLine );
    Vec_IntFree( vNums );
    printf( "Read %d words of simulation data for %d outputs (padded %d zero-patterns).\n", nWords, nOuts, nWords*64-nLines );
    return vSimO;
}
void Gia_ManReadSimInfoInputs( char * pFileName, char * pFileOut1, int fVerbose )
{
    Vec_Wrd_t * vSimI;
    Vec_WrdReadText2( pFileName, &vSimI );
    Vec_WrdDumpBin( pFileOut1, vSimI, fVerbose );
    Vec_WrdFree( vSimI );
}
void Gia_ManReadSimInfoOutputs( char * pFileName, char * pFileOut, int nOuts )
{
    Vec_Wrd_t * vSimO = Vec_WrdReadTextOut( pFileName, nOuts );
    Vec_WrdDumpBin( pFileOut, vSimO, 1 );
    Vec_WrdFree( vSimO );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wrd_t * Vec_WrdZoneExtract( int ZoneSize, Vec_Wrd_t * p, int iWord, int nWords )
{
    int z, nZones = Vec_WrdSize(p)/ZoneSize;
    int w, Limit = Abc_MinInt( nWords, ZoneSize-iWord );
    Vec_Wrd_t * pNew = Vec_WrdStart( nZones*nWords );
    for ( z = 0; z < nZones; z++ )
        for ( w = 0; w < Limit; w++ )
            Vec_WrdWriteEntry( pNew, z*nWords + w, Vec_WrdEntry(p, z*ZoneSize + iWord + w) );
    return pNew;
}
void Vec_WrdZoneInsert( Vec_Wrd_t * pNew, int ZoneSize, Vec_Wrd_t * p, int iWord, int nWords )
{
    int z, nZones = Vec_WrdSize(pNew)/ZoneSize;
    int w, Limit = Abc_MinInt( nWords, ZoneSize-iWord );
    for ( z = 0; z < nZones; z++ )
        for ( w = 0; w < Limit; w++ )
            Vec_WrdWriteEntry( pNew, z*ZoneSize + iWord + w, Vec_WrdEntry(p, z*nWords + w) );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManSimInfoPrintOne( Gia_Man_t * p, Vec_Wrd_t * vSimsIn, Vec_Wrd_t * vSimsOut, int nWords, int nPats )
{
    int Id, i, k;
    for ( k = 0; k < nPats; k++ )
    {
        Gia_ManForEachCiId( p, Id, i )
    //        printf( "%d", Vec_WrdEntry(p->vSims, p->nSimWords*Id) & 1 );
            printf( "%d", (int)(Vec_WrdEntry(vSimsIn,  nWords*i) >> k) & 1 );
        printf( " " );
        Gia_ManForEachCoId( p, Id, i )
    //        printf( "%d", Vec_WrdEntry(p->vSims, p->nSimWords*Id) & 1 );
            printf( "%d", (int)(Vec_WrdEntry(vSimsOut, nWords*i) >> k) & 1 );
        printf( "\n" );
    }
}
Vec_Wrd_t * Gia_ManSimInfoTryOne( Gia_Man_t * p, Vec_Wrd_t * vSimI, int fPrint )
{
    extern Vec_Wrd_t * Gia_ManSimulateWordsOut( Gia_Man_t * p, Vec_Wrd_t * vSimsIn );
    Vec_Wrd_t * vSimsOut = Gia_ManSimulateWordsOut( p, vSimI );
    int nWords = Vec_WrdSize(vSimI) / Gia_ManCiNum(p);
    assert( Vec_WrdSize(vSimI) % Gia_ManCiNum(p) == 0 );
    if ( fPrint )
        Gia_ManSimInfoPrintOne( p, vSimI, vSimsOut, nWords, 6 );
    return vSimsOut;
}
int Gia_ManSimEvalOne( Gia_Man_t * p, Vec_Wrd_t * vSimO, Vec_Wrd_t * vSimO_new )
{
    int i, Count = 0, nWords = Vec_WrdSize(vSimO) / Gia_ManCoNum(p);
    word * pSim0 = ABC_CALLOC( word, nWords );
    assert( Vec_WrdSize(vSimO) == Vec_WrdSize(vSimO_new) );
    for ( i = 0; i < Gia_ManCoNum(p); i++ )
    {
        word * pSimGold = Vec_WrdEntryP( vSimO,     i * nWords );
        word * pSimImpl = Vec_WrdEntryP( vSimO_new, i * nWords );
        Abc_TtOrXor( pSim0, pSimImpl, pSimGold, nWords );
    }
    Count = Abc_TtCountOnesVec( pSim0, nWords );
    printf( "Number of failed patterns is %d (%8.4f %% of %d). The first one is %d.\n", 
        Count, 100.0*Count/(64*nWords), 64*nWords, Abc_TtFindFirstBit2(pSim0, nWords) );
    ABC_FREE( pSim0 );
    return Count;
}
int Gia_ManSimEvalOne2( Gia_Man_t * p, Vec_Wrd_t * vSimO, Vec_Wrd_t * vSimO_new )
{
    int i, Count = 0, nWords = Vec_WrdSize(vSimO) / Gia_ManCoNum(p);
    word * pSim0 = ABC_CALLOC( word, nWords );
    assert( Vec_WrdSize(vSimO) == Vec_WrdSize(vSimO_new) );
    for ( i = 0; i < Gia_ManCoNum(p); i++ )
    {
        word * pSimGold = Vec_WrdEntryP( vSimO,     i * nWords );
        word * pSimImpl = Vec_WrdEntryP( vSimO_new, i * nWords );
        Abc_TtXor( pSim0, pSimImpl, pSimGold, nWords, 0 );
        Count += Abc_TtCountOnesVec( pSim0, nWords );
    }
    printf( "Number of failed patterns is %d (%8.4f %% of %d). The first one is %d.\n", 
        Count, 100.0*Count/(64*nWords*Gia_ManCoNum(p)), 64*nWords*Gia_ManCoNum(p), Abc_TtFindFirstBit2(pSim0, nWords) );
    ABC_FREE( pSim0 );
    return Count;
}
int Gia_ManSimEvalMaxValue( Vec_Wrd_t * vSimO, int nWords, int nOuts, int nBits, int iPat )
{
    int o, ValueMax = -1, OutMax = -1;
    for ( o = 0; o < nOuts; o++ )
    {
        int i, Value = 0;
        for ( i = 0; i < nBits; i++ )
        {
            word * pSim = Vec_WrdEntryP( vSimO, (o*nBits+i) * nWords );
            if ( Abc_TtGetBit(pSim, iPat) )
                Value |= 1 << i;
        }
        if ( ValueMax <= Value )
        {
            ValueMax = Value;
            OutMax = o;
        }
    }
    return OutMax;
}
int Gia_ManSimEvalOne3( Gia_Man_t * p, Vec_Wrd_t * vSimO, Vec_Int_t * vValues, int nBits )
{
    int i, Value, nOuts = Gia_ManCoNum(p) / nBits;
    int First = -1, Count = 0, nWords = Vec_WrdSize(vSimO) / Gia_ManCoNum(p);
    assert( Gia_ManCoNum(p) % nBits == 0 );
    assert( 64*(nWords-1) < Vec_IntSize(vValues) && Vec_IntSize(vValues) <= 64*nWords );
    Vec_IntForEachEntry( vValues, Value, i )
        if ( Value == Gia_ManSimEvalMaxValue(vSimO, nWords, nOuts, nBits, i) )
        {
            Count++;
            if ( First == -1 )
                First = i;
        }
    printf( "The accuracy is %8.4f %% (%d out of %d output are correct, for example, output number %d).\n", 
        100.0*Count/Vec_IntSize(vValues), Count, Vec_IntSize(vValues), First );
    if ( 0 )
    {
        FILE * pTable = fopen( "stats.txt", "a+" );
        fprintf( pTable, "%0.2f \n", 100.0*Count/Vec_IntSize(vValues) );
        fclose( pTable );
    }
    return Count;
}
Vec_Wrd_t * Gia_ManSimInfoTry( Gia_Man_t * p, Vec_Wrd_t * vSimI )
{
    int nWords = Vec_WrdSize(vSimI) / Gia_ManCiNum(p);
    int w, nWordsOne = 200, nWordBatches = (nWords + nWordsOne - 1)/nWordsOne;
    Vec_Wrd_t * vSimO_new = Vec_WrdStart( nWords * Gia_ManCoNum(p) );
    for ( w = 0; w < nWordBatches; w++ )
    {
        //int Value = printf( "%3d / %3d : ", w, nWordBatches );
        Vec_Wrd_t * vSimI_ = Vec_WrdZoneExtract( nWords, vSimI, w*nWordsOne, nWordsOne );
        Vec_Wrd_t * vSimO_ = Gia_ManSimInfoTryOne( p, vSimI_, 0 );
        Vec_WrdZoneInsert( vSimO_new, nWords, vSimO_, w*nWordsOne, nWordsOne );
        Vec_WrdFree( vSimI_ );
        Vec_WrdFree( vSimO_ );
        //Value = 0;
    }
    return vSimO_new;
}
int Gia_ManSimInfoEval_old( Gia_Man_t * p, Vec_Wrd_t * vSimO, Vec_Wrd_t * vSimO_new )
{
    int nResult = Gia_ManSimEvalOne2(p, vSimO, vSimO_new);
    //Vec_WrdDumpBin( "temp.simo", vSimO_new, 1 );
    printf( "Total errors = %d.  ", nResult );
    printf( "Density of output patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSimO_new), Vec_WrdSize(vSimO_new))/(64*Vec_WrdSize(vSimO_new)) );
    return nResult;
}
void Gia_ManSimInfoPassTest( Gia_Man_t * p, char * pFileName, char * pFileName2, int fVerbose )
{
    abctime clk = Abc_Clock();
    Vec_Wrd_t * vSimI = Vec_WrdReadBin( pFileName, fVerbose );
    Vec_Wrd_t * vSimO = Gia_ManSimInfoTry( p, vSimI );
    if ( fVerbose )
    printf( "Density of input  patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSimI), Vec_WrdSize(vSimI))/(64*Vec_WrdSize(vSimI)) );
    if ( fVerbose )
    printf( "Density of output patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSimO), Vec_WrdSize(vSimO))/(64*Vec_WrdSize(vSimO)) );
    Vec_WrdDumpBin( pFileName2, vSimO, fVerbose );
    Vec_WrdFree( vSimI );
    Vec_WrdFree( vSimO );
    if ( fVerbose )
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
}
void Gia_ManSimInfoEval( Gia_Man_t * p, char * pFileName, char * pFileName2, int nOuts, int fVerbose )
{
    abctime clk = Abc_Clock();
    Vec_Wrd_t * vSim1 = Vec_WrdReadBin( pFileName, fVerbose );
    Vec_Int_t * vNums = Vec_WrdReadNumsOut( pFileName2, fVerbose );
    assert( nOuts > 0 );
    if ( fVerbose )
    printf( "Density of input  patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSim1), Vec_WrdSize(vSim1))/(64*Vec_WrdSize(vSim1)) );
    Gia_ManSimEvalOne3( p, vSim1, vNums, nOuts );
    Vec_WrdFree( vSim1 );
    Vec_IntFree( vNums );
    if ( fVerbose )
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
word * Gia_ManCountFraction( Gia_Man_t * p, Vec_Wrd_t * vSimI, Vec_Int_t * vSupp, int Thresh, int fVerbose, int * pCare )
{
    Gia_Obj_t * pObj;
    int i, k, nUsed = 0, nGood = 0;
    int nWords = Vec_WrdSize(vSimI) / Gia_ManCiNum(p);
    int nMints = 1 << Vec_IntSize(vSupp);
    word ** pSims = ABC_ALLOC( word *, Vec_IntSize(vSupp) );
    word * pRes   = ABC_CALLOC( word, Abc_Truth6WordNum(Vec_IntSize(vSupp)) );
    int * pCounts = ABC_CALLOC( int, nMints );
    Gia_ManForEachObjVec( vSupp, p, pObj, i )
        pSims[i] = Vec_WrdEntryP( vSimI, Gia_ObjCioId(pObj) * nWords );
    for ( k = 0; k < 64*nWords; k++ )
    {
        int iMint = 0;
        for ( i = 0; i < Vec_IntSize(vSupp); i++ )
            if ( Abc_TtGetBit(pSims[i], k) )
                iMint |= 1 << i;
        assert( iMint < nMints );
        pCounts[iMint]++;
    }
    for ( k = 0; k < nMints; k++ )
    {
        nUsed += (pCounts[k] > 0);
        nGood += (pCounts[k] >= Thresh);
        if ( pCounts[k] >= Thresh )
            Abc_TtXorBit( pRes, k );
        //printf( "%d ", pCounts[k] );
    }
    if ( Vec_IntSize(vSupp) < 6 )
        pRes[0] = Abc_Tt6Stretch( pRes[0], Vec_IntSize(vSupp) );
    //printf( "\n" );
    if ( fVerbose )
    printf( "Used %4d and good %4d (out of %4d).\n", nUsed, nGood, nMints ); 
    ABC_FREE( pSims );
    ABC_FREE( pCounts );
    *pCare = nGood;
    return pRes;
}
void Gia_ManCollectSupp_rec( Gia_Man_t * p, int iObj, Vec_Int_t * vSupp )
{
    Gia_Obj_t * pObj;
    if ( !iObj || Gia_ObjIsTravIdCurrentId(p, iObj) )
        return;
    Gia_ObjSetTravIdCurrentId(p, iObj);
    pObj = Gia_ManObj( p, iObj );
    if ( Gia_ObjIsCi(pObj) )
    {
        //Vec_IntPush( vSupp, Gia_ObjCioId(pObj) );
        Vec_IntPush( vSupp, Gia_ObjId(p, pObj) );
        return;
    }
    assert( Gia_ObjIsAnd(pObj) );
    Gia_ManCollectSupp_rec( p, Gia_ObjFaninId0(pObj, iObj), vSupp );
    Gia_ManCollectSupp_rec( p, Gia_ObjFaninId1(pObj, iObj), vSupp );
}
Vec_Int_t * Gia_ManCollectSupp( Gia_Man_t * p, int iOut, int nOuts )
{
    Vec_Int_t * vSupp = Vec_IntAlloc( 16 ); int i;
    Gia_ManIncrementTravId( p );
    for ( i = 0; i < nOuts; i++ )
        Gia_ManCollectSupp_rec( p, Gia_ObjFaninId0p(p, Gia_ManCo(p, iOut+i)), vSupp );
    return vSupp;
}
int Gia_ManPerformLNetOpt_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj )
{
    if ( ~pObj->Value )
        return pObj->Value;
    assert( Gia_ObjIsAnd(pObj) );
    Gia_ManPerformLNetOpt_rec( pNew, p, Gia_ObjFanin0(pObj) );
    Gia_ManPerformLNetOpt_rec( pNew, p, Gia_ObjFanin1(pObj) );
    return pObj->Value = Gia_ManHashAnd( pNew, Gia_ObjFanin0Copy(pObj), Gia_ObjFanin1Copy(pObj) );
}
Gia_Man_t * Gia_ManPerformLNetOpt( Gia_Man_t * p, int fTryNew, char * pFileName, int nIns, int nOuts, int Thresh, int nRounds, int fVerbose )
{
    extern int Gia_ManDupOrderDfs_rec( Gia_Man_t * pNew, Gia_Man_t * p, Gia_Obj_t * pObj );
    extern Gia_Man_t * Gia_TryPermOpt( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
    extern Gia_Man_t * Gia_TryPermOptCare( word * pTruths, int nIns, int nOuts, int nWords, int nRounds, int fVerbose );
    extern int Kit_TruthToGia2( Gia_Man_t * p, unsigned * pTruth0, unsigned * pTruth1, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );
    abctime clk = Abc_Clock();
    Gia_Man_t * pNew; Gia_Obj_t * pObj;
    Vec_Int_t * vMemory = Vec_IntAlloc( 1 << 18 );
    Vec_Int_t * vLeaves = Vec_IntAlloc( nIns );
    Vec_Wrd_t * vSimI = pFileName ? Vec_WrdReadBin( pFileName, fVerbose ) : NULL;  
    word * pTruth0 = ABC_CALLOC( word, Abc_Truth6WordNum(nIns) );
    word * pTruth1 = ABC_CALLOC( word, Abc_Truth6WordNum(nIns) ); int g, k; float CareAve = 0;
    word * pTruthsTry = ABC_CALLOC( word, 2*nOuts*Abc_Truth6WordNum(nIns) );
    if ( vSimI && fVerbose )
    {
        //int nPats = 64*Vec_WrdSize(vSimI)/Gia_ManCiNum(p);
        printf( "Density of input  patterns %8.4f.\n", (float)Abc_TtCountOnesVec(Vec_WrdArray(vSimI), Vec_WrdSize(vSimI))/(64*Vec_WrdSize(vSimI)) );
        printf( "Using patterns with count %d and higher as cares.\n", Thresh );
    }
    Gia_ManFillValue( p );
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, k )
        pObj->Value = Gia_ManAppendCi(pNew);
    Gia_ObjComputeTruthTableStart( p, nIns );
    Gia_ManHashStart( pNew );
    for ( g = 0; g < Gia_ManCoNum(p); g += nOuts )
    {
        Vec_Int_t * vSupp = Gia_ManCollectSupp( p, g, nOuts );
        int Care = 1 << Vec_IntSize(vSupp), Temp = fVerbose ? printf( "Group %3d / %3d / %3d : Supp = %3d   %s", g, nOuts, Gia_ManCoNum(p), Vec_IntSize(vSupp), vSimI ? "":"\n" ) : 0;
        word * pCare = vSimI ? Gia_ManCountFraction( p, vSimI, vSupp, Thresh, fVerbose, &Care ) : ABC_FALLOC( word, Abc_Truth6WordNum(Vec_IntSize(vSupp)) );
        int nWords = Abc_Truth6WordNum( Vec_IntSize(vSupp) );
        CareAve += 100.0*Care/(1 << Vec_IntSize(vSupp));
        assert( Vec_IntSize(vSupp) <= nIns );
        Vec_IntClear( vLeaves );
        Gia_ManForEachObjVec( vSupp, p, pObj, k )
            Vec_IntPush( vLeaves, pObj->Value );        
        for ( k = 0; k < nOuts; k++ )
        {
            Gia_Obj_t * pObj = Gia_ManCo( p, g+k );
            word * pTruth = Gia_ObjComputeTruthTableCut( p, Gia_ObjFanin0(pObj), vSupp );
            Abc_TtSharp( pTruth0, pCare, pTruth, nWords );
            Abc_TtAnd( pTruth1, pCare, pTruth, nWords, 0 );
            if ( vSimI )
            {
                Abc_TtCopy( pTruthsTry + (2*k+0)*nWords, pTruth1, nWords, 0 );
                Abc_TtCopy( pTruthsTry + (2*k+1)*nWords, pTruth0, nWords, 0 );
            }
            else
                Abc_TtCopy( pTruthsTry + k*nWords, pTruth1, nWords, 0 );
            if ( !fTryNew )
            {
                pObj->Value = Kit_TruthToGia2( pNew, (unsigned *)pTruth0, (unsigned *)pTruth1, Vec_IntSize(vLeaves), vMemory, vLeaves, 1 );
                pObj->Value ^= Gia_ObjFaninC0(pObj);
            }
        }
        if ( fTryNew )
        {
            Gia_Man_t * pMin;
            if ( vSimI )
                pMin = Gia_TryPermOpt( pTruthsTry, Vec_IntSize(vSupp), 2*nOuts, nWords, nRounds, fVerbose );
            else
                pMin = Gia_TryPermOptCare( pTruthsTry, Vec_IntSize(vSupp), nOuts, nWords, nRounds, fVerbose );
            Gia_ManFillValue( pMin );
            Gia_ManConst0(pMin)->Value = 0;
            Gia_ManForEachCi( pMin, pObj, k )
                pObj->Value = Vec_IntEntry( vLeaves, k );
            for ( k = 0; k < nOuts; k++ )
            {
                Gia_Obj_t * pObj  = Gia_ManCo( p, g+k );
                Gia_Obj_t * pObj2 = Gia_ManCo( pMin, k );
                pObj->Value  = Gia_ManPerformLNetOpt_rec( pNew, pMin, Gia_ObjFanin0(pObj2) );
                pObj->Value ^= Gia_ObjFaninC0(pObj2);
                pObj->Value ^= Gia_ObjFaninC0(pObj);
            }
            Gia_ManStop( pMin );
        }
        ABC_FREE( pCare );
        Vec_IntFree( vSupp );
        Temp = 0;
    }
    CareAve /= Gia_ManCoNum(p)/nOuts;
    Gia_ManHashStop( pNew );
    Gia_ManForEachCo( p, pObj, k )
        pObj->Value = Gia_ManAppendCo( pNew, pObj->Value );
    Gia_ObjComputeTruthTableStop( p );
    ABC_FREE( pTruth0 );
    ABC_FREE( pTruth1 );
    Vec_IntFree( vLeaves );
    Vec_IntFree( vMemory );
    Vec_WrdFreeP( &vSimI );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    printf( "Using patterns with count %d and higher as cares. Average care set is %8.4f %%.  ", Thresh, CareAve );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    if ( 0 )
    {
        FILE * pTable = fopen( "stats.txt", "a+" );
        fprintf( pTable, "%0.2f ", CareAve );
        fclose( pTable );
    }
    ABC_FREE( pTruthsTry );
    return pNew;
}

#ifdef ABC_USE_CUDD

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDoMuxMapping( Gia_Man_t * p )
{
    extern Gia_Man_t * Gia_ManPerformMfs( Gia_Man_t * p, Sfm_Par_t * pPars );
    Gia_Man_t * pTemp, * pNew = Gia_ManDup( p );
    Jf_Par_t Pars, * pPars = &Pars; int c, nIters = 2;
    Sfm_Par_t Pars2, * pPars2 = &Pars2;
    Lf_ManSetDefaultPars( pPars );
    Sfm_ParSetDefault( pPars2 );
    pPars2->nTfoLevMax  =    5;
    pPars2->nDepthMax   =  100;
    pPars2->nWinSizeMax = 2000;
    for ( c = 0; c < nIters; c++ )
    {
        pNew = Lf_ManPerformMapping( pTemp = pNew, pPars );
        Gia_ManStop( pTemp );
        pNew = Gia_ManPerformMfs( pTemp = pNew, pPars2 );
        Gia_ManStop( pTemp );
        if ( c == nIters-1 )
            break;
        pNew = (Gia_Man_t *)Dsm_ManDeriveGia( pTemp = pNew, 0 );
        Gia_ManStop( pTemp );
    }
    return pNew;
}
Gia_Man_t * Gia_ManDoMuxTransform( Gia_Man_t * p, int fReorder )
{
    extern Gia_Man_t * Abc_NtkStrashToGia( Abc_Ntk_t * pNtk );
    extern int Abc_NtkBddToMuxesPerformGlo( Abc_Ntk_t * pNtk, Abc_Ntk_t * pNtkNew, int Limit, int fReorder, int fUseAdd );
    Gia_Man_t * pRes = NULL;
    Aig_Man_t * pMan = Gia_ManToAig( p, 0 );
    Abc_Ntk_t * pNtk = Abc_NtkFromAigPhase( pMan );
    Abc_Ntk_t * pNtkNew = Abc_NtkStartFrom( pNtk, ABC_NTK_LOGIC, ABC_FUNC_SOP );
    pNtk->pName = Extra_UtilStrsav( pMan->pName );
    Aig_ManStop( pMan );
    //pNtkNew = Abc_NtkBddToMuxes( pNtk, 1, 1000000, 1 );
    if ( Abc_NtkBddToMuxesPerformGlo( pNtk, pNtkNew, 1000000, fReorder, 0 ) )
    {
        Abc_Ntk_t * pStrash = Abc_NtkStrash( pNtkNew, 1, 1, 0 );
        pRes = Abc_NtkStrashToGia( pStrash );
        Abc_NtkDelete( pStrash );
    }
    Abc_NtkDelete( pNtkNew );
    Abc_NtkDelete( pNtk );
    return pRes;
}
int Gia_ManDoTest1( Gia_Man_t * p, int fReorder )
{
    Gia_Man_t * pTemp, * pNew; int Res;
    pNew = Gia_ManDoMuxTransform( p, fReorder );
    pNew = Gia_ManDoMuxMapping( pTemp = pNew );
    Gia_ManStop( pTemp );
    Res = Gia_ManLutNum( pNew );
    Gia_ManStop( pNew );
    return Res;
}
Abc_Ntk_t * Gia_ManDoTest2( Gia_Man_t * p, int fReorder, int fTryNew )
{
    extern Abc_Ntk_t * Abc_NtkFromMappedGia( Gia_Man_t * p, int fFindEnables, int fUseBuffs );
    Abc_Ntk_t * pNtkNew;
    Gia_Man_t * pTemp, * pNew;
    pNew = fTryNew ? Gia_ManDup(p) : Gia_ManDoMuxTransform( p, fReorder );
    pNew = Gia_ManDoMuxMapping( pTemp = pNew );
    Gia_ManStop( pTemp );
    pNtkNew = Abc_NtkFromMappedGia( pNew, 0, 0 );
    pNtkNew->pName = Extra_UtilStrsav(p->pName);
    Gia_ManStop( pNew );
    Abc_NtkToSop( pNtkNew, 1, ABC_INFINITY );
    return pNtkNew;
}
Abc_Ntk_t * Abc_NtkMapTransform( Gia_Man_t * p, int nOuts, int fUseFixed, int fTryNew, int fVerbose )
{
    extern Abc_Ntk_t * Abc_NtkSpecialMapping( Abc_Ntk_t * pNtk, int fVerbose );
    int i, k, g, nGroups = Gia_ManCoNum(p) / nOuts, CountsAll[3] = {0}; 
    Abc_Obj_t * pObjNew, * pFaninNew;  Gia_Obj_t * pObj;
    Abc_Ntk_t * pNtkNew = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_SOP, 1 );
    assert( Gia_ManCoNum(p) % nOuts == 0 );
    pNtkNew->pName = Extra_UtilStrsav(p->pName);
    pNtkNew->pSpec = Extra_UtilStrsav(p->pSpec);
    Gia_ManFillValue( p );
    Gia_ManForEachPi( p, pObj, i )
        Abc_NtkCreatePi( pNtkNew );
    Gia_ManForEachPo( p, pObj, i )
        Abc_NtkCreatePo( pNtkNew );
    assert( nOuts <= 64 );
    for ( g = 0; g < nGroups; g++ )
    {
        Gia_Man_t * pNew;   Aig_Man_t * pMan;
        Abc_Ntk_t * pNtk, * pNtkRes, * pNtkMap;
        int pPos[64], Counter = 0, Counts[3] = {0};
        for ( i = 0; i < nOuts; i++ )
            pPos[i] = g*nOuts+i;
        pNew = Gia_ManDupCones( p, pPos, nOuts, 1 );
        if ( !fUseFixed )
            pNtkMap = Gia_ManDoTest2( pNew, 1, fTryNew );
        else
        {
            pMan = Gia_ManToAig( pNew, 0 );
            pNtk = Abc_NtkFromAigPhase( pMan );
            Aig_ManStop( pMan );
            pNtkRes = Abc_NtkBddToMuxes( pNtk, 1, 1000000, 1 );
            Abc_NtkDelete( pNtk );
            pNtkMap = Abc_NtkSpecialMapping( pNtkRes, 0 );
            Abc_NtkDelete( pNtkRes );
        }
        Gia_ManStop( pNew );
        Gia_ManForEachCi( p, pObj, i )
            if ( ~pObj->Value )
                Abc_NtkCi(pNtkMap, Counter++)->pCopy = Abc_NtkCi(pNtkNew, i);
        assert( Counter == Abc_NtkCiNum(pNtkMap) );
        Abc_NtkForEachNode( pNtkMap, pObjNew, i )
        {
            pObjNew->pCopy = Abc_NtkDupObj( pNtkNew, pObjNew, 0 );
            pObjNew->pCopy->fPersist = pObjNew->fPersist;
            if ( pObjNew->fPersist )
                Counts[1]++;
            else
                Counts[0]++;
            Abc_ObjForEachFanin( pObjNew, pFaninNew, k )
                Abc_ObjAddFanin( pObjNew->pCopy, pFaninNew->pCopy );
        }
        Abc_NtkForEachCo( pNtkMap, pObjNew, i )
            Abc_ObjAddFanin( Abc_NtkCo(pNtkNew, g*nOuts+i), Abc_ObjFanin0(pObjNew)->pCopy );
        Abc_NtkDelete( pNtkMap );

        if ( fVerbose )
        {
        printf( "%3d / %3d :  ", g, nGroups );
        printf( "Test   = %4d   ", Counts[0] );
        printf( "MarkA  = %4d   ", Counts[1] );
        printf( "MarkB  = %4d   ", Counts[2] );
        printf( "\n" );
        }

        CountsAll[0] += Counts[0];
        CountsAll[1] += Counts[1];
        CountsAll[2] += Counts[2];
    }
    if ( fVerbose )
    printf( "Total LUT count = %5d.  MarkA = %5d. MarkB = %5d.\n", CountsAll[0], CountsAll[1], CountsAll[2] );
    // create names
    Abc_NtkAddDummyPiNames( pNtkNew );
    Abc_NtkAddDummyPoNames( pNtkNew );
    Abc_NtkAddDummyBoxNames( pNtkNew );
    // check the resulting AIG
    if ( !Abc_NtkCheck( pNtkNew ) )
        Abc_Print( 1, "Abc_NtkFromMappedGia(): Network check has failed.\n" );
    return pNtkNew;
}

Abc_Ntk_t * Gia_ManPerformLNetMap( Gia_Man_t * p, int GroupSize, int fUseFixed, int fTryNew, int fVerbose )
{
    int fPrintOnly = 0;
    int Res1, Res2, Result = 0;
    int g, nGroups = Gia_ManCoNum(p) / GroupSize;
    assert( Gia_ManCoNum(p) % GroupSize == 0 );
    assert( GroupSize <= 64 );
    if ( fPrintOnly )
    {
        for ( g = 0; g < nGroups; g++ )
        {
            Gia_Man_t * pNew;
            int o, pPos[64];
            for ( o = 0; o < GroupSize; o++ )
                pPos[o] = g*GroupSize+o;
            pNew = Gia_ManDupCones( p, pPos, GroupSize, 0 );
            printf( "%3d / %3d :  ", g, nGroups );
            printf( "Test1 = %4d   ", Res1 = Gia_ManDoTest1(pNew, 0) );
            printf( "Test2 = %4d   ", Res2 = Gia_ManDoTest1(pNew, 1) );
            printf( "Test  = %4d   ", Abc_MinInt(Res1, Res2) );
            printf( "\n" );
            Result += Abc_MinInt(Res1, Res2);
            //Gia_ManPrintStats( pNew, NULL );
            Gia_ManStop( pNew );
        }
        printf( "Total LUT count = %d.\n", Result );
        return NULL;

    }
    return Abc_NtkMapTransform( p, GroupSize, fUseFixed, fTryNew, fVerbose );
}

#else

Abc_Ntk_t * Gia_ManPerformLNetMap( Gia_Man_t * p, int GroupSize, int fUseFixed, int fTryNew, int fVerbose )
{
    return NULL;
}

#endif

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END