decUtil.c 4.69 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**CFile****************************************************************

  FileName    [decUtil.c]

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

  Synopsis    [Decomposition unitilies.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: decUtil.c,v 1.1 2003/05/22 19:20:05 alanmi Exp $]

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

19 20
#include "base/abc/abc.h"
#include "misc/extra/extraBdd.h"
Alan Mishchenko committed
21 22
#include "dec.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
31
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Converts graph to BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Dec_GraphDeriveBdd( DdManager * dd, Dec_Graph_t * pGraph )
{
    DdNode * bFunc, * bFunc0, * bFunc1;
Alan Mishchenko committed
48
    Dec_Node_t * pNode = NULL; // Suppress "might be used uninitialized"
Alan Mishchenko committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    int i;

    // sanity checks
    assert( Dec_GraphLeaveNum(pGraph) >= 0 );
    assert( Dec_GraphLeaveNum(pGraph) <= pGraph->nSize );

    // check for constant function
    if ( Dec_GraphIsConst(pGraph) )
        return Cudd_NotCond( b1, Dec_GraphIsComplement(pGraph) );
    // check for a literal
    if ( Dec_GraphIsVar(pGraph) )
        return Cudd_NotCond( Cudd_bddIthVar(dd, Dec_GraphVarInt(pGraph)), Dec_GraphIsComplement(pGraph) );

    // assign the elementary variables
    Dec_GraphForEachLeaf( pGraph, pNode, i )
        pNode->pFunc = Cudd_bddIthVar( dd, i );

    // compute the function for each internal node
    Dec_GraphForEachNode( pGraph, pNode, i )
    {
        bFunc0 = Cudd_NotCond( Dec_GraphNode(pGraph, pNode->eEdge0.Node)->pFunc, pNode->eEdge0.fCompl ); 
        bFunc1 = Cudd_NotCond( Dec_GraphNode(pGraph, pNode->eEdge1.Node)->pFunc, pNode->eEdge1.fCompl ); 
71
        pNode->pFunc = Cudd_bddAnd( dd, bFunc0, bFunc1 );   Cudd_Ref( (DdNode *)pNode->pFunc );
Alan Mishchenko committed
72 73 74
    }

    // deref the intermediate results
75
    bFunc = (DdNode *)pNode->pFunc;   Cudd_Ref( bFunc );
Alan Mishchenko committed
76
    Dec_GraphForEachNode( pGraph, pNode, i )
77
        Cudd_RecursiveDeref( dd, (DdNode *)pNode->pFunc );
Alan Mishchenko committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    Cudd_Deref( bFunc );

    // complement the result if necessary
    return Cudd_NotCond( bFunc, Dec_GraphIsComplement(pGraph) );
}

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

  Synopsis    [Derives the truth table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
unsigned Dec_GraphDeriveTruth( Dec_Graph_t * pGraph )
{
    unsigned uTruths[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
Alan Mishchenko committed
98 99
    unsigned uTruth = 0; // Suppress "might be used uninitialized"
    unsigned uTruth0, uTruth1;
Alan Mishchenko committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    Dec_Node_t * pNode;
    int i;

    // sanity checks
    assert( Dec_GraphLeaveNum(pGraph) >= 0 );
    assert( Dec_GraphLeaveNum(pGraph) <= pGraph->nSize );
    assert( Dec_GraphLeaveNum(pGraph) <= 5 );

    // check for constant function
    if ( Dec_GraphIsConst(pGraph) )
        return Dec_GraphIsComplement(pGraph)? 0 : ~((unsigned)0);
    // check for a literal
    if ( Dec_GraphIsVar(pGraph) )
        return Dec_GraphIsComplement(pGraph)? ~uTruths[Dec_GraphVarInt(pGraph)] : uTruths[Dec_GraphVarInt(pGraph)];

    // assign the elementary variables
    Dec_GraphForEachLeaf( pGraph, pNode, i )
Alan Mishchenko committed
117
        pNode->pFunc = (void *)(ABC_PTRUINT_T)uTruths[i];
Alan Mishchenko committed
118 119 120 121

    // compute the function for each internal node
    Dec_GraphForEachNode( pGraph, pNode, i )
    {
Alan Mishchenko committed
122 123
        uTruth0 = (unsigned)(ABC_PTRUINT_T)Dec_GraphNode(pGraph, pNode->eEdge0.Node)->pFunc;
        uTruth1 = (unsigned)(ABC_PTRUINT_T)Dec_GraphNode(pGraph, pNode->eEdge1.Node)->pFunc;
Alan Mishchenko committed
124 125 126
        uTruth0 = pNode->eEdge0.fCompl? ~uTruth0 : uTruth0;
        uTruth1 = pNode->eEdge1.fCompl? ~uTruth1 : uTruth1;
        uTruth = uTruth0 & uTruth1;
Alan Mishchenko committed
127
        pNode->pFunc = (void *)(ABC_PTRUINT_T)uTruth;
Alan Mishchenko committed
128 129 130 131 132 133 134 135 136 137 138 139
    }

    // complement the result if necessary
    return Dec_GraphIsComplement(pGraph)? ~uTruth : uTruth;
}


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


140 141
ABC_NAMESPACE_IMPL_END