ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
kitFactor.c
Go to the documentation of this file.
1
20
21#include "kit.h"
22
24
25
29
30// factoring fails if intermediate memory usage exceed this limit
31#define KIT_FACTOR_MEM_LIMIT (1<<20)
32
33static Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory );
34static Kit_Edge_t Kit_SopFactorLF_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, Kit_Sop_t * cSimple, int nLits, Vec_Int_t * vMemory );
35static Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits );
36static Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits );
37
38extern int Kit_SopFactorVerify( Vec_Int_t * cSop, Kit_Graph_t * pFForm, int nVars );
39
43
55Kit_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 );
74 Vec_IntGrow( vMemory, KIT_FACTOR_MEM_LIMIT );
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
108Kit_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
166Kit_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
199Kit_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
247Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits )
248{
249 return Kit_SopFactorTrivialCube_rec( pFForm, uCube, 0, nLits );
250}
251
263Kit_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
291Kit_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
308void 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
341
342
344
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Kit_Graph_t * Kit_SopFactor(Vec_Int_t *vCover, int fCompl, int nVars, Vec_Int_t *vMemory)
FUNCTION DEFINITIONS ///.
Definition kitFactor.c:55
#define KIT_FACTOR_MEM_LIMIT
DECLARATIONS ///.
Definition kitFactor.c:31
int Kit_SopFactorVerify(Vec_Int_t *cSop, Kit_Graph_t *pFForm, int nVars)
Kit_Edge_t Kit_SopFactorTrivial_rec(Kit_Graph_t *pFForm, unsigned *pCubes, int nCubes, int nLits)
Definition kitFactor.c:263
void Kit_FactorTest(unsigned *pTruth, int nVars)
Definition kitFactor.c:308
Kit_Edge_t Kit_SopFactorTrivialCube_rec(Kit_Graph_t *pFForm, unsigned uCube, int nStart, int nFinish)
Definition kitFactor.c:199
struct Kit_Graph_t_ Kit_Graph_t
Definition kit.h:88
int Kit_TruthIsop(unsigned *puTruth, int nVars, Vec_Int_t *vMemory, int fTryBoth)
Definition kitIsop.c:134
void Kit_SopMakeCubeFree(Kit_Sop_t *cSop)
Definition kitSop.c:311
Kit_Edge_t Kit_GraphAddNodeAnd(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1)
Definition kitGraph.c:173
typedefABC_NAMESPACE_HEADER_START struct Kit_Sop_t_ Kit_Sop_t
INCLUDES ///.
Definition kit.h:54
Kit_Graph_t * Kit_GraphCreateConst1()
Definition kitGraph.c:91
void Kit_SopCommonCubeCover(Kit_Sop_t *cResult, Kit_Sop_t *cSop, Vec_Int_t *vMemory)
Definition kitSop.c:350
void Kit_SopCreateInverse(Kit_Sop_t *cResult, Vec_Int_t *vInput, int nVars, Vec_Int_t *vMemory)
Definition kitSop.c:68
int Kit_SopIsCubeFree(Kit_Sop_t *cSop)
Definition kitSop.c:334
void Kit_GraphFree(Kit_Graph_t *pGraph)
Definition kitGraph.c:132
Kit_Edge_t Kit_GraphAddNodeOr(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1)
Definition kitGraph.c:197
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
Kit_Graph_t * Kit_GraphCreate(int nLeaves)
DECLARATIONS ///.
Definition kitGraph.c:46
Kit_Graph_t * Kit_GraphCreateConst0()
Definition kitGraph.c:70
int Kit_SopDivisor(Kit_Sop_t *cResult, Kit_Sop_t *cSop, int nLits, Vec_Int_t *vMemory)
Definition kitSop.c:534
struct Kit_Edge_t_ Kit_Edge_t
Definition kit.h:62
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_SopBestLiteralCover(Kit_Sop_t *cResult, Kit_Sop_t *cSop, unsigned uCube, int nLits, Vec_Int_t *vMemory)
Definition kitSop.c:560
#define assert(ex)
Definition util_old.h:213