00001
00021 #include "kit.h"
00022
00026
00027
00028 #define KIT_FACTOR_MEM_LIMIT (1<<16)
00029
00030 static Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory );
00031 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 );
00032 static Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits );
00033 static Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits );
00034
00035 extern int Kit_SopFactorVerify( Vec_Int_t * cSop, Kit_Graph_t * pFForm, int nVars );
00036
00040
00052 Kit_Graph_t * Kit_SopFactor( Vec_Int_t * vCover, int fCompl, int nVars, Vec_Int_t * vMemory )
00053 {
00054 Kit_Sop_t Sop, * cSop = &Sop;
00055 Kit_Graph_t * pFForm;
00056 Kit_Edge_t eRoot;
00057
00058
00059
00060
00061 assert( nVars < 16 );
00062
00063
00064 if ( Vec_IntSize(vCover) == 0 )
00065 return Kit_GraphCreateConst0();
00066 if ( Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0 )
00067 return Kit_GraphCreateConst1();
00068
00069
00070
00071 Vec_IntGrow( vMemory, KIT_FACTOR_MEM_LIMIT );
00072
00073
00074 Kit_SopCreateInverse( cSop, vCover, 2 * nVars, vMemory );
00075
00076
00077 pFForm = Kit_GraphCreate( nVars );
00078
00079 eRoot = Kit_SopFactor_rec( pFForm, cSop, 2 * nVars, vMemory );
00080
00081 Kit_GraphSetRoot( pFForm, eRoot );
00082 if ( fCompl )
00083 Kit_GraphComplement( pFForm );
00084
00085
00086
00087
00088
00089
00090 return pFForm;
00091 }
00092
00105 Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory )
00106 {
00107 Kit_Sop_t Div, Quo, Rem, Com;
00108 Kit_Sop_t * cDiv = &Div, * cQuo = &Quo, * cRem = &Rem, * cCom = &Com;
00109 Kit_Edge_t eNodeDiv, eNodeQuo, eNodeRem, eNodeAnd;
00110
00111
00112 assert( Kit_SopCubeNum(cSop) > 0 );
00113
00114
00115 if ( !Kit_SopDivisor(cDiv, cSop, nLits, vMemory) )
00116 return Kit_SopFactorTrivial( pFForm, cSop, nLits );
00117
00118
00119 Kit_SopDivideInternal( cSop, cDiv, cQuo, cRem, vMemory );
00120
00121
00122 assert( Kit_SopCubeNum(cQuo) > 0 );
00123 if ( Kit_SopCubeNum(cQuo) == 1 )
00124 return Kit_SopFactorLF_rec( pFForm, cSop, cQuo, nLits, vMemory );
00125
00126
00127 Kit_SopMakeCubeFree( cQuo );
00128
00129
00130 Kit_SopDivideInternal( cSop, cQuo, cDiv, cRem, vMemory );
00131
00132
00133 if ( Kit_SopIsCubeFree( cDiv ) )
00134 {
00135 eNodeDiv = Kit_SopFactor_rec( pFForm, cDiv, nLits, vMemory );
00136 eNodeQuo = Kit_SopFactor_rec( pFForm, cQuo, nLits, vMemory );
00137 eNodeAnd = Kit_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
00138 if ( Kit_SopCubeNum(cRem) == 0 )
00139 return eNodeAnd;
00140 eNodeRem = Kit_SopFactor_rec( pFForm, cRem, nLits, vMemory );
00141 return Kit_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
00142 }
00143
00144
00145 Kit_SopCommonCubeCover( cCom, cDiv, vMemory );
00146
00147
00148 return Kit_SopFactorLF_rec( pFForm, cSop, cCom, nLits, vMemory );
00149 }
00150
00151
00163 Kit_Edge_t Kit_SopFactorLF_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, Kit_Sop_t * cSimple, int nLits, Vec_Int_t * vMemory )
00164 {
00165 Kit_Sop_t Div, Quo, Rem;
00166 Kit_Sop_t * cDiv = &Div, * cQuo = &Quo, * cRem = &Rem;
00167 Kit_Edge_t eNodeDiv, eNodeQuo, eNodeRem, eNodeAnd;
00168 assert( Kit_SopCubeNum(cSimple) == 1 );
00169
00170 Kit_SopBestLiteralCover( cDiv, cSop, Kit_SopCube(cSimple, 0), nLits, vMemory );
00171
00172 Kit_SopDivideByCube( cSop, cDiv, cQuo, cRem, vMemory );
00173
00174 eNodeDiv = Kit_SopFactorTrivialCube( pFForm, Kit_SopCube(cDiv, 0), nLits );
00175
00176 eNodeQuo = Kit_SopFactor_rec( pFForm, cQuo, nLits, vMemory );
00177 eNodeAnd = Kit_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
00178 if ( Kit_SopCubeNum(cRem) == 0 )
00179 return eNodeAnd;
00180 eNodeRem = Kit_SopFactor_rec( pFForm, cRem, nLits, vMemory );
00181 return Kit_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
00182 }
00183
00184
00196 Kit_Edge_t Kit_SopFactorTrivialCube_rec( Kit_Graph_t * pFForm, unsigned uCube, int nStart, int nFinish )
00197 {
00198 Kit_Edge_t eNode1, eNode2;
00199 int i, iLit = -1, nLits, nLits1, nLits2;
00200 assert( uCube );
00201
00202 nLits = 0;
00203 for ( i = nStart; i < nFinish; i++ )
00204 if ( Kit_CubeHasLit(uCube, i) )
00205 {
00206 iLit = i;
00207 nLits++;
00208 }
00209 assert( iLit != -1 );
00210
00211 if ( nLits == 1 )
00212 return Kit_EdgeCreate( iLit/2, iLit%2 );
00213
00214 nLits1 = nLits/2;
00215 nLits2 = nLits - nLits1;
00216
00217
00218
00219 nLits = 0;
00220 for ( i = nStart; i < nFinish; i++ )
00221 if ( Kit_CubeHasLit(uCube, i) )
00222 {
00223 if ( nLits == nLits1 )
00224 break;
00225 nLits++;
00226 }
00227
00228 eNode1 = Kit_SopFactorTrivialCube_rec( pFForm, uCube, nStart, i );
00229 eNode2 = Kit_SopFactorTrivialCube_rec( pFForm, uCube, i, nFinish );
00230 return Kit_GraphAddNodeAnd( pFForm, eNode1, eNode2 );
00231 }
00232
00244 Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits )
00245 {
00246 return Kit_SopFactorTrivialCube_rec( pFForm, uCube, 0, nLits );
00247 }
00248
00260 Kit_Edge_t Kit_SopFactorTrivial_rec( Kit_Graph_t * pFForm, unsigned * pCubes, int nCubes, int nLits )
00261 {
00262 Kit_Edge_t eNode1, eNode2;
00263 int nCubes1, nCubes2;
00264 if ( nCubes == 1 )
00265 return Kit_SopFactorTrivialCube_rec( pFForm, pCubes[0], 0, nLits );
00266
00267 nCubes1 = nCubes/2;
00268 nCubes2 = nCubes - nCubes1;
00269
00270
00271
00272 eNode1 = Kit_SopFactorTrivial_rec( pFForm, pCubes, nCubes1, nLits );
00273 eNode2 = Kit_SopFactorTrivial_rec( pFForm, pCubes + nCubes1, nCubes2, nLits );
00274 return Kit_GraphAddNodeOr( pFForm, eNode1, eNode2 );
00275 }
00276
00288 Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits )
00289 {
00290 return Kit_SopFactorTrivial_rec( pFForm, cSop->pCubes, cSop->nCubes, nLits );
00291 }
00292
00293
00305 void Kit_FactorTest( unsigned * pTruth, int nVars )
00306 {
00307 Vec_Int_t * vCover, * vMemory;
00308 Kit_Graph_t * pGraph;
00309
00310 int RetValue;
00311
00312
00313 vCover = Vec_IntAlloc( 0 );
00314 RetValue = Kit_TruthIsop( pTruth, nVars, vCover, 0 );
00315 assert( RetValue == 0 );
00316
00317
00318 vMemory = Vec_IntAlloc( 0 );
00319 pGraph = Kit_SopFactor( vCover, 0, nVars, vMemory );
00320
00321
00322
00323
00324
00325
00326
00327 printf( "Vars = %2d. Cubes = %3d. FFNodes = %3d. FF_memory = %3d.\n",
00328 nVars, Vec_IntSize(vCover), Kit_GraphNodeNum(pGraph), Vec_IntSize(vMemory) );
00329
00330 Vec_IntFree( vMemory );
00331 Vec_IntFree( vCover );
00332 Kit_GraphFree( pGraph );
00333 }
00334
00338
00339