abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cnfCut.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [cnfCut.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [AIG-to-CNF conversion.]
8 
9  Synopsis []
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - April 28, 2007.]
16 
17  Revision [$Id: cnfCut.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "cnf.h"
22 #include "bool/kit/kit.h"
23 
25 
26 
27 ////////////////////////////////////////////////////////////////////////
28 /// DECLARATIONS ///
29 ////////////////////////////////////////////////////////////////////////
30 
31 ////////////////////////////////////////////////////////////////////////
32 /// FUNCTION DEFINITIONS ///
33 ////////////////////////////////////////////////////////////////////////
34 
35 /**Function*************************************************************
36 
37  Synopsis [Allocates cut of the given size.]
38 
39  Description []
40 
41  SideEffects []
42 
43  SeeAlso []
44 
45 ***********************************************************************/
46 Cnf_Cut_t * Cnf_CutAlloc( Cnf_Man_t * p, int nLeaves )
47 {
48  Cnf_Cut_t * pCut;
49  int nSize = sizeof(Cnf_Cut_t) + sizeof(int) * nLeaves + sizeof(unsigned) * Abc_TruthWordNum(nLeaves);
50  pCut = (Cnf_Cut_t *)Aig_MmFlexEntryFetch( p->pMemCuts, nSize );
51  pCut->nFanins = nLeaves;
52  pCut->nWords = Abc_TruthWordNum(nLeaves);
53  pCut->vIsop[0] = pCut->vIsop[1] = NULL;
54  return pCut;
55 }
56 
57 /**Function*************************************************************
58 
59  Synopsis [Deallocates cut.]
60 
61  Description []
62 
63  SideEffects []
64 
65  SeeAlso []
66 
67 ***********************************************************************/
68 void Cnf_CutFree( Cnf_Cut_t * pCut )
69 {
70  if ( pCut->vIsop[0] )
71  Vec_IntFree( pCut->vIsop[0] );
72  if ( pCut->vIsop[1] )
73  Vec_IntFree( pCut->vIsop[1] );
74 }
75 
76 /**Function*************************************************************
77 
78  Synopsis [Creates cut for the given node.]
79 
80  Description []
81 
82  SideEffects []
83 
84  SeeAlso []
85 
86 ***********************************************************************/
88 {
89  Dar_Cut_t * pCutBest;
90  Cnf_Cut_t * pCut;
91  unsigned * pTruth;
92  assert( Aig_ObjIsNode(pObj) );
93  pCutBest = Dar_ObjBestCut( pObj );
94  assert( pCutBest != NULL );
95  assert( pCutBest->nLeaves <= 4 );
96  pCut = Cnf_CutAlloc( p, pCutBest->nLeaves );
97  memcpy( pCut->pFanins, pCutBest->pLeaves, sizeof(int) * pCutBest->nLeaves );
98  pTruth = Cnf_CutTruth(pCut);
99  *pTruth = (pCutBest->uTruth << 16) | pCutBest->uTruth;
100  pCut->Cost = Cnf_CutSopCost( p, pCutBest );
101  return pCut;
102 }
103 
104 /**Function*************************************************************
105 
106  Synopsis [Deallocates cut.]
107 
108  Description []
109 
110  SideEffects []
111 
112  SeeAlso []
113 
114 ***********************************************************************/
115 void Cnf_CutPrint( Cnf_Cut_t * pCut )
116 {
117  int i;
118  printf( "{" );
119  for ( i = 0; i < pCut->nFanins; i++ )
120  printf( "%d ", pCut->pFanins[i] );
121  printf( " } " );
122 }
123 
124 /**Function*************************************************************
125 
126  Synopsis [Allocates cut of the given size.]
127 
128  Description []
129 
130  SideEffects []
131 
132  SeeAlso []
133 
134 ***********************************************************************/
136 {
137  Aig_Obj_t * pObj;
138  int i;
139  Cnf_CutForEachLeaf( p->pManAig, pCut, pObj, i )
140  {
141  assert( pObj->nRefs > 0 );
142  pObj->nRefs--;
143  }
144 }
145 
146 /**Function*************************************************************
147 
148  Synopsis [Allocates cut of the given size.]
149 
150  Description []
151 
152  SideEffects []
153 
154  SeeAlso []
155 
156 ***********************************************************************/
157 void Cnf_CutRef( Cnf_Man_t * p, Cnf_Cut_t * pCut )
158 {
159  Aig_Obj_t * pObj;
160  int i;
161  Cnf_CutForEachLeaf( p->pManAig, pCut, pObj, i )
162  {
163  pObj->nRefs++;
164  }
165 }
166 
167 /**Function*************************************************************
168 
169  Synopsis [Allocates cut of the given size.]
170 
171  Description []
172 
173  SideEffects []
174 
175  SeeAlso []
176 
177 ***********************************************************************/
178 void Cnf_CutUpdateRefs( Cnf_Man_t * p, Cnf_Cut_t * pCut, Cnf_Cut_t * pCutFan, Cnf_Cut_t * pCutRes )
179 {
180  Cnf_CutDeref( p, pCut );
181  Cnf_CutDeref( p, pCutFan );
182  Cnf_CutRef( p, pCutRes );
183 }
184 
185 /**Function*************************************************************
186 
187  Synopsis [Merges two arrays of integers.]
188 
189  Description [Returns the number of items.]
190 
191  SideEffects []
192 
193  SeeAlso []
194 
195 ***********************************************************************/
196 static inline int Cnf_CutMergeLeaves( Cnf_Cut_t * pCut, Cnf_Cut_t * pCutFan, int * pFanins )
197 {
198  int i, k, nFanins = 0;
199  for ( i = k = 0; i < pCut->nFanins && k < pCutFan->nFanins; )
200  {
201  if ( pCut->pFanins[i] == pCutFan->pFanins[k] )
202  pFanins[nFanins++] = pCut->pFanins[i], i++, k++;
203  else if ( pCut->pFanins[i] < pCutFan->pFanins[k] )
204  pFanins[nFanins++] = pCut->pFanins[i], i++;
205  else
206  pFanins[nFanins++] = pCutFan->pFanins[k], k++;
207  }
208  for ( ; i < pCut->nFanins; i++ )
209  pFanins[nFanins++] = pCut->pFanins[i];
210  for ( ; k < pCutFan->nFanins; k++ )
211  pFanins[nFanins++] = pCutFan->pFanins[k];
212  return nFanins;
213 }
214 
215 /**Function*************************************************************
216 
217  Synopsis [Computes the stretching phase of the cut w.r.t. the merged cut.]
218 
219  Description []
220 
221  SideEffects []
222 
223  SeeAlso []
224 
225 ***********************************************************************/
226 static inline unsigned Cnf_TruthPhase( Cnf_Cut_t * pCut, Cnf_Cut_t * pCut1 )
227 {
228  unsigned uPhase = 0;
229  int i, k;
230  for ( i = k = 0; i < pCut->nFanins; i++ )
231  {
232  if ( k == pCut1->nFanins )
233  break;
234  if ( pCut->pFanins[i] < pCut1->pFanins[k] )
235  continue;
236  assert( pCut->pFanins[i] == pCut1->pFanins[k] );
237  uPhase |= (1 << i);
238  k++;
239  }
240  return uPhase;
241 }
242 
243 /**Function*************************************************************
244 
245  Synopsis [Removes the fanin variable.]
246 
247  Description []
248 
249  SideEffects []
250 
251  SeeAlso []
252 
253 ***********************************************************************/
254 void Cnf_CutRemoveIthVar( Cnf_Cut_t * pCut, int iVar, int iFan )
255 {
256  int i;
257  assert( pCut->pFanins[iVar] == iFan );
258  pCut->nFanins--;
259  for ( i = iVar; i < pCut->nFanins; i++ )
260  pCut->pFanins[i] = pCut->pFanins[i+1];
261 }
262 
263 /**Function*************************************************************
264 
265  Synopsis [Inserts the fanin variable.]
266 
267  Description []
268 
269  SideEffects []
270 
271  SeeAlso []
272 
273 ***********************************************************************/
274 void Cnf_CutInsertIthVar( Cnf_Cut_t * pCut, int iVar, int iFan )
275 {
276  int i;
277  for ( i = pCut->nFanins; i > iVar; i-- )
278  pCut->pFanins[i] = pCut->pFanins[i-1];
279  pCut->pFanins[iVar] = iFan;
280  pCut->nFanins++;
281 }
282 
283 /**Function*************************************************************
284 
285  Synopsis [Merges two cuts.]
286 
287  Description [Returns NULL of the cuts cannot be merged.]
288 
289  SideEffects []
290 
291  SeeAlso []
292 
293 ***********************************************************************/
294 Cnf_Cut_t * Cnf_CutCompose( Cnf_Man_t * p, Cnf_Cut_t * pCut, Cnf_Cut_t * pCutFan, int iFan )
295 {
296  Cnf_Cut_t * pCutRes;
297  static int pFanins[32];
298  unsigned * pTruth, * pTruthFan, * pTruthRes;
299  unsigned * pTop = p->pTruths[0], * pFan = p->pTruths[2], * pTemp = p->pTruths[3];
300  unsigned uPhase, uPhaseFan;
301  int i, iVar, nFanins, RetValue;
302 
303  // make sure the second cut is the fanin of the first
304  for ( iVar = 0; iVar < pCut->nFanins; iVar++ )
305  if ( pCut->pFanins[iVar] == iFan )
306  break;
307  assert( iVar < pCut->nFanins );
308  // remove this variable
309  Cnf_CutRemoveIthVar( pCut, iVar, iFan );
310  // merge leaves of the cuts
311  nFanins = Cnf_CutMergeLeaves( pCut, pCutFan, pFanins );
312  if ( nFanins+1 > p->nMergeLimit )
313  {
314  Cnf_CutInsertIthVar( pCut, iVar, iFan );
315  return NULL;
316  }
317  // create new cut
318  pCutRes = Cnf_CutAlloc( p, nFanins );
319  memcpy( pCutRes->pFanins, pFanins, sizeof(int) * nFanins );
320  assert( pCutRes->nFanins <= pCut->nFanins + pCutFan->nFanins );
321 
322  // derive its truth table
323  // get the truth tables in the composition space
324  pTruth = Cnf_CutTruth(pCut);
325  pTruthFan = Cnf_CutTruth(pCutFan);
326  pTruthRes = Cnf_CutTruth(pCutRes);
327  for ( i = 0; i < 2*pCutRes->nWords; i++ )
328  pTop[i] = pTruth[i % pCut->nWords];
329  for ( i = 0; i < pCutRes->nWords; i++ )
330  pFan[i] = pTruthFan[i % pCutFan->nWords];
331  // move the variable to the end
332  uPhase = Kit_BitMask( pCutRes->nFanins+1 ) & ~(1 << iVar);
333  Kit_TruthShrink( pTemp, pTop, pCutRes->nFanins, pCutRes->nFanins+1, uPhase, 1 );
334  // compute the phases
335  uPhase = Cnf_TruthPhase( pCutRes, pCut ) | (1 << pCutRes->nFanins);
336  uPhaseFan = Cnf_TruthPhase( pCutRes, pCutFan );
337  // permute truth-tables to the common support
338  Kit_TruthStretch( pTemp, pTop, pCut->nFanins+1, pCutRes->nFanins+1, uPhase, 1 );
339  Kit_TruthStretch( pTemp, pFan, pCutFan->nFanins, pCutRes->nFanins, uPhaseFan, 1 );
340  // perform Boolean operation
341  Kit_TruthMux( pTruthRes, pTop, pTop+pCutRes->nWords, pFan, pCutRes->nFanins );
342  // return the cut to its original condition
343  Cnf_CutInsertIthVar( pCut, iVar, iFan );
344  // consider the simple case
345  if ( pCutRes->nFanins < 5 )
346  {
347  pCutRes->Cost = p->pSopSizes[0xFFFF & *pTruthRes] + p->pSopSizes[0xFFFF & ~*pTruthRes];
348  return pCutRes;
349  }
350 
351  // derive ISOP for positive phase
352  RetValue = Kit_TruthIsop( pTruthRes, pCutRes->nFanins, p->vMemory, 0 );
353  pCutRes->vIsop[1] = (RetValue == -1)? NULL : Vec_IntDup( p->vMemory );
354  // derive ISOP for negative phase
355  Kit_TruthNot( pTruthRes, pTruthRes, pCutRes->nFanins );
356  RetValue = Kit_TruthIsop( pTruthRes, pCutRes->nFanins, p->vMemory, 0 );
357  pCutRes->vIsop[0] = (RetValue == -1)? NULL : Vec_IntDup( p->vMemory );
358  Kit_TruthNot( pTruthRes, pTruthRes, pCutRes->nFanins );
359 
360  // compute the cut cost
361  if ( pCutRes->vIsop[0] == NULL || pCutRes->vIsop[1] == NULL )
362  pCutRes->Cost = 127;
363  else if ( Vec_IntSize(pCutRes->vIsop[0]) + Vec_IntSize(pCutRes->vIsop[1]) > 127 )
364  pCutRes->Cost = 127;
365  else
366  pCutRes->Cost = Vec_IntSize(pCutRes->vIsop[0]) + Vec_IntSize(pCutRes->vIsop[1]);
367  return pCutRes;
368 }
369 
370 ////////////////////////////////////////////////////////////////////////
371 /// END OF FILE ///
372 ////////////////////////////////////////////////////////////////////////
373 
374 
376 
void Cnf_CutInsertIthVar(Cnf_Cut_t *pCut, int iVar, int iFan)
Definition: cnfCut.c:274
Cnf_Cut_t * Cnf_CutCompose(Cnf_Man_t *p, Cnf_Cut_t *pCut, Cnf_Cut_t *pCutFan, int iFan)
Definition: cnfCut.c:294
void Cnf_CutUpdateRefs(Cnf_Man_t *p, Cnf_Cut_t *pCut, Cnf_Cut_t *pCutFan, Cnf_Cut_t *pCutRes)
Definition: cnfCut.c:178
static Vec_Int_t * Vec_IntDup(Vec_Int_t *pVec)
Definition: vecInt.h:214
static Llb_Mgr_t * p
Definition: llb3Image.c:950
void Cnf_CutFree(Cnf_Cut_t *pCut)
Definition: cnfCut.c:68
static int Cnf_CutSopCost(Cnf_Man_t *p, Dar_Cut_t *pCut)
Definition: cnf.h:99
unsigned uTruth
Definition: darInt.h:58
char * Aig_MmFlexEntryFetch(Aig_MmFlex_t *p, int nBytes)
Definition: aigMem.c:366
char * memcpy()
static int Abc_TruthWordNum(int nVars)
Definition: abc_global.h:256
void Cnf_CutDeref(Cnf_Man_t *p, Cnf_Cut_t *pCut)
Definition: cnfCut.c:135
void Kit_TruthStretch(unsigned *pOut, unsigned *pIn, int nVars, int nVarsAll, unsigned Phase, int fReturnIn)
Definition: kitTruth.c:166
short nWords
Definition: cnf.h:75
for(p=first;p->value< newval;p=p->next)
char Cost
Definition: cnf.h:74
Vec_Int_t * vIsop[2]
Definition: cnf.h:76
static void Kit_TruthMux(unsigned *pOut, unsigned *pIn0, unsigned *pIn1, unsigned *pCtrl, int nVars)
Definition: kit.h:457
static unsigned * Cnf_CutTruth(Cnf_Cut_t *pCut)
Definition: cnf.h:103
void Cnf_CutRef(Cnf_Man_t *p, Cnf_Cut_t *pCut)
Definition: cnfCut.c:157
static int Aig_ObjIsNode(Aig_Obj_t *pObj)
Definition: aig.h:280
int pLeaves[4]
Definition: darInt.h:63
Definition: cnf.h:71
void Cnf_CutPrint(Cnf_Cut_t *pCut)
Definition: cnfCut.c:115
int Kit_TruthIsop(unsigned *puTruth, int nVars, Vec_Int_t *vMemory, int fTryBoth)
FUNCTION DEFINITIONS ///.
Definition: kitIsop.c:55
int pFanins[0]
Definition: cnf.h:77
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define Cnf_CutForEachLeaf(p, pCut, pLeaf, i)
Definition: cnf.h:121
Definition: aig.h:69
static void Kit_TruthNot(unsigned *pOut, unsigned *pIn, int nVars)
Definition: kit.h:373
static Dar_Cut_t * Dar_ObjBestCut(Aig_Obj_t *pObj)
Definition: cnf.h:97
unsigned nLeaves
Definition: darInt.h:62
ABC_NAMESPACE_IMPL_START Cnf_Cut_t * Cnf_CutAlloc(Cnf_Man_t *p, int nLeaves)
DECLARATIONS ///.
Definition: cnfCut.c:46
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void Kit_TruthShrink(unsigned *pOut, unsigned *pIn, int nVars, int nVarsAll, unsigned Phase, int fReturnIn)
Definition: kitTruth.c:200
static int Cnf_CutMergeLeaves(Cnf_Cut_t *pCut, Cnf_Cut_t *pCutFan, int *pFanins)
Definition: cnfCut.c:196
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
struct Cnf_Cut_t_ Cnf_Cut_t
Definition: cnf.h:53
#define assert(ex)
Definition: util_old.h:213
void Cnf_CutRemoveIthVar(Cnf_Cut_t *pCut, int iVar, int iFan)
Definition: cnfCut.c:254
char nFanins
Definition: cnf.h:73
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
typedefABC_NAMESPACE_HEADER_START struct Cnf_Man_t_ Cnf_Man_t
INCLUDES ///.
Definition: cnf.h:51
Cnf_Cut_t * Cnf_CutCreate(Cnf_Man_t *p, Aig_Obj_t *pObj)
Definition: cnfCut.c:87
unsigned int nRefs
Definition: aig.h:81
static unsigned Cnf_TruthPhase(Cnf_Cut_t *pCut, Cnf_Cut_t *pCut1)
Definition: cnfCut.c:226
static unsigned Kit_BitMask(int nBits)
Definition: kit.h:225