abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
kitFactor.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [kitFactor.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Computation kit.]
8 
9  Synopsis [Algebraic factoring.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - Dec 6, 2006.]
16 
17  Revision [$Id: kitFactor.c,v 1.00 2006/12/06 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "kit.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 // factoring fails if intermediate memory usage exceed this limit
31 #define KIT_FACTOR_MEM_LIMIT (1<<20)
32 
33 static Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory );
34 static Kit_Edge_t Kit_SopFactorLF_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, Kit_Sop_t * cSimple, int nLits, Vec_Int_t * vMemory );
35 static Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits );
36 static Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits );
37 
38 extern int Kit_SopFactorVerify( Vec_Int_t * cSop, Kit_Graph_t * pFForm, int nVars );
39 
40 ////////////////////////////////////////////////////////////////////////
41 /// FUNCTION DEFINITIONS ///
42 ////////////////////////////////////////////////////////////////////////
43 
44 /**Function*************************************************************
45 
46  Synopsis [Factors the cover.]
47 
48  Description []
49 
50  SideEffects []
51 
52  SeeAlso []
53 
54 ***********************************************************************/
55 Kit_Graph_t * Kit_SopFactor( Vec_Int_t * vCover, int fCompl, int nVars, Vec_Int_t * vMemory )
56 {
57  Kit_Sop_t Sop, * cSop = &Sop;
58  Kit_Graph_t * pFForm;
59  Kit_Edge_t eRoot;
60 // int nCubes;
61 
62  // works for up to 15 variables because division procedure
63  // used the last bit for marking the cubes going to the remainder
64  assert( nVars < 16 );
65 
66  // check for trivial functions
67  if ( Vec_IntSize(vCover) == 0 )
68  return Kit_GraphCreateConst0();
69  if ( Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0 )
70  return Kit_GraphCreateConst1();
71 
72  // prepare memory manager
73 // Vec_IntClear( vMemory );
75 
76  // perform CST
77  Kit_SopCreateInverse( cSop, vCover, 2 * nVars, vMemory ); // CST
78 
79  // start the factored form
80  pFForm = Kit_GraphCreate( nVars );
81  // factor the cover
82  eRoot = Kit_SopFactor_rec( pFForm, cSop, 2 * nVars, vMemory );
83  // finalize the factored form
84  Kit_GraphSetRoot( pFForm, eRoot );
85  if ( fCompl )
86  Kit_GraphComplement( pFForm );
87 
88  // verify the factored form
89 // nCubes = Vec_IntSize(vCover);
90 // Vec_IntShrink( vCover, nCubes );
91 // if ( !Kit_SopFactorVerify( vCover, pFForm, nVars ) )
92 // printf( "Verification has failed.\n" );
93  return pFForm;
94 }
95 
96 /**Function*************************************************************
97 
98  Synopsis [Recursive factoring procedure.]
99 
100  Description [For the pseudo-code, see Hachtel/Somenzi,
101  Logic synthesis and verification algorithms, Kluwer, 1996, p. 432.]
102 
103  SideEffects []
104 
105  SeeAlso []
106 
107 ***********************************************************************/
108 Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory )
109 {
110  Kit_Sop_t Div, Quo, Rem, Com;
111  Kit_Sop_t * cDiv = &Div, * cQuo = &Quo, * cRem = &Rem, * cCom = &Com;
112  Kit_Edge_t eNodeDiv, eNodeQuo, eNodeRem, eNodeAnd;
113 
114  // make sure the cover contains some cubes
115  assert( Kit_SopCubeNum(cSop) > 0 );
116 
117  // get the divisor
118  if ( !Kit_SopDivisor(cDiv, cSop, nLits, vMemory) )
119  return Kit_SopFactorTrivial( pFForm, cSop, nLits );
120 
121  // divide the cover by the divisor
122  Kit_SopDivideInternal( cSop, cDiv, cQuo, cRem, vMemory );
123 
124  // check the trivial case
125  assert( Kit_SopCubeNum(cQuo) > 0 );
126  if ( Kit_SopCubeNum(cQuo) == 1 )
127  return Kit_SopFactorLF_rec( pFForm, cSop, cQuo, nLits, vMemory );
128 
129  // make the quotient cube ABC_FREE
130  Kit_SopMakeCubeFree( cQuo );
131 
132  // divide the cover by the quotient
133  Kit_SopDivideInternal( cSop, cQuo, cDiv, cRem, vMemory );
134 
135  // check the trivial case
136  if ( Kit_SopIsCubeFree( cDiv ) )
137  {
138  eNodeDiv = Kit_SopFactor_rec( pFForm, cDiv, nLits, vMemory );
139  eNodeQuo = Kit_SopFactor_rec( pFForm, cQuo, nLits, vMemory );
140  eNodeAnd = Kit_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
141  if ( Kit_SopCubeNum(cRem) == 0 )
142  return eNodeAnd;
143  eNodeRem = Kit_SopFactor_rec( pFForm, cRem, nLits, vMemory );
144  return Kit_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
145  }
146 
147  // get the common cube
148  Kit_SopCommonCubeCover( cCom, cDiv, vMemory );
149 
150  // solve the simple problem
151  return Kit_SopFactorLF_rec( pFForm, cSop, cCom, nLits, vMemory );
152 }
153 
154 
155 /**Function*************************************************************
156 
157  Synopsis [Internal recursive factoring procedure for the leaf case.]
158 
159  Description []
160 
161  SideEffects []
162 
163  SeeAlso []
164 
165 ***********************************************************************/
166 Kit_Edge_t Kit_SopFactorLF_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, Kit_Sop_t * cSimple, int nLits, Vec_Int_t * vMemory )
167 {
168  Kit_Sop_t Div, Quo, Rem;
169  Kit_Sop_t * cDiv = &Div, * cQuo = &Quo, * cRem = &Rem;
170  Kit_Edge_t eNodeDiv, eNodeQuo, eNodeRem, eNodeAnd;
171  assert( Kit_SopCubeNum(cSimple) == 1 );
172  // get the most often occurring literal
173  Kit_SopBestLiteralCover( cDiv, cSop, Kit_SopCube(cSimple, 0), nLits, vMemory );
174  // divide the cover by the literal
175  Kit_SopDivideByCube( cSop, cDiv, cQuo, cRem, vMemory );
176  // get the node pointer for the literal
177  eNodeDiv = Kit_SopFactorTrivialCube( pFForm, Kit_SopCube(cDiv, 0), nLits );
178  // factor the quotient and remainder
179  eNodeQuo = Kit_SopFactor_rec( pFForm, cQuo, nLits, vMemory );
180  eNodeAnd = Kit_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
181  if ( Kit_SopCubeNum(cRem) == 0 )
182  return eNodeAnd;
183  eNodeRem = Kit_SopFactor_rec( pFForm, cRem, nLits, vMemory );
184  return Kit_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
185 }
186 
187 
188 /**Function*************************************************************
189 
190  Synopsis [Factoring cube.]
191 
192  Description []
193 
194  SideEffects []
195 
196  SeeAlso []
197 
198 ***********************************************************************/
199 Kit_Edge_t Kit_SopFactorTrivialCube_rec( Kit_Graph_t * pFForm, unsigned uCube, int nStart, int nFinish )
200 {
201  Kit_Edge_t eNode1, eNode2;
202  int i, iLit = -1, nLits, nLits1, nLits2;
203  assert( uCube );
204  // count the number of literals in this interval
205  nLits = 0;
206  for ( i = nStart; i < nFinish; i++ )
207  if ( Kit_CubeHasLit(uCube, i) )
208  {
209  iLit = i;
210  nLits++;
211  }
212  assert( iLit != -1 );
213  // quit if there is only one literal
214  if ( nLits == 1 )
215  return Kit_EdgeCreate( iLit/2, iLit%2 ); // CST
216  // split the literals into two parts
217  nLits1 = nLits/2;
218  nLits2 = nLits - nLits1;
219 // nLits2 = nLits/2;
220 // nLits1 = nLits - nLits2;
221  // find the splitting point
222  nLits = 0;
223  for ( i = nStart; i < nFinish; i++ )
224  if ( Kit_CubeHasLit(uCube, i) )
225  {
226  if ( nLits == nLits1 )
227  break;
228  nLits++;
229  }
230  // recursively construct the tree for the parts
231  eNode1 = Kit_SopFactorTrivialCube_rec( pFForm, uCube, nStart, i );
232  eNode2 = Kit_SopFactorTrivialCube_rec( pFForm, uCube, i, nFinish );
233  return Kit_GraphAddNodeAnd( pFForm, eNode1, eNode2 );
234 }
235 
236 /**Function*************************************************************
237 
238  Synopsis [Factoring cube.]
239 
240  Description []
241 
242  SideEffects []
243 
244  SeeAlso []
245 
246 ***********************************************************************/
247 Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits )
248 {
249  return Kit_SopFactorTrivialCube_rec( pFForm, uCube, 0, nLits );
250 }
251 
252 /**Function*************************************************************
253 
254  Synopsis [Factoring SOP.]
255 
256  Description []
257 
258  SideEffects []
259 
260  SeeAlso []
261 
262 ***********************************************************************/
263 Kit_Edge_t Kit_SopFactorTrivial_rec( Kit_Graph_t * pFForm, unsigned * pCubes, int nCubes, int nLits )
264 {
265  Kit_Edge_t eNode1, eNode2;
266  int nCubes1, nCubes2;
267  if ( nCubes == 1 )
268  return Kit_SopFactorTrivialCube_rec( pFForm, pCubes[0], 0, nLits );
269  // split the cubes into two parts
270  nCubes1 = nCubes/2;
271  nCubes2 = nCubes - nCubes1;
272 // nCubes2 = nCubes/2;
273 // nCubes1 = nCubes - nCubes2;
274  // recursively construct the tree for the parts
275  eNode1 = Kit_SopFactorTrivial_rec( pFForm, pCubes, nCubes1, nLits );
276  eNode2 = Kit_SopFactorTrivial_rec( pFForm, pCubes + nCubes1, nCubes2, nLits );
277  return Kit_GraphAddNodeOr( pFForm, eNode1, eNode2 );
278 }
279 
280 /**Function*************************************************************
281 
282  Synopsis [Factoring the cover, which has no algebraic divisors.]
283 
284  Description []
285 
286  SideEffects []
287 
288  SeeAlso []
289 
290 ***********************************************************************/
291 Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits )
292 {
293  return Kit_SopFactorTrivial_rec( pFForm, cSop->pCubes, cSop->nCubes, nLits );
294 }
295 
296 
297 /**Function*************************************************************
298 
299  Synopsis [Testing procedure for the factoring code.]
300 
301  Description []
302 
303  SideEffects []
304 
305  SeeAlso []
306 
307 ***********************************************************************/
308 void Kit_FactorTest( unsigned * pTruth, int nVars )
309 {
310  Vec_Int_t * vCover, * vMemory;
311  Kit_Graph_t * pGraph;
312 // unsigned uTruthRes;
313  int RetValue;
314 
315  // derive SOP
316  vCover = Vec_IntAlloc( 0 );
317  RetValue = Kit_TruthIsop( pTruth, nVars, vCover, 0 );
318  assert( RetValue == 0 );
319 
320  // derive factored form
321  vMemory = Vec_IntAlloc( 0 );
322  pGraph = Kit_SopFactor( vCover, 0, nVars, vMemory );
323 /*
324  // derive truth table
325  assert( nVars <= 5 );
326  uTruthRes = Kit_GraphToTruth( pGraph );
327  if ( uTruthRes != pTruth[0] )
328  printf( "Verification failed!" );
329 */
330  printf( "Vars = %2d. Cubes = %3d. FFNodes = %3d. FF_memory = %3d.\n",
331  nVars, Vec_IntSize(vCover), Kit_GraphNodeNum(pGraph), Vec_IntSize(vMemory) );
332 
333  Vec_IntFree( vMemory );
334  Vec_IntFree( vCover );
335  Kit_GraphFree( pGraph );
336 }
337 
338 ////////////////////////////////////////////////////////////////////////
339 /// END OF FILE ///
340 ////////////////////////////////////////////////////////////////////////
341 
342 
344 
void Kit_SopBestLiteralCover(Kit_Sop_t *cResult, Kit_Sop_t *cSop, unsigned uCube, int nLits, Vec_Int_t *vMemory)
Definition: kitSop.c:560
static unsigned Kit_SopCube(Kit_Sop_t *cSop, int i)
Definition: kit.h:189
#define KIT_FACTOR_MEM_LIMIT
DECLARATIONS ///.
Definition: kitFactor.c:31
void Kit_SopDivideByCube(Kit_Sop_t *cSop, Kit_Sop_t *cDiv, Kit_Sop_t *vQuo, Kit_Sop_t *vRem, Vec_Int_t *vMemory)
Definition: kitSop.c:145
int Kit_SopDivisor(Kit_Sop_t *cResult, Kit_Sop_t *cSop, int nLits, Vec_Int_t *vMemory)
Definition: kitSop.c:534
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static Kit_Edge_t Kit_SopFactorTrivialCube(Kit_Graph_t *pFForm, unsigned uCube, int nLits)
Definition: kitFactor.c:247
static Kit_Edge_t Kit_EdgeCreate(int Node, int fCompl)
Definition: kit.h:194
int Kit_SopIsCubeFree(Kit_Sop_t *cSop)
Definition: kitSop.c:334
static Kit_Edge_t Kit_SopFactorLF_rec(Kit_Graph_t *pFForm, Kit_Sop_t *cSop, Kit_Sop_t *cSimple, int nLits, Vec_Int_t *vMemory)
Definition: kitFactor.c:166
void Kit_SopDivideInternal(Kit_Sop_t *cSop, Kit_Sop_t *cDiv, Kit_Sop_t *vQuo, Kit_Sop_t *vRem, Vec_Int_t *vMemory)
Definition: kitSop.c:178
void Kit_SopCommonCubeCover(Kit_Sop_t *cResult, Kit_Sop_t *cSop, Vec_Int_t *vMemory)
Definition: kitSop.c:350
static int Kit_CubeHasLit(unsigned uCube, int i)
Definition: kit.h:175
void Kit_SopMakeCubeFree(Kit_Sop_t *cSop)
Definition: kitSop.c:311
static void Vec_IntGrow(Vec_Int_t *p, int nCapMin)
Definition: bblif.c:336
void Kit_FactorTest(unsigned *pTruth, int nVars)
Definition: kitFactor.c:308
Kit_Edge_t Kit_GraphAddNodeAnd(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1)
Definition: kitGraph.c:172
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
void Kit_GraphFree(Kit_Graph_t *pGraph)
Definition: kitGraph.c:131
Kit_Graph_t * Kit_SopFactor(Vec_Int_t *vCover, int fCompl, int nVars, Vec_Int_t *vMemory)
FUNCTION DEFINITIONS ///.
Definition: kitFactor.c:55
int Kit_TruthIsop(unsigned *puTruth, int nVars, Vec_Int_t *vMemory, int fTryBoth)
FUNCTION DEFINITIONS ///.
Definition: kitIsop.c:55
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static Kit_Edge_t Kit_SopFactor_rec(Kit_Graph_t *pFForm, Kit_Sop_t *cSop, int nLits, Vec_Int_t *vMemory)
Definition: kitFactor.c:108
Kit_Graph_t * Kit_GraphCreate(int nLeaves)
DECLARATIONS ///.
Definition: kitGraph.c:45
Kit_Graph_t * Kit_GraphCreateConst1()
Definition: kitGraph.c:90
Kit_Edge_t Kit_SopFactorTrivialCube_rec(Kit_Graph_t *pFForm, unsigned uCube, int nStart, int nFinish)
Definition: kitFactor.c:199
int Kit_SopFactorVerify(Vec_Int_t *cSop, Kit_Graph_t *pFForm, int nVars)
Definition: kitBdd.c:198
Kit_Graph_t * Kit_GraphCreateConst0()
Definition: kitGraph.c:69
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
typedefABC_NAMESPACE_HEADER_START struct Kit_Sop_t_ Kit_Sop_t
INCLUDES ///.
Definition: kit.h:51
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
static void Kit_GraphSetRoot(Kit_Graph_t *pGraph, Kit_Edge_t eRoot)
Definition: kit.h:208
Kit_Edge_t Kit_GraphAddNodeOr(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1)
Definition: kitGraph.c:196
void Kit_SopCreateInverse(Kit_Sop_t *cResult, Vec_Int_t *vInput, int nVars, Vec_Int_t *vMemory)
Definition: kitSop.c:68
static int Kit_GraphNodeNum(Kit_Graph_t *pGraph)
Definition: kit.h:210
#define assert(ex)
Definition: util_old.h:213
static void Kit_GraphComplement(Kit_Graph_t *pGraph)
Definition: kit.h:207
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
Kit_Edge_t Kit_SopFactorTrivial_rec(Kit_Graph_t *pFForm, unsigned *pCubes, int nCubes, int nLits)
Definition: kitFactor.c:263
static int Kit_SopCubeNum(Kit_Sop_t *cSop)
Definition: kit.h:188
static Kit_Edge_t Kit_SopFactorTrivial(Kit_Graph_t *pFForm, Kit_Sop_t *cSop, int nLits)
Definition: kitFactor.c:291