abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
covMinEsop.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [covMinEsop.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Mapping into network of SOPs/ESOPs.]
8 
9  Synopsis [ESOP manipulation.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: covMinEsop.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "covInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 static void Min_EsopRewrite( Min_Man_t * p );
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// FUNCTION DEFINITIONS ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38  Synopsis []
39 
40  Description []
41 
42  SideEffects []
43 
44  SeeAlso []
45 
46 ***********************************************************************/
48 {
49  int nCubesInit, nCubesOld, nIter;
50  if ( p->nCubes < 3 )
51  return;
52  nIter = 0;
53  nCubesInit = p->nCubes;
54  do {
55  nCubesOld = p->nCubes;
56  Min_EsopRewrite( p );
57  nIter++;
58  }
59  while ( 100.0*(nCubesOld - p->nCubes)/nCubesOld > 3.0 );
60 
61 // printf( "%d:%d->%d ", nIter, nCubesInit, p->nCubes );
62 }
63 
64 /**Function*************************************************************
65 
66  Synopsis [Performs one round of rewriting using distance 2 cubes.]
67 
68  Description [The weakness of this procedure is that it tries each cube
69  with only one distance-2 cube. If this pair does not lead to improvement
70  the cube is inserted into the cover anyhow, and we try another pair.
71  A possible improvement would be to try this cube with all distance-2
72  cubes, until an improvement is found, or until all such cubes are tried.]
73 
74  SideEffects []
75 
76  SeeAlso []
77 
78 ***********************************************************************/
80 {
81  Min_Cube_t * pCube, ** ppPrev;
82  Min_Cube_t * pThis, ** ppPrevT;
83  int v00, v01, v10, v11, Var0, Var1, Index, nCubesOld;
84  int nPairs = 0;
85 
86  // insert the bubble before the first cube
87  p->pBubble->pNext = p->ppStore[0];
88  p->ppStore[0] = p->pBubble;
89  p->pBubble->nLits = 0;
90 
91  // go through the cubes
92  while ( 1 )
93  {
94  // get the index of the bubble
95  Index = p->pBubble->nLits;
96 
97  // find the bubble
98  Min_CoverForEachCubePrev( p->ppStore[Index], pCube, ppPrev )
99  if ( pCube == p->pBubble )
100  break;
101  assert( pCube == p->pBubble );
102 
103  // remove the bubble, get the next cube after the bubble
104  *ppPrev = p->pBubble->pNext;
105  pCube = p->pBubble->pNext;
106  if ( pCube == NULL )
107  for ( Index++; Index <= p->nVars; Index++ )
108  if ( p->ppStore[Index] )
109  {
110  ppPrev = &(p->ppStore[Index]);
111  pCube = p->ppStore[Index];
112  break;
113  }
114  // stop if there is no more cubes
115  if ( pCube == NULL )
116  break;
117 
118  // find the first dist2 cube
119  Min_CoverForEachCubePrev( pCube->pNext, pThis, ppPrevT )
120  if ( Min_CubesDistTwo( pCube, pThis, &Var0, &Var1 ) )
121  break;
122  if ( pThis == NULL && Index < p->nVars )
123  Min_CoverForEachCubePrev( p->ppStore[Index+1], pThis, ppPrevT )
124  if ( Min_CubesDistTwo( pCube, pThis, &Var0, &Var1 ) )
125  break;
126  if ( pThis == NULL && Index < p->nVars - 1 )
127  Min_CoverForEachCubePrev( p->ppStore[Index+2], pThis, ppPrevT )
128  if ( Min_CubesDistTwo( pCube, pThis, &Var0, &Var1 ) )
129  break;
130  // continue if there is no dist2 cube
131  if ( pThis == NULL )
132  {
133  // insert the bubble after the cube
134  p->pBubble->pNext = pCube->pNext;
135  pCube->pNext = p->pBubble;
136  p->pBubble->nLits = pCube->nLits;
137  continue;
138  }
139  nPairs++;
140 
141  // remove the cubes, insert the bubble instead of pCube
142  *ppPrevT = pThis->pNext;
143  *ppPrev = p->pBubble;
144  p->pBubble->pNext = pCube->pNext;
145  p->pBubble->nLits = pCube->nLits;
146  p->nCubes -= 2;
147 
148  // Exorlink-2:
149  // A{v00} B{v01} + A{v10} B{v11} =
150  // A{v00+v10} B{v01} + A{v10} B{v01+v11} =
151  // A{v00} B{v01+v11} + A{v00+v10} B{v11}
152 
153  // save the dist2 parameters
154  v00 = Min_CubeGetVar( pCube, Var0 );
155  v01 = Min_CubeGetVar( pCube, Var1 );
156  v10 = Min_CubeGetVar( pThis, Var0 );
157  v11 = Min_CubeGetVar( pThis, Var1 );
158 //printf( "\n" );
159 //Min_CubeWrite( stdout, pCube );
160 //Min_CubeWrite( stdout, pThis );
161 
162  // derive the first pair of resulting cubes
163  Min_CubeXorVar( pCube, Var0, v10 );
164  pCube->nLits -= (v00 != 3);
165  pCube->nLits += ((v00 ^ v10) != 3);
166  Min_CubeXorVar( pThis, Var1, v01 );
167  pThis->nLits -= (v11 != 3);
168  pThis->nLits += ((v01 ^ v11) != 3);
169 
170  // add the cubes
171  nCubesOld = p->nCubes;
172  Min_EsopAddCube( p, pCube );
173  Min_EsopAddCube( p, pThis );
174  // check if the cubes were absorbed
175  if ( p->nCubes < nCubesOld + 2 )
176  continue;
177 
178  // pull out both cubes
179  assert( pThis == p->ppStore[pThis->nLits] );
180  p->ppStore[pThis->nLits] = pThis->pNext;
181  assert( pCube == p->ppStore[pCube->nLits] );
182  p->ppStore[pCube->nLits] = pCube->pNext;
183  p->nCubes -= 2;
184 
185  // derive the second pair of resulting cubes
186  Min_CubeXorVar( pCube, Var0, v10 );
187  pCube->nLits -= ((v00 ^ v10) != 3);
188  pCube->nLits += (v00 != 3);
189  Min_CubeXorVar( pCube, Var1, v11 );
190  pCube->nLits -= (v01 != 3);
191  pCube->nLits += ((v01 ^ v11) != 3);
192 
193  Min_CubeXorVar( pThis, Var0, v00 );
194  pThis->nLits -= (v10 != 3);
195  pThis->nLits += ((v00 ^ v10) != 3);
196  Min_CubeXorVar( pThis, Var1, v01 );
197  pThis->nLits -= ((v01 ^ v11) != 3);
198  pThis->nLits += (v11 != 3);
199 
200  // add them anyhow
201  Min_EsopAddCube( p, pCube );
202  Min_EsopAddCube( p, pThis );
203  }
204 // printf( "Pairs = %d ", nPairs );
205 }
206 
207 /**Function*************************************************************
208 
209  Synopsis [Adds the cube to storage.]
210 
211  Description [Returns 0 if the cube is added or removed. Returns 1
212  if the cube is glued with some other cube and has to be added again.
213  Do not forget to clean the storage!]
214 
215  SideEffects []
216 
217  SeeAlso []
218 
219 ***********************************************************************/
221 {
222  Min_Cube_t * pThis, ** ppPrev;
223  // try to find the identical cube
224  Min_CoverForEachCubePrev( p->ppStore[pCube->nLits], pThis, ppPrev )
225  {
226  if ( Min_CubesAreEqual( pCube, pThis ) )
227  {
228  *ppPrev = pThis->pNext;
229  Min_CubeRecycle( p, pCube );
230  Min_CubeRecycle( p, pThis );
231  p->nCubes--;
232  return 0;
233  }
234  }
235  // find a distance-1 cube if it exists
236  if ( pCube->nLits < pCube->nVars )
237  Min_CoverForEachCubePrev( p->ppStore[pCube->nLits+1], pThis, ppPrev )
238  {
239  if ( Min_CubesDistOne( pCube, pThis, p->pTemp ) )
240  {
241  *ppPrev = pThis->pNext;
242  Min_CubesTransform( pCube, pThis, p->pTemp );
243  pCube->nLits++;
244  Min_CubeRecycle( p, pThis );
245  p->nCubes--;
246  return 1;
247  }
248  }
249  Min_CoverForEachCubePrev( p->ppStore[pCube->nLits], pThis, ppPrev )
250  {
251  if ( Min_CubesDistOne( pCube, pThis, p->pTemp ) )
252  {
253  *ppPrev = pThis->pNext;
254  Min_CubesTransform( pCube, pThis, p->pTemp );
255  pCube->nLits--;
256  Min_CubeRecycle( p, pThis );
257  p->nCubes--;
258  return 1;
259  }
260  }
261  if ( pCube->nLits > 0 )
262  Min_CoverForEachCubePrev( p->ppStore[pCube->nLits-1], pThis, ppPrev )
263  {
264  if ( Min_CubesDistOne( pCube, pThis, p->pTemp ) )
265  {
266  *ppPrev = pThis->pNext;
267  Min_CubesTransform( pCube, pThis, p->pTemp );
268  Min_CubeRecycle( p, pThis );
269  p->nCubes--;
270  return 1;
271  }
272  }
273  // add the cube
274  pCube->pNext = p->ppStore[pCube->nLits];
275  p->ppStore[pCube->nLits] = pCube;
276  p->nCubes++;
277  return 0;
278 }
279 
280 /**Function*************************************************************
281 
282  Synopsis [Adds the cube to storage.]
283 
284  Description []
285 
286  SideEffects []
287 
288  SeeAlso []
289 
290 ***********************************************************************/
292 {
293  assert( pCube != p->pBubble );
294  assert( (int)pCube->nLits == Min_CubeCountLits(pCube) );
295  while ( Min_EsopAddCubeInt( p, pCube ) );
296 }
297 
298 ////////////////////////////////////////////////////////////////////////
299 /// END OF FILE ///
300 ////////////////////////////////////////////////////////////////////////
301 
302 
304 
static void Min_CubeRecycle(Min_Man_t *p, Min_Cube_t *pCube)
Definition: covInt.h:189
unsigned nVars
Definition: covInt.h:57
int Min_EsopAddCubeInt(Min_Man_t *p, Min_Cube_t *pCube)
Definition: covMinEsop.c:220
static void Min_CubeXorVar(Min_Cube_t *p, int Var, int Value)
Definition: covInt.h:87
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static int Min_CubeCountLits(Min_Cube_t *pCube)
Definition: covInt.h:224
Min_Cube_t * pNext
Definition: covInt.h:56
static int Min_CubesDistOne(Min_Cube_t *pCube0, Min_Cube_t *pCube1, Min_Cube_t *pTemp)
Definition: covInt.h:349
static int Min_CubesDistTwo(Min_Cube_t *pCube0, Min_Cube_t *pCube1, int *pVar0, int *pVar1)
Definition: covInt.h:390
static int Min_CubeGetVar(Min_Cube_t *p, int Var)
Definition: covInt.h:86
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static void Min_CubesTransform(Min_Cube_t *pCube, Min_Cube_t *pDist, Min_Cube_t *pMask)
Definition: covInt.h:542
unsigned nLits
Definition: covInt.h:59
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define Min_CoverForEachCubePrev(pCover, pCube, ppPrev)
Definition: covInt.h:75
static int Min_CubesAreEqual(Min_Cube_t *pCube0, Min_Cube_t *pCube1)
Definition: covInt.h:502
static ABC_NAMESPACE_IMPL_START void Min_EsopRewrite(Min_Man_t *p)
DECLARATIONS ///.
Definition: covMinEsop.c:79
#define assert(ex)
Definition: util_old.h:213
void Min_EsopMinimize(Min_Man_t *p)
FUNCTION DEFINITIONS ///.
Definition: covMinEsop.c:47
void Min_EsopAddCube(Min_Man_t *p, Min_Cube_t *pCube)
Definition: covMinEsop.c:291
typedefABC_NAMESPACE_HEADER_START struct Min_Man_t_ Min_Man_t
DECLARATIONS ///.
Definition: covInt.h:34