1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/**CFile****************************************************************
FileName [ftFactor.c]
PackageName [MVSIS 2.0: Multi-valued logic synthesis system.]
Synopsis [Procedures for algebraic factoring.]
Author [MVSIS Group]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - February 1, 2003.]
Revision [$Id: ftFactor.c,v 1.3 2003/09/01 04:56:43 alanmi Exp $]
***********************************************************************/
#include "base/abc/abc.h"
#include "base/main/main.h"
#include "misc/mvc/mvc.h"
#include "dec.h"
#ifdef ABC_USE_CUDD
#include "bdd/extrab/extraBdd.h"
#endif
ABC_NAMESPACE_IMPL_START
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
static Dec_Edge_t Dec_Factor_rec( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover );
static Dec_Edge_t Dec_FactorLF_rec( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover, Mvc_Cover_t * pSimple );
static Dec_Edge_t Dec_FactorTrivial( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover );
static Dec_Edge_t Dec_FactorTrivialCube( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover, Mvc_Cube_t * pCube, Vec_Int_t * vEdgeLits );
static Dec_Edge_t Dec_FactorTrivialTree_rec( Dec_Graph_t * pFForm, Dec_Edge_t * peNodes, int nNodes, int fNodeOr );
static int Dec_FactorVerify( char * pSop, Dec_Graph_t * pFForm );
static Mvc_Cover_t * Dec_ConvertSopToMvc( char * pSop );
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Factors the cover.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dec_Graph_t * Dec_Factor( char * pSop )
{
Mvc_Cover_t * pCover;
Dec_Graph_t * pFForm;
Dec_Edge_t eRoot;
if ( Abc_SopIsConst0(pSop) )
return Dec_GraphCreateConst0();
if ( Abc_SopIsConst1(pSop) )
return Dec_GraphCreateConst1();
// derive the cover from the SOP representation
pCover = Dec_ConvertSopToMvc( pSop );
// make sure the cover is CCS free (should be done before CST)
Mvc_CoverContain( pCover );
// check for trivial functions
assert( !Mvc_CoverIsEmpty(pCover) );
assert( !Mvc_CoverIsTautology(pCover) );
// perform CST
Mvc_CoverInverse( pCover ); // CST
// start the factored form
pFForm = Dec_GraphCreate( Abc_SopGetVarNum(pSop) );
// factor the cover
eRoot = Dec_Factor_rec( pFForm, pCover );
// finalize the factored form
Dec_GraphSetRoot( pFForm, eRoot );
// complement the factored form if SOP is complemented
if ( Abc_SopIsComplement(pSop) )
Dec_GraphComplement( pFForm );
// verify the factored form
// if ( !Dec_FactorVerify( pSop, pFForm ) )
// printf( "Verification has failed.\n" );
// Mvc_CoverInverse( pCover ); // undo CST
Mvc_CoverFree( pCover );
return pFForm;
}
/**Function*************************************************************
Synopsis [Internal recursive factoring procedure.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dec_Edge_t Dec_Factor_rec( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover )
{
Mvc_Cover_t * pDiv, * pQuo, * pRem, * pCom;
Dec_Edge_t eNodeDiv, eNodeQuo, eNodeRem;
Dec_Edge_t eNodeAnd, eNode;
// make sure the cover contains some cubes
assert( Mvc_CoverReadCubeNum(pCover) );
// get the divisor
pDiv = Mvc_CoverDivisor( pCover );
if ( pDiv == NULL )
return Dec_FactorTrivial( pFForm, pCover );
// divide the cover by the divisor
Mvc_CoverDivideInternal( pCover, pDiv, &pQuo, &pRem );
assert( Mvc_CoverReadCubeNum(pQuo) );
Mvc_CoverFree( pDiv );
Mvc_CoverFree( pRem );
// check the trivial case
if ( Mvc_CoverReadCubeNum(pQuo) == 1 )
{
eNode = Dec_FactorLF_rec( pFForm, pCover, pQuo );
Mvc_CoverFree( pQuo );
return eNode;
}
// make the quotient cube ABC_FREE
Mvc_CoverMakeCubeFree( pQuo );
// divide the cover by the quotient
Mvc_CoverDivideInternal( pCover, pQuo, &pDiv, &pRem );
// check the trivial case
if ( Mvc_CoverIsCubeFree( pDiv ) )
{
eNodeDiv = Dec_Factor_rec( pFForm, pDiv );
eNodeQuo = Dec_Factor_rec( pFForm, pQuo );
Mvc_CoverFree( pDiv );
Mvc_CoverFree( pQuo );
eNodeAnd = Dec_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
if ( Mvc_CoverReadCubeNum(pRem) == 0 )
{
Mvc_CoverFree( pRem );
return eNodeAnd;
}
else
{
eNodeRem = Dec_Factor_rec( pFForm, pRem );
Mvc_CoverFree( pRem );
return Dec_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
}
}
// get the common cube
pCom = Mvc_CoverCommonCubeCover( pDiv );
Mvc_CoverFree( pDiv );
Mvc_CoverFree( pQuo );
Mvc_CoverFree( pRem );
// solve the simple problem
eNode = Dec_FactorLF_rec( pFForm, pCover, pCom );
Mvc_CoverFree( pCom );
return eNode;
}
/**Function*************************************************************
Synopsis [Internal recursive factoring procedure for the leaf case.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dec_Edge_t Dec_FactorLF_rec( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover, Mvc_Cover_t * pSimple )
{
Dec_Man_t * pManDec = (Dec_Man_t *)Abc_FrameReadManDec();
Vec_Int_t * vEdgeLits = pManDec->vLits;
Mvc_Cover_t * pDiv, * pQuo, * pRem;
Dec_Edge_t eNodeDiv, eNodeQuo, eNodeRem;
Dec_Edge_t eNodeAnd;
// get the most often occurring literal
pDiv = Mvc_CoverBestLiteralCover( pCover, pSimple );
// divide the cover by the literal
Mvc_CoverDivideByLiteral( pCover, pDiv, &pQuo, &pRem );
// get the node pointer for the literal
eNodeDiv = Dec_FactorTrivialCube( pFForm, pDiv, Mvc_CoverReadCubeHead(pDiv), vEdgeLits );
Mvc_CoverFree( pDiv );
// factor the quotient and remainder
eNodeQuo = Dec_Factor_rec( pFForm, pQuo );
Mvc_CoverFree( pQuo );
eNodeAnd = Dec_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
if ( Mvc_CoverReadCubeNum(pRem) == 0 )
{
Mvc_CoverFree( pRem );
return eNodeAnd;
}
else
{
eNodeRem = Dec_Factor_rec( pFForm, pRem );
Mvc_CoverFree( pRem );
return Dec_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
}
}
/**Function*************************************************************
Synopsis [Factoring the cover, which has no algebraic divisors.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dec_Edge_t Dec_FactorTrivial( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover )
{
Dec_Man_t * pManDec = (Dec_Man_t *)Abc_FrameReadManDec();
Vec_Int_t * vEdgeCubes = pManDec->vCubes;
Vec_Int_t * vEdgeLits = pManDec->vLits;
Dec_Edge_t eNode;
Mvc_Cube_t * pCube;
// create the factored form for each cube
Vec_IntClear( vEdgeCubes );
Mvc_CoverForEachCube( pCover, pCube )
{
eNode = Dec_FactorTrivialCube( pFForm, pCover, pCube, vEdgeLits );
Vec_IntPush( vEdgeCubes, Dec_EdgeToInt_(eNode) );
}
// balance the factored forms
return Dec_FactorTrivialTree_rec( pFForm, (Dec_Edge_t *)vEdgeCubes->pArray, vEdgeCubes->nSize, 1 );
}
/**Function*************************************************************
Synopsis [Factoring the cube.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dec_Edge_t Dec_FactorTrivialCube( Dec_Graph_t * pFForm, Mvc_Cover_t * pCover, Mvc_Cube_t * pCube, Vec_Int_t * vEdgeLits )
{
Dec_Edge_t eNode;
int iBit, Value;
// create the factored form for each literal
Vec_IntClear( vEdgeLits );
Mvc_CubeForEachBit( pCover, pCube, iBit, Value )
if ( Value )
{
eNode = Dec_EdgeCreate( iBit/2, iBit%2 ); // CST
Vec_IntPush( vEdgeLits, Dec_EdgeToInt_(eNode) );
}
// balance the factored forms
return Dec_FactorTrivialTree_rec( pFForm, (Dec_Edge_t *)vEdgeLits->pArray, vEdgeLits->nSize, 0 );
}
/**Function*************************************************************
Synopsis [Create the well-balanced tree of nodes.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dec_Edge_t Dec_FactorTrivialTree_rec( Dec_Graph_t * pFForm, Dec_Edge_t * peNodes, int nNodes, int fNodeOr )
{
Dec_Edge_t eNode1, eNode2;
int nNodes1, nNodes2;
if ( nNodes == 1 )
return peNodes[0];
// split the nodes into two parts
nNodes1 = nNodes/2;
nNodes2 = nNodes - nNodes1;
// nNodes2 = nNodes/2;
// nNodes1 = nNodes - nNodes2;
// recursively construct the tree for the parts
eNode1 = Dec_FactorTrivialTree_rec( pFForm, peNodes, nNodes1, fNodeOr );
eNode2 = Dec_FactorTrivialTree_rec( pFForm, peNodes + nNodes1, nNodes2, fNodeOr );
if ( fNodeOr )
return Dec_GraphAddNodeOr( pFForm, eNode1, eNode2 );
else
return Dec_GraphAddNodeAnd( pFForm, eNode1, eNode2 );
}
/**Function*************************************************************
Synopsis [Converts SOP into MVC.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Mvc_Cover_t * Dec_ConvertSopToMvc( char * pSop )
{
Dec_Man_t * pManDec = (Dec_Man_t *)Abc_FrameReadManDec();
Mvc_Manager_t * pMem = (Mvc_Manager_t *)pManDec->pMvcMem;
Mvc_Cover_t * pMvc;
Mvc_Cube_t * pMvcCube;
char * pCube;
int nVars, Value, v;
// start the cover
nVars = Abc_SopGetVarNum(pSop);
assert( nVars > 0 );
pMvc = Mvc_CoverAlloc( pMem, nVars * 2 );
// check the logic function of the node
Abc_SopForEachCube( pSop, nVars, pCube )
{
// create and add the cube
pMvcCube = Mvc_CubeAlloc( pMvc );
Mvc_CoverAddCubeTail( pMvc, pMvcCube );
// fill in the literals
Mvc_CubeBitFill( pMvcCube );
Abc_CubeForEachVar( pCube, Value, v )
{
if ( Value == '0' )
Mvc_CubeBitRemove( pMvcCube, v * 2 + 1 );
else if ( Value == '1' )
Mvc_CubeBitRemove( pMvcCube, v * 2 );
}
}
return pMvc;
}
#ifdef ABC_USE_CUDD
/**Function*************************************************************
Synopsis [Verifies that the factoring is correct.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Dec_FactorVerify( char * pSop, Dec_Graph_t * pFForm )
{
extern DdNode * Abc_ConvertSopToBdd( DdManager * dd, char * pSop, DdNode ** pbVars );
extern DdNode * Dec_GraphDeriveBdd( DdManager * dd, Dec_Graph_t * pGraph );
DdManager * dd = (DdManager *)Abc_FrameReadManDd();
DdNode * bFunc1, * bFunc2;
int RetValue;
bFunc1 = Abc_ConvertSopToBdd( dd, pSop, NULL ); Cudd_Ref( bFunc1 );
bFunc2 = Dec_GraphDeriveBdd( dd, pFForm ); Cudd_Ref( bFunc2 );
//Extra_bddPrint( dd, bFunc1 ); printf("\n");
//Extra_bddPrint( dd, bFunc2 ); printf("\n");
RetValue = (bFunc1 == bFunc2);
if ( bFunc1 != bFunc2 )
{
int s;
Extra_bddPrint( dd, bFunc1 ); printf("\n");
Extra_bddPrint( dd, bFunc2 ); printf("\n");
s = 0;
}
Cudd_RecursiveDeref( dd, bFunc1 );
Cudd_RecursiveDeref( dd, bFunc2 );
return RetValue;
}
#endif
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|