abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cutExpand.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [cutExpand.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [K-feasible cut computation package.]
8 
9  Synopsis [Computes the truth table of the cut after expansion.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: cutExpand.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "cutInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 #define CUT_CELL_MVAR 9
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// FUNCTION DEFINITIONS ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38  Synopsis [Computes the stretching phase of the cut w.r.t. the merged cut.]
39 
40  Description []
41 
42  SideEffects []
43 
44  SeeAlso []
45 
46 ***********************************************************************/
47 static inline unsigned Cut_TruthPhase( Cut_Cut_t * pCut, Cut_Cut_t * pCut1 )
48 {
49  unsigned uPhase = 0;
50  int i, k;
51  for ( i = k = 0; i < (int)pCut->nLeaves; i++ )
52  {
53  if ( k == (int)pCut1->nLeaves )
54  break;
55  if ( pCut->pLeaves[i] < pCut1->pLeaves[k] )
56  continue;
57  assert( pCut->pLeaves[i] == pCut1->pLeaves[k] );
58  uPhase |= (1 << i);
59  k++;
60  }
61  return uPhase;
62 }
63 
64 /**Function*************************************************************
65 
66  Synopsis [Computes the truth table of the composition of cuts.]
67 
68  Description [Inputs are:
69  - a factor cut (truth table is stored inside)
70  - a node in the factor cut
71  - a tree cut to be substituted (truth table is stored inside)
72  - the resulting cut (truth table will be filled in).
73  Note that all cuts, including the resulting one, should be already
74  computed and the nodes should be stored in the ascending order.]
75 
76  SideEffects []
77 
78  SeeAlso []
79 
80 ***********************************************************************/
81 void Cut_TruthCompose( Cut_Cut_t * pCutF, int Node, Cut_Cut_t * pCutT, Cut_Cut_t * pCutRes )
82 {
83  static unsigned uCof0[1<<(CUT_CELL_MVAR-5)];
84  static unsigned uCof1[1<<(CUT_CELL_MVAR-5)];
85  static unsigned uTemp[1<<(CUT_CELL_MVAR-5)];
86  unsigned * pIn, * pOut, * pTemp;
87  unsigned uPhase;
88  int NodeIndex, i, k;
89 
90  // sanity checks
91  assert( pCutF->nVarsMax == pCutT->nVarsMax );
92  assert( pCutF->nVarsMax == pCutRes->nVarsMax );
93  assert( pCutF->nVarsMax <= CUT_CELL_MVAR );
94  // the factor cut (pCutF) should have its nodes sorted in the ascending order
95  assert( pCutF->nLeaves <= pCutF->nVarsMax );
96  for ( i = 0; i < (int)pCutF->nLeaves - 1; i++ )
97  assert( pCutF->pLeaves[i] < pCutF->pLeaves[i+1] );
98  // the tree cut (pCutT) should have its nodes sorted in the ascending order
99  assert( pCutT->nLeaves <= pCutT->nVarsMax );
100  for ( i = 0; i < (int)pCutT->nLeaves - 1; i++ )
101  assert( pCutT->pLeaves[i] < pCutT->pLeaves[i+1] );
102  // the resulting cut (pCutRes) should have its nodes sorted in the ascending order
103  assert( pCutRes->nLeaves <= pCutRes->nVarsMax );
104  for ( i = 0; i < (int)pCutRes->nLeaves - 1; i++ )
105  assert( pCutRes->pLeaves[i] < pCutRes->pLeaves[i+1] );
106  // make sure that every node in pCutF (except Node) appears in pCutRes
107  for ( i = 0; i < (int)pCutF->nLeaves; i++ )
108  {
109  if ( pCutF->pLeaves[i] == Node )
110  continue;
111  for ( k = 0; k < (int)pCutRes->nLeaves; k++ )
112  if ( pCutF->pLeaves[i] == pCutRes->pLeaves[k] )
113  break;
114  assert( k < (int)pCutRes->nLeaves ); // node i from pCutF is not found in pCutRes!!!
115  }
116  // make sure that every node in pCutT appears in pCutRes
117  for ( i = 0; i < (int)pCutT->nLeaves; i++ )
118  {
119  for ( k = 0; k < (int)pCutRes->nLeaves; k++ )
120  if ( pCutT->pLeaves[i] == pCutRes->pLeaves[k] )
121  break;
122  assert( k < (int)pCutRes->nLeaves ); // node i from pCutT is not found in pCutRes!!!
123  }
124 
125 
126  // find the index of the given node in the factor cut
127  NodeIndex = -1;
128  for ( NodeIndex = 0; NodeIndex < (int)pCutF->nLeaves; NodeIndex++ )
129  if ( pCutF->pLeaves[NodeIndex] == Node )
130  break;
131  assert( NodeIndex >= 0 ); // Node should be in pCutF
132 
133  // copy the truth table
134  Extra_TruthCopy( uTemp, Cut_CutReadTruth(pCutF), pCutF->nLeaves );
135 
136  // bubble-move the NodeIndex variable to be the last one (the most significant one)
137  pIn = uTemp; pOut = uCof0; // uCof0 is used for temporary storage here
138  for ( i = NodeIndex; i < (int)pCutF->nLeaves - 1; i++ )
139  {
140  Extra_TruthSwapAdjacentVars( pOut, pIn, pCutF->nLeaves, i );
141  pTemp = pIn; pIn = pOut; pOut = pTemp;
142  }
143  if ( (pCutF->nLeaves - 1 - NodeIndex) & 1 )
144  Extra_TruthCopy( pOut, pIn, pCutF->nLeaves );
145  // the result of stretching is in uTemp
146 
147  // cofactor the factor cut with respect to the node
148  Extra_TruthCopy( uCof0, uTemp, pCutF->nLeaves );
149  Extra_TruthCofactor0( uCof0, pCutF->nLeaves, pCutF->nLeaves-1 );
150  Extra_TruthCopy( uCof1, uTemp, pCutF->nLeaves );
151  Extra_TruthCofactor1( uCof1, pCutF->nLeaves, pCutF->nLeaves-1 );
152 
153  // temporarily shrink the factor cut's variables by removing Node
154  for ( i = NodeIndex; i < (int)pCutF->nLeaves - 1; i++ )
155  pCutF->pLeaves[i] = pCutF->pLeaves[i+1];
156  pCutF->nLeaves--;
157 
158  // spread out the cofactors' truth tables to the same var order as the resulting cut
159  uPhase = Cut_TruthPhase(pCutRes, pCutF);
160  assert( Extra_WordCountOnes(uPhase) == (int)pCutF->nLeaves );
161  Extra_TruthStretch( uTemp, uCof0, pCutF->nLeaves, pCutF->nVarsMax, uPhase );
162  Extra_TruthCopy( uCof0, uTemp, pCutF->nVarsMax );
163  Extra_TruthStretch( uTemp, uCof1, pCutF->nLeaves, pCutF->nVarsMax, uPhase );
164  Extra_TruthCopy( uCof1, uTemp, pCutF->nVarsMax );
165 
166  // spread out the tree cut's truth table to the same var order as the resulting cut
167  uPhase = Cut_TruthPhase(pCutRes, pCutT);
168  assert( Extra_WordCountOnes(uPhase) == (int)pCutT->nLeaves );
169  Extra_TruthStretch( uTemp, Cut_CutReadTruth(pCutT), pCutT->nLeaves, pCutT->nVarsMax, uPhase );
170 
171  // create the resulting truth table
172  pTemp = Cut_CutReadTruth(pCutRes);
173  for ( i = Extra_TruthWordNum(pCutRes->nLeaves)-1; i >= 0; i-- )
174  pTemp[i] = (uCof0[i] & ~uTemp[i]) | (uCof1[i] & uTemp[i]);
175 
176  // undo the removal of the node from the cut
177  for ( i = (int)pCutF->nLeaves - 1; i >= NodeIndex; --i )
178  pCutF->pLeaves[i+1] = pCutF->pLeaves[i];
179  pCutF->pLeaves[NodeIndex] = Node;
180  pCutF->nLeaves++;
181 }
182 
183 ////////////////////////////////////////////////////////////////////////
184 /// END OF FILE ///
185 ////////////////////////////////////////////////////////////////////////
186 
187 
189 
void Cut_TruthCompose(Cut_Cut_t *pCutF, int Node, Cut_Cut_t *pCutT, Cut_Cut_t *pCutRes)
Definition: cutExpand.c:81
static unsigned Cut_TruthPhase(Cut_Cut_t *pCut, Cut_Cut_t *pCut1)
FUNCTION DEFINITIONS ///.
Definition: cutExpand.c:47
static unsigned * Cut_CutReadTruth(Cut_Cut_t *p)
Definition: cut.h:94
#define CUT_CELL_MVAR
DECLARATIONS ///.
Definition: cutExpand.c:30
void Extra_TruthCofactor1(unsigned *pTruth, int nVars, int iVar)
int pLeaves[0]
Definition: cut.h:89
unsigned nLeaves
Definition: cut.h:84
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Extra_TruthStretch(unsigned *pOut, unsigned *pIn, int nVars, int nVarsAll, unsigned Phase)
static int Extra_TruthWordNum(int nVars)
Definition: extra.h:249
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void Extra_TruthSwapAdjacentVars(unsigned *pOut, unsigned *pIn, int nVars, int Start)
static int Extra_WordCountOnes(unsigned uWord)
Definition: extra.h:255
void Extra_TruthCofactor0(unsigned *pTruth, int nVars, int iVar)
#define assert(ex)
Definition: util_old.h:213
unsigned nVarsMax
Definition: cut.h:83
static void Extra_TruthCopy(unsigned *pOut, unsigned *pIn, int nVars)
Definition: extra.h:302