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
|
/**CFile****************************************************************
FileName [kitFactor.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Computation kit.]
Synopsis [Algebraic factoring.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - Dec 6, 2006.]
Revision [$Id: kitFactor.c,v 1.00 2006/12/06 00:00:00 alanmi Exp $]
***********************************************************************/
#include "kit.h"
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
// factoring fails if intermediate memory usage exceed this limit
#define KIT_FACTOR_MEM_LIMIT (1<<16)
static Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory );
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 );
static Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits );
static Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits );
extern int Kit_SopFactorVerify( Vec_Int_t * cSop, Kit_Graph_t * pFForm, int nVars );
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Factors the cover.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Graph_t * Kit_SopFactor( Vec_Int_t * vCover, int fCompl, int nVars, Vec_Int_t * vMemory )
{
Kit_Sop_t Sop, * cSop = &Sop;
Kit_Graph_t * pFForm;
Kit_Edge_t eRoot;
// int nCubes;
// works for up to 15 variables because division procedure
// used the last bit for marking the cubes going to the remainder
assert( nVars < 16 );
// check for trivial functions
if ( Vec_IntSize(vCover) == 0 )
return Kit_GraphCreateConst0();
if ( Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0 )
return Kit_GraphCreateConst1();
// prepare memory manager
// Vec_IntClear( vMemory );
Vec_IntGrow( vMemory, KIT_FACTOR_MEM_LIMIT );
// perform CST
Kit_SopCreateInverse( cSop, vCover, 2 * nVars, vMemory ); // CST
// start the factored form
pFForm = Kit_GraphCreate( nVars );
// factor the cover
eRoot = Kit_SopFactor_rec( pFForm, cSop, 2 * nVars, vMemory );
// finalize the factored form
Kit_GraphSetRoot( pFForm, eRoot );
if ( fCompl )
Kit_GraphComplement( pFForm );
// verify the factored form
// nCubes = Vec_IntSize(vCover);
// Vec_IntShrink( vCover, nCubes );
// if ( !Kit_SopFactorVerify( vCover, pFForm, nVars ) )
// printf( "Verification has failed.\n" );
return pFForm;
}
/**Function*************************************************************
Synopsis [Recursive factoring procedure.]
Description [For the pseudo-code, see Hachtel/Somenzi,
Logic synthesis and verification algorithms, Kluwer, 1996, p. 432.]
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Edge_t Kit_SopFactor_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits, Vec_Int_t * vMemory )
{
Kit_Sop_t Div, Quo, Rem, Com;
Kit_Sop_t * cDiv = &Div, * cQuo = &Quo, * cRem = &Rem, * cCom = &Com;
Kit_Edge_t eNodeDiv, eNodeQuo, eNodeRem, eNodeAnd;
// make sure the cover contains some cubes
assert( Kit_SopCubeNum(cSop) > 0 );
// get the divisor
if ( !Kit_SopDivisor(cDiv, cSop, nLits, vMemory) )
return Kit_SopFactorTrivial( pFForm, cSop, nLits );
// divide the cover by the divisor
Kit_SopDivideInternal( cSop, cDiv, cQuo, cRem, vMemory );
// check the trivial case
assert( Kit_SopCubeNum(cQuo) > 0 );
if ( Kit_SopCubeNum(cQuo) == 1 )
return Kit_SopFactorLF_rec( pFForm, cSop, cQuo, nLits, vMemory );
// make the quotient cube free
Kit_SopMakeCubeFree( cQuo );
// divide the cover by the quotient
Kit_SopDivideInternal( cSop, cQuo, cDiv, cRem, vMemory );
// check the trivial case
if ( Kit_SopIsCubeFree( cDiv ) )
{
eNodeDiv = Kit_SopFactor_rec( pFForm, cDiv, nLits, vMemory );
eNodeQuo = Kit_SopFactor_rec( pFForm, cQuo, nLits, vMemory );
eNodeAnd = Kit_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
if ( Kit_SopCubeNum(cRem) == 0 )
return eNodeAnd;
eNodeRem = Kit_SopFactor_rec( pFForm, cRem, nLits, vMemory );
return Kit_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
}
// get the common cube
Kit_SopCommonCubeCover( cCom, cDiv, vMemory );
// solve the simple problem
return Kit_SopFactorLF_rec( pFForm, cSop, cCom, nLits, vMemory );
}
/**Function*************************************************************
Synopsis [Internal recursive factoring procedure for the leaf case.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Edge_t Kit_SopFactorLF_rec( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, Kit_Sop_t * cSimple, int nLits, Vec_Int_t * vMemory )
{
Kit_Sop_t Div, Quo, Rem;
Kit_Sop_t * cDiv = &Div, * cQuo = &Quo, * cRem = &Rem;
Kit_Edge_t eNodeDiv, eNodeQuo, eNodeRem, eNodeAnd;
assert( Kit_SopCubeNum(cSimple) == 1 );
// get the most often occurring literal
Kit_SopBestLiteralCover( cDiv, cSop, Kit_SopCube(cSimple, 0), nLits, vMemory );
// divide the cover by the literal
Kit_SopDivideByCube( cSop, cDiv, cQuo, cRem, vMemory );
// get the node pointer for the literal
eNodeDiv = Kit_SopFactorTrivialCube( pFForm, Kit_SopCube(cDiv, 0), nLits );
// factor the quotient and remainder
eNodeQuo = Kit_SopFactor_rec( pFForm, cQuo, nLits, vMemory );
eNodeAnd = Kit_GraphAddNodeAnd( pFForm, eNodeDiv, eNodeQuo );
if ( Kit_SopCubeNum(cRem) == 0 )
return eNodeAnd;
eNodeRem = Kit_SopFactor_rec( pFForm, cRem, nLits, vMemory );
return Kit_GraphAddNodeOr( pFForm, eNodeAnd, eNodeRem );
}
/**Function*************************************************************
Synopsis [Factoring cube.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Edge_t Kit_SopFactorTrivialCube_rec( Kit_Graph_t * pFForm, unsigned uCube, int nStart, int nFinish )
{
Kit_Edge_t eNode1, eNode2;
int i, iLit = -1, nLits, nLits1, nLits2;
assert( uCube );
// count the number of literals in this interval
nLits = 0;
for ( i = nStart; i < nFinish; i++ )
if ( Kit_CubeHasLit(uCube, i) )
{
iLit = i;
nLits++;
}
assert( iLit != -1 );
// quit if there is only one literal
if ( nLits == 1 )
return Kit_EdgeCreate( iLit/2, iLit%2 ); // CST
// split the literals into two parts
nLits1 = nLits/2;
nLits2 = nLits - nLits1;
// nLits2 = nLits/2;
// nLits1 = nLits - nLits2;
// find the splitting point
nLits = 0;
for ( i = nStart; i < nFinish; i++ )
if ( Kit_CubeHasLit(uCube, i) )
{
if ( nLits == nLits1 )
break;
nLits++;
}
// recursively construct the tree for the parts
eNode1 = Kit_SopFactorTrivialCube_rec( pFForm, uCube, nStart, i );
eNode2 = Kit_SopFactorTrivialCube_rec( pFForm, uCube, i, nFinish );
return Kit_GraphAddNodeAnd( pFForm, eNode1, eNode2 );
}
/**Function*************************************************************
Synopsis [Factoring cube.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Edge_t Kit_SopFactorTrivialCube( Kit_Graph_t * pFForm, unsigned uCube, int nLits )
{
return Kit_SopFactorTrivialCube_rec( pFForm, uCube, 0, nLits );
}
/**Function*************************************************************
Synopsis [Factoring SOP.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Edge_t Kit_SopFactorTrivial_rec( Kit_Graph_t * pFForm, unsigned * pCubes, int nCubes, int nLits )
{
Kit_Edge_t eNode1, eNode2;
int nCubes1, nCubes2;
if ( nCubes == 1 )
return Kit_SopFactorTrivialCube_rec( pFForm, pCubes[0], 0, nLits );
// split the cubes into two parts
nCubes1 = nCubes/2;
nCubes2 = nCubes - nCubes1;
// nCubes2 = nCubes/2;
// nCubes1 = nCubes - nCubes2;
// recursively construct the tree for the parts
eNode1 = Kit_SopFactorTrivial_rec( pFForm, pCubes, nCubes1, nLits );
eNode2 = Kit_SopFactorTrivial_rec( pFForm, pCubes + nCubes1, nCubes2, nLits );
return Kit_GraphAddNodeOr( pFForm, eNode1, eNode2 );
}
/**Function*************************************************************
Synopsis [Factoring the cover, which has no algebraic divisors.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Kit_Edge_t Kit_SopFactorTrivial( Kit_Graph_t * pFForm, Kit_Sop_t * cSop, int nLits )
{
return Kit_SopFactorTrivial_rec( pFForm, cSop->pCubes, cSop->nCubes, nLits );
}
/**Function*************************************************************
Synopsis [Testing procedure for the factoring code.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Kit_FactorTest( unsigned * pTruth, int nVars )
{
Vec_Int_t * vCover, * vMemory;
Kit_Graph_t * pGraph;
// unsigned uTruthRes;
int RetValue;
// derive SOP
vCover = Vec_IntAlloc( 0 );
RetValue = Kit_TruthIsop( pTruth, nVars, vCover, 0 );
assert( RetValue == 0 );
// derive factored form
vMemory = Vec_IntAlloc( 0 );
pGraph = Kit_SopFactor( vCover, 0, nVars, vMemory );
/*
// derive truth table
assert( nVars <= 5 );
uTruthRes = Kit_GraphToTruth( pGraph );
if ( uTruthRes != pTruth[0] )
printf( "Verification failed!" );
*/
printf( "Vars = %2d. Cubes = %3d. FFNodes = %3d. FF_memory = %3d.\n",
nVars, Vec_IntSize(vCover), Kit_GraphNodeNum(pGraph), Vec_IntSize(vMemory) );
Vec_IntFree( vMemory );
Vec_IntFree( vCover );
Kit_GraphFree( pGraph );
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
|