summaryrefslogtreecommitdiffstats
path: root/src/sop/ft/ftFactor.c
blob: 04779fe0167f590f0b369a26ffd0865c908f867b (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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/**CFile****************************************************************

  FileName    [ftFactor.c]

  PackageName [MVSIS 2.0: Multi-valued logic synthesis system.]

  Synopsis    [Procedures for algebraic factoring.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - February 1, 2003.]

  Revision    [$Id: ftFactor.c,v 1.3 2003/09/01 04:56:43 alanmi Exp $]

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

#include "abc.h"
#include "mvc.h"
#include "ft.h"

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

// the types of nodes in FFs
enum { FT_NODE_NONE, FT_NODE_AND, FT_NODE_OR, FT_NODE_INV, FT_NODE_LEAF, FT_NODE_0, FT_NODE_1 };

static Ft_Node_t *       Ft_Factor_rec( Vec_Int_t * vForm, Mvc_Cover_t * pCover );
static Ft_Node_t *       Ft_FactorLF_rec( Vec_Int_t * vForm, Mvc_Cover_t * pCover, Mvc_Cover_t * pSimple );

static Ft_Node_t *       Ft_FactorTrivial( Vec_Int_t * vForm, Mvc_Cover_t * pCover );
static Ft_Node_t *       Ft_FactorTrivialCube( Vec_Int_t * vForm, Mvc_Cover_t * pCover, Mvc_Cube_t * pCube );
static Ft_Node_t *       Ft_FactorTrivialTree_rec( Vec_Int_t * vForm, Ft_Node_t ** ppNodes, int nNodes, int fAnd );
static Ft_Node_t *       Ft_FactorTrivialCascade( Vec_Int_t * vForm, Mvc_Cover_t * pCover );
static Ft_Node_t *       Ft_FactorTrivialCubeCascade( Vec_Int_t * vForm, Mvc_Cover_t * pCover, Mvc_Cube_t * pCube );

static Ft_Node_t *       Ft_FactorNodeCreate( Vec_Int_t * vForm, int Type, Ft_Node_t * pNode1, Ft_Node_t * pNode2 );
static Ft_Node_t *       Ft_FactorLeafCreate( Vec_Int_t * vForm, int iLit );
static void              Ft_FactorFinalize( Vec_Int_t * vForm, Ft_Node_t * pNode, int nVars );
static void              Ft_FactorComplement( Vec_Int_t * vForm );
static Vec_Int_t *       Ft_FactorConst( int fConst1 );

// temporary procedures that work with the covers
static Mvc_Cover_t *     Ft_ConvertSopToMvc( char * pSop );
static int               Ft_FactorVerify( char * pSop, Vec_Int_t * vForm );

// temporary managers
static Mvc_Manager_t *   pMem = NULL;
static DdManager *       dd = NULL;
 
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Factors the cover.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Ft_Factor( char * pSop )
{
    Vec_Int_t * vForm;
    Ft_Node_t * pNode;
    Mvc_Cover_t * pCover;
    int nVars = Abc_SopGetVarNum( pSop );

    // derive the cover from the SOP representation
    pCover = Ft_ConvertSopToMvc( pSop );

    // make sure the cover is CCS free (should be done before CST)
    Mvc_CoverContain( pCover );
    // check for trivial functions
    if ( Mvc_CoverIsEmpty(pCover) )
    {
        Mvc_CoverFree( pCover );
        return Ft_FactorConst( 0 );
    }
    if ( Mvc_CoverIsTautology(pCover) )
    {
        Mvc_CoverFree( pCover );
        return Ft_FactorConst( 1 );
    }

    // perform CST
    Mvc_CoverInverse( pCover ); // CST

    // start the factored form
    vForm = Vec_IntAlloc( 1000 );
    Vec_IntFill( vForm, nVars, 0 );
    // factor the cover
    pNode = Ft_Factor_rec( vForm, pCover );
    // finalize the factored form
    Ft_FactorFinalize( vForm, pNode, nVars );
    // check if the cover was originally complented
    if ( Abc_SopGetPhase(pSop) == 0 )
        Ft_FactorComplement( vForm );

    // verify the factored form
//    if ( !Ft_FactorVerify( pSop, vForm ) )
//        printf( "Verification has failed.\n" );

//    Mvc_CoverInverse( pCover ); // undo CST
    Mvc_CoverFree( pCover );
    return vForm;
}

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

  Synopsis    [Internal recursive factoring procedure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_Factor_rec( Vec_Int_t * vForm, Mvc_Cover_t * pCover )
{
    Mvc_Cover_t * pDiv, * pQuo, * pRem, * pCom;
    Ft_Node_t * pNodeDiv, * pNodeQuo, * pNodeRem;
    Ft_Node_t * pNodeAnd, * pNode;

    // make sure the cover contains some cubes
    assert( Mvc_CoverReadCubeNum(pCover) );

    // get the divisor
    pDiv = Mvc_CoverDivisor( pCover );
    if ( pDiv == NULL )
        return Ft_FactorTrivial( vForm, pCover );

    // divide the cover by the divisor
    Mvc_CoverDivideInternal( pCover, pDiv, &pQuo, &pRem );
    assert( Mvc_CoverReadCubeNum(pQuo) );

    Mvc_CoverFree( pDiv );
    Mvc_CoverFree( pRem );

    // check the trivial case
    if ( Mvc_CoverReadCubeNum(pQuo) == 1 )
    {
        pNode = Ft_FactorLF_rec( vForm, pCover, pQuo );
        Mvc_CoverFree( pQuo );
        return pNode;
    }

    // make the quotient cube free
    Mvc_CoverMakeCubeFree( pQuo );

    // divide the cover by the quotient
    Mvc_CoverDivideInternal( pCover, pQuo, &pDiv, &pRem );

    // check the trivial case
    if ( Mvc_CoverIsCubeFree( pDiv ) )
    {
        pNodeDiv = Ft_Factor_rec( vForm, pDiv );
        pNodeQuo = Ft_Factor_rec( vForm, pQuo );
        Mvc_CoverFree( pDiv );
        Mvc_CoverFree( pQuo );
        pNodeAnd = Ft_FactorNodeCreate( vForm, FT_NODE_AND, pNodeDiv, pNodeQuo );
        if ( Mvc_CoverReadCubeNum(pRem) == 0 )
        {
            Mvc_CoverFree( pRem );
            return pNodeAnd;
        }
        else
        {
            pNodeRem = Ft_Factor_rec( vForm, pRem );
            Mvc_CoverFree( pRem );
            return Ft_FactorNodeCreate( vForm, FT_NODE_OR,  pNodeAnd, pNodeRem );
        }
    }

    // get the common cube
    pCom = Mvc_CoverCommonCubeCover( pDiv );
    Mvc_CoverFree( pDiv );
    Mvc_CoverFree( pQuo );
    Mvc_CoverFree( pRem );

    // solve the simple problem
    pNode = Ft_FactorLF_rec( vForm, pCover, pCom );
    Mvc_CoverFree( pCom );
    return pNode;
}


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

  Synopsis    [Internal recursive factoring procedure for the leaf case.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorLF_rec( Vec_Int_t * vForm, Mvc_Cover_t * pCover, Mvc_Cover_t * pSimple )
{
    Mvc_Cover_t * pDiv, * pQuo, * pRem;
    Ft_Node_t * pNodeDiv, * pNodeQuo, * pNodeRem;
    Ft_Node_t * pNodeAnd;

    // get the most often occurring literal
    pDiv = Mvc_CoverBestLiteralCover( pCover, pSimple );
    // divide the cover by the literal
    Mvc_CoverDivideByLiteral( pCover, pDiv, &pQuo, &pRem );
    // get the node pointer for the literal
    pNodeDiv = Ft_FactorTrivialCube( vForm, pDiv, Mvc_CoverReadCubeHead(pDiv) );
    Mvc_CoverFree( pDiv );
    // factor the quotient and remainder
    pNodeQuo = Ft_Factor_rec( vForm, pQuo );
    Mvc_CoverFree( pQuo );
    pNodeAnd = Ft_FactorNodeCreate( vForm, FT_NODE_AND, pNodeDiv, pNodeQuo );
    if ( Mvc_CoverReadCubeNum(pRem) == 0 )
    {
        Mvc_CoverFree( pRem );
        return pNodeAnd;
    }
    else
    {
        pNodeRem = Ft_Factor_rec( vForm, pRem );
        Mvc_CoverFree( pRem );
        return Ft_FactorNodeCreate( vForm, FT_NODE_OR,  pNodeAnd, pNodeRem );
    }
}



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

  Synopsis    [Factoring the cover, which has no algebraic divisors.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorTrivial( Vec_Int_t * vForm, Mvc_Cover_t * pCover )
{
    Ft_Node_t ** ppNodes;
    Ft_Node_t * pNode;
    Mvc_Cube_t * pCube;
    int i, nNodes;

    // create space to put the cubes
    nNodes = Mvc_CoverReadCubeNum(pCover);
    assert( nNodes > 0 );
    ppNodes = ALLOC( Ft_Node_t *, nNodes );
    // create the factored form for each cube
    i = 0;
    Mvc_CoverForEachCube( pCover, pCube )
        ppNodes[i++] = Ft_FactorTrivialCube( vForm, pCover, pCube );
    assert( i == nNodes );
    // balance the factored forms
    pNode = Ft_FactorTrivialTree_rec( vForm, ppNodes, nNodes, 0 );
    free( ppNodes );
    return pNode;
}

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

  Synopsis    [Factoring the cube.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorTrivialCube( Vec_Int_t * vForm, Mvc_Cover_t * pCover, Mvc_Cube_t * pCube )
{
    Ft_Node_t ** ppNodes;
    Ft_Node_t * pNode;
    int iBit, Value, i;

    // create space to put each literal
    ppNodes = ALLOC( Ft_Node_t *, pCover->nBits );
    // create the factored form for each literal
    i = 0;
    Mvc_CubeForEachBit( pCover, pCube, iBit, Value )
    {
        if ( Value )
            ppNodes[i++] = Ft_FactorLeafCreate( vForm, iBit );
    }
    assert( i > 0 && i < pCover->nBits );
    // balance the factored forms
    pNode = Ft_FactorTrivialTree_rec( vForm, ppNodes, i, 1 );
    free( ppNodes );
    return pNode;
}
 
/**Function*************************************************************

  Synopsis    [Create the well-balanced tree of nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorTrivialTree_rec( Vec_Int_t * vForm, Ft_Node_t ** ppNodes, int nNodes, int fAnd )
{
    Ft_Node_t * pNode1, * pNode2;
    int nNodes1, nNodes2;

    if ( nNodes == 1 )
        return ppNodes[0];

    // split the nodes into two parts
    nNodes1 = nNodes/2;
    nNodes2 = nNodes - nNodes1;

    // recursively construct the tree for the parts
    pNode1 = Ft_FactorTrivialTree_rec( vForm, ppNodes,           nNodes1, fAnd );
    pNode2 = Ft_FactorTrivialTree_rec( vForm, ppNodes + nNodes1, nNodes2, fAnd );

    return Ft_FactorNodeCreate( vForm, fAnd? FT_NODE_AND : FT_NODE_OR, pNode1, pNode2 );
}



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

  Synopsis    [Factoring the cover, which has no algebraic divisors.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorTrivialCascade( Vec_Int_t * vForm, Mvc_Cover_t * pCover )
{
    Ft_Node_t * pNode;
    Mvc_Cube_t * pCube;

    // iterate through the cubes
    pNode = NULL;
    Mvc_CoverForEachCube( pCover, pCube )
    {
        if ( pNode == NULL )
            pNode = Ft_FactorTrivialCube( vForm, pCover, pCube );
        else
            pNode = Ft_FactorNodeCreate( vForm, FT_NODE_OR, pNode, Ft_FactorTrivialCubeCascade( vForm, pCover, pCube ) );
    }
    assert( pNode ); // if this assertion fails, the input cover is not SCC-free
    return pNode;
}

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

  Synopsis    [Factoring the cube.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorTrivialCubeCascade( Vec_Int_t * vForm, Mvc_Cover_t * pCover, Mvc_Cube_t * pCube )
{
    Ft_Node_t * pNode;
    int iBit, Value;

    // iterate through the literals
    pNode = NULL;
    Mvc_CubeForEachBit( pCover, pCube, iBit, Value )
    {
        if ( Value )
        {
            if ( pNode == NULL )
                pNode = Ft_FactorLeafCreate( vForm, iBit );
            else
                pNode = Ft_FactorNodeCreate( vForm, FT_NODE_AND, pNode, Ft_FactorLeafCreate( vForm, iBit ) );
        }
    }
    assert( pNode ); // if this assertion fails, the input cover is not SCC-free
    return pNode;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorNodeCreate( Vec_Int_t * vForm, int Type, Ft_Node_t * pNode1, Ft_Node_t * pNode2 )
{
    Ft_Node_t * pNode;
    // get the new node
    Vec_IntPush( vForm, 0 );
    pNode = Ft_NodeReadLast( vForm );
    // set the inputs and other info
    pNode->iFanin0 = (Ft_Node_t *)Ptr_Regular(pNode1) - (Ft_Node_t *)vForm->pArray;
    pNode->iFanin1 = (Ft_Node_t *)Ptr_Regular(pNode2) - (Ft_Node_t *)vForm->pArray;
    assert( pNode->iFanin0 < (unsigned)vForm->nSize );
    assert( pNode->iFanin1 < (unsigned)vForm->nSize );
    pNode->fIntern = 1;
    pNode->fCompl  = 0;
    pNode->fConst  = 0;
    pNode->fEdge0  = Ptr_IsComplement(pNode1);
    pNode->fEdge1  = Ptr_IsComplement(pNode2);
    // consider specific gates
    if ( Type == FT_NODE_OR )
    {
        pNode->fCompl0 = !Ptr_IsComplement(pNode1);
        pNode->fCompl1 = !Ptr_IsComplement(pNode2);
        pNode->fNodeOr = 1;
        return Ptr_Not( pNode );
    }
    if ( Type == FT_NODE_AND )
    {
        pNode->fCompl0 = Ptr_IsComplement(pNode1);
        pNode->fCompl1 = Ptr_IsComplement(pNode2);
        pNode->fNodeOr = 0;
        return pNode;
    }
    assert( 0 );
    return NULL;

/*
    Vec_Int_t * vForm;
    assert( pNode1 && pNode2 );
    pNode = MEM_ALLOC( vForm->pMem, void, 1 );
    memset( pNode, 0, sizeof(void) );
    pNode->Type = Type;
    pNode->pOne = pNode1;
    pNode->pTwo = pNode2;
    // update FF statistics
    if ( pNode->Type == FT_NODE_LEAF )
        vForm->nNodes++;
    return pNode;
*/
}

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

  Synopsis    [Factoring the cube.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Ft_Node_t * Ft_FactorLeafCreate( Vec_Int_t * vForm, int iLit )
{
    return Ptr_NotCond( Ft_NodeRead(vForm, iLit/2), iLit%2 ); // using CST

/*
    Vm_VarMap_t * pVm;
    int * pValuesFirst, * pValues;
    int nValuesIn, nVarsIn;
    Vec_Int_t * vForm;
    int iVar;
    pVm = vForm->pVm;
    pValues      = Vm_VarMapReadValuesArray(pVm);
    pValuesFirst = Vm_VarMapReadValuesFirstArray(pVm);
    nValuesIn    = Vm_VarMapReadValuesInNum(pVm);
    nVarsIn      = Vm_VarMapReadVarsInNum(pVm);
    assert( iLit < nValuesIn );
    for ( iVar = 0; iVar < nVarsIn; iVar++ )
        if ( iLit < pValuesFirst[iVar] + pValues[iVar] )
            break;
    assert( iVar < nVarsIn );
    pNode = Ft_FactorNodeCreate( vForm, FT_NODE_LEAF, NULL, NULL );
    pNode->VarNum  = iVar;
    pNode->nValues = pValues[iVar];
    pNode->uData   = FT_MV_MASK(pNode->nValues) ^ (1 << (iLit - pValuesFirst[iVar]));
    return pNode;
*/
}


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

  Synopsis    [Adds a single-variable literal if necessary.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ft_FactorFinalize( Vec_Int_t * vForm, Ft_Node_t * pRoot, int nVars )
{
    Ft_Node_t * pRootR = Ptr_Regular(pRoot);
    int iNode = pRootR - (Ft_Node_t *)vForm->pArray;
    Ft_Node_t * pNode;
    if ( iNode >= nVars )
    {
        // set the complemented attribute
        pRootR->fCompl = Ptr_IsComplement(pRoot);
        return;
    }
    // create a new node
    Vec_IntPush( vForm, 0 );
    pNode = Ft_NodeReadLast( vForm );
    pNode->iFanin0 = iNode;
    pNode->iFanin1 = iNode;
    pNode->fIntern = 1;
    pNode->fCompl  = Ptr_IsComplement(pRoot);
}

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

  Synopsis    [Computes the number of variables in the factored form.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ft_FactorGetNumVars( Vec_Int_t * vForm )
{
    int i;
    for ( i = 0; i < vForm->nSize; i++ )
        if ( vForm->pArray[i] )
            break;
    return i;
}

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

  Synopsis    [Computes the number of variables in the factored form.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ft_FactorGetNumNodes( Vec_Int_t * vForm )
{
    Ft_Node_t * pNode;
    int i;
    pNode = Ft_NodeReadLast(vForm);
    if ( pNode->fConst )
        return 0;
    if ( !pNode->fConst && pNode->iFanin0 == pNode->iFanin1 ) // literal
        return 1;
    for ( i = 0; i < vForm->nSize; i++ )
        if ( vForm->pArray[i] )
            break;
    return vForm->nSize - i + 1;
}

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

  Synopsis    [Complements the factored form.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ft_FactorComplement( Vec_Int_t * vForm )
{
    Ft_Node_t * pNode;
    int nVars = Ft_FactorGetNumVars( vForm );
    assert( nVars >= 0 );
    pNode = Ft_NodeReadLast(vForm);
    pNode->fCompl ^= 1;
}

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

  Synopsis    [Creates a constant 0 or 1 factored form.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Ft_FactorConst( int fConst1 )
{
    Vec_Int_t * vForm;
    Ft_Node_t * pNode;
    // create the constant node
    vForm = Vec_IntAlloc( 1 );
    Vec_IntPush( vForm, 0 );
    pNode = Ft_NodeReadLast( vForm );
    pNode->fIntern = 1;
    pNode->fConst  = 1;
    pNode->fCompl  = !fConst1;
    return vForm;
}






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

  Synopsis    [Start the MVC manager used in the factoring package.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ft_FactorStartMan()
{
    assert( pMem == NULL );
    pMem = Mvc_ManagerStart();
    dd = Cudd_Init( 0, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
}

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

  Synopsis    [Stops the MVC maanager used in the factoring package.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Ft_FactorStopMan()
{
    assert( pMem );
    Mvc_ManagerFree( pMem );
    Cudd_Quit( dd );
    pMem = NULL;
    dd = NULL;
}





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

  Synopsis    [Converts SOP into MVC.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Mvc_Cover_t * Ft_ConvertSopToMvc( char * pSop )
{
    Mvc_Cover_t * pMvc;
    Mvc_Cube_t * pMvcCube;
    char * pCube;
    int nVars, Value, v;

    // start the cover
    nVars = Abc_SopGetVarNum(pSop);
    pMvc = Mvc_CoverAlloc( pMem, nVars * 2 );
    // check the logic function of the node
    Abc_SopForEachCube( pSop, nVars, pCube )
    {
        // create and add the cube
        pMvcCube = Mvc_CubeAlloc( pMvc );
        Mvc_CoverAddCubeTail( pMvc, pMvcCube );
        // fill in the literals
        Mvc_CubeBitFill( pMvcCube );
        Abc_CubeForEachVar( pCube, Value, v )
        {
            if ( Value == '0' )
                Mvc_CubeBitRemove( pMvcCube, v * 2 + 1 );
            else if ( Value == '1' )
                Mvc_CubeBitRemove( pMvcCube, v * 2 );
        }
    }
//Mvc_CoverPrint( pMvc );
    return pMvc;
}



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

  Synopsis    [Converts SOP into BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Ft_ConvertSopToBdd( DdManager * dd, char * pSop )
{
    DdNode * bSum, * bCube, * bTemp, * bVar;
    char * pCube;
    int nVars, Value, v;
    // start the cover
    nVars = Abc_SopGetVarNum(pSop);
    // check the logic function of the node
    bSum = Cudd_ReadLogicZero(dd);   Cudd_Ref( bSum );
    Abc_SopForEachCube( pSop, nVars, pCube )
    {
        bCube = Cudd_ReadOne(dd);   Cudd_Ref( bCube );
        Abc_CubeForEachVar( pCube, Value, v )
        {
            if ( Value == '0' )
                bVar = Cudd_Not( Cudd_bddIthVar( dd, v ) );
            else if ( Value == '1' )
                bVar = Cudd_bddIthVar( dd, v );
            else
                continue;
            bCube  = Cudd_bddAnd( dd, bTemp = bCube, bVar );   Cudd_Ref( bCube );
            Cudd_RecursiveDeref( dd, bTemp );
        }
        bSum = Cudd_bddOr( dd, bTemp = bSum, bCube );   Cudd_Ref( bSum );
        Cudd_RecursiveDeref( dd, bTemp );
        Cudd_RecursiveDeref( dd, bCube );
    }
    // complement the result if necessary
    bSum = Cudd_NotCond( bSum, !Abc_SopGetPhase(pSop) );
    Cudd_Deref( bSum );
    return bSum;
}

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

  Synopsis    [Converts SOP into BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Ft_ConvertFormToBdd( DdManager * dd, Vec_Int_t * vForm )
{
    Vec_Ptr_t * vFuncs;
    DdNode * bFunc, * bFunc0, * bFunc1;
    Ft_Node_t * pNode;
    int i, nVars;

    // sanity checks
    nVars = Ft_FactorGetNumVars( vForm );
    assert( nVars >= 0 );
    assert( vForm->nSize > nVars );

    // check for constant function
    pNode = Ft_NodeRead( vForm, 0 );
    if ( pNode->fConst )
        return Cudd_NotCond( dd->one, pNode->fCompl );

    // start the array of elementary variables
    vFuncs = Vec_PtrAlloc( 20 );
    for ( i = 0; i < nVars; i++ )
        Vec_PtrPush( vFuncs, Cudd_bddIthVar(dd, i) );

    // compute the functions of other nodes
    for ( i = nVars; i < vForm->nSize; i++ )
    {
        pNode  = Ft_NodeRead( vForm, i );
        bFunc0 = Cudd_NotCond( vFuncs->pArray[pNode->iFanin0], pNode->fCompl0 ); 
        bFunc1 = Cudd_NotCond( vFuncs->pArray[pNode->iFanin1], pNode->fCompl1 ); 
        bFunc  = Cudd_bddAnd( dd, bFunc0, bFunc1 );   Cudd_Ref( bFunc );
        Vec_PtrPush( vFuncs, bFunc );
    }
    assert( vForm->nSize = vFuncs->nSize );

    // deref the intermediate results
    for ( i = nVars; i < vForm->nSize-1; i++ )
        Cudd_RecursiveDeref( dd, (DdNode *)vFuncs->pArray[i] );
    Vec_PtrFree( vFuncs );

    // complement the result if necessary
    pNode = Ft_NodeReadLast( vForm );
    bFunc = Cudd_NotCond( bFunc, pNode->fCompl );

    // return the result
    Cudd_Deref( bFunc );
    return bFunc;
}


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

  Synopsis    [Verifies that the factoring is correct.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Ft_FactorVerify( char * pSop, Vec_Int_t * vForm )
{
    DdNode * bFunc1, * bFunc2;
    int RetValue;
    bFunc1 = Ft_ConvertSopToBdd( dd, pSop );    Cudd_Ref( bFunc1 );
    bFunc2 = Ft_ConvertFormToBdd( dd, vForm );  Cudd_Ref( bFunc2 );
//Extra_bddPrint( dd, bFunc1 ); printf("\n");
//Extra_bddPrint( dd, bFunc2 ); printf("\n");
    RetValue = (bFunc1 == bFunc2);
    Cudd_RecursiveDeref( dd, bFunc1 );
    Cudd_RecursiveDeref( dd, bFunc2 );
    return RetValue;
}


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