ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
acecPolyn.c
Go to the documentation of this file.
1
20
21#include "acecInt.h"
22#include "misc/vec/vecWec.h"
23#include "misc/vec/vecHsh.h"
24#include "misc/vec/vecQue.h"
25
27
28
32
33/*
34!a -> 1 - a
35a & b -> ab
36a | b -> a + b - ab
37a ^ b -> a + b - 2ab
38MUX(a, b, c) -> ab | (1 - a)c = ab + (1-a)c - ab(1-a)c = ab + c - ac
39
40!a & b -> (1 - a)b = b - ab
41 a & !b -> a(1 - b) = a - ab
42!a & !b -> 1 - a - b + ab
43*/
44
45typedef struct Pln_Man_t_ Pln_Man_t;
47{
48 Gia_Man_t * pGia; // AIG manager
49 Hsh_VecMan_t * pHashC; // hash table for constants
50 Hsh_VecMan_t * pHashM; // hash table for monomials
51 Vec_Que_t * vQue; // queue by largest node
52 Vec_Flt_t * vCounts; // largest node
53 Vec_Int_t * vCoefs; // coefficients for each monomial
54 Vec_Int_t * vTempC[2]; // polynomial representation
55 Vec_Int_t * vTempM[4]; // polynomial representation
56 Vec_Int_t * vOrder; // order of collapsing
57 int nBuilds; // built monomials
58 int nUsed; // used monomials
59};
60
64
77{
79 p->pGia = pGia;
80 p->pHashC = Hsh_VecManStart( 1000 );
81 p->pHashM = Hsh_VecManStart( 1000 );
82 p->vQue = Vec_QueAlloc( 1000 );
83 p->vCounts = Vec_FltAlloc( 1000 );
84 p->vCoefs = Vec_IntAlloc( 1000 );
85 p->vTempC[0] = Vec_IntAlloc( 100 );
86 p->vTempC[1] = Vec_IntAlloc( 100 );
87 p->vTempM[0] = Vec_IntAlloc( 100 );
88 p->vTempM[1] = Vec_IntAlloc( 100 );
89 p->vTempM[2] = Vec_IntAlloc( 100 );
90 p->vTempM[3] = Vec_IntAlloc( 100 );
91 p->vOrder = vOrder ? Vec_IntDup(vOrder) : Vec_IntStartNatural( Gia_ManObjNum(pGia) );
92 assert( Vec_IntSize(p->vOrder) == Gia_ManObjNum(pGia) );
93 Vec_QueSetPriority( p->vQue, Vec_FltArrayP(p->vCounts) );
94 // add 0-constant and 1-monomial
95 Hsh_VecManAdd( p->pHashC, p->vTempC[0] );
96 Hsh_VecManAdd( p->pHashM, p->vTempM[0] );
97 Vec_FltPush( p->vCounts, 0 );
98 Vec_IntPush( p->vCoefs, 0 );
99 return p;
100}
102{
103 Hsh_VecManStop( p->pHashC );
104 Hsh_VecManStop( p->pHashM );
105 Vec_QueFree( p->vQue );
106 Vec_FltFree( p->vCounts );
107 Vec_IntFree( p->vCoefs );
108 Vec_IntFree( p->vTempC[0] );
109 Vec_IntFree( p->vTempC[1] );
110 Vec_IntFree( p->vTempM[0] );
111 Vec_IntFree( p->vTempM[1] );
112 Vec_IntFree( p->vTempM[2] );
113 Vec_IntFree( p->vTempM[3] );
114 Vec_IntFree( p->vOrder );
115 ABC_FREE( p );
116}
117int Pln_ManCompare3( int * pData0, int * pData1 )
118{
119 if ( pData0[0] < pData1[0] ) return -1;
120 if ( pData0[0] > pData1[0] ) return 1;
121 if ( pData0[1] < pData1[1] ) return -1;
122 if ( pData0[1] > pData1[1] ) return 1;
123 return 0;
124}
125void Pln_ManPrintFinal( Pln_Man_t * p, int fVerbose, int fVeryVerbose )
126{
127 Vec_Int_t * vArray;
128 int i, k, Entry, iMono, iConst;
129 // collect triples
130 Vec_Int_t * vPairs = Vec_IntAlloc( 100 );
131 Vec_IntForEachEntry( p->vCoefs, iConst, iMono )
132 {
133 if ( iConst == 0 )
134 continue;
135 vArray = Hsh_VecReadEntry( p->pHashC, iConst );
136 Vec_IntPush( vPairs, Vec_IntEntry(vArray, 0) );
137 vArray = Hsh_VecReadEntry( p->pHashM, iMono );
138 Vec_IntPush( vPairs, Vec_IntSize(vArray) ? Vec_IntEntry(vArray, 0) : 0 );
139 Vec_IntPushTwo( vPairs, iConst, iMono );
140 }
141 // sort triples
142 qsort( Vec_IntArray(vPairs), (size_t)(Vec_IntSize(vPairs)/4), 16, (int (*)(const void *, const void *))Pln_ManCompare3 );
143 // print
144 if ( fVerbose )
145 Vec_IntForEachEntryDouble( vPairs, iConst, iMono, i )
146 {
147 if ( i % 4 == 0 )
148 continue;
149 printf( "%-6d : ", i/4 );
150 vArray = Hsh_VecReadEntry( p->pHashC, iConst );
151 Vec_IntForEachEntry( vArray, Entry, k )
152 printf( "%s%d", Entry < 0 ? "-" : "+", (1 << (Abc_AbsInt(Entry)-1)) );
153 vArray = Hsh_VecReadEntry( p->pHashM, iMono );
154 Vec_IntForEachEntry( vArray, Entry, k )
155 printf( " * %d", Entry );
156 printf( "\n" );
157 }
158 printf( "HashC = %d. HashM = %d. Total = %d. Used = %d. ", Hsh_VecSize(p->pHashC), Hsh_VecSize(p->pHashM), p->nBuilds, Vec_IntSize(vPairs)/4 );
159 Vec_IntFree( vPairs );
160}
161
173static inline void Vec_IntAppendMinus2x( Vec_Int_t * vVec1, Vec_Int_t * vVec2 )
174{
175 int Entry, i;
176 Vec_IntClear( vVec1 );
177 Vec_IntForEachEntry( vVec2, Entry, i )
178 Vec_IntPush( vVec1, Entry > 0 ? -Entry-1 : -Entry+1 );
179}
180
192static inline void Gia_PolynMergeConstOne( Vec_Int_t * vConst, int New )
193{
194 int i, Old;
195 assert( New != 0 );
196 Vec_IntForEachEntry( vConst, Old, i )
197 {
198 assert( Old != 0 );
199 if ( Old == New ) // A == B
200 {
201 Vec_IntDrop( vConst, i );
202 Gia_PolynMergeConstOne( vConst, New > 0 ? New + 1 : New - 1 );
203 return;
204 }
205 if ( Abc_AbsInt(Old) == Abc_AbsInt(New) ) // A == -B
206 {
207 Vec_IntDrop( vConst, i );
208 return;
209 }
210 if ( Old + New == 1 || Old + New == -1 ) // sign(A) != sign(B) && abs(abs(A)-abs(B)) == 1
211 {
212 int Value = Abc_MinInt( Abc_AbsInt(Old), Abc_AbsInt(New) );
213 Vec_IntDrop( vConst, i );
214 Gia_PolynMergeConstOne( vConst, (Old + New == 1) ? Value : -Value );
215 return;
216 }
217 }
218 Vec_IntPushUniqueOrder( vConst, New );
219}
220static inline void Gia_PolynMergeConst( Vec_Int_t * vConst, Pln_Man_t * p, int iConstAdd )
221{
222 int i, New;
223 Vec_Int_t * vConstAdd = Hsh_VecReadEntry( p->pHashC, iConstAdd );
224 Vec_IntForEachEntry( vConstAdd, New, i )
225 {
226 Gia_PolynMergeConstOne( vConst, New );
227 vConstAdd = Hsh_VecReadEntry( p->pHashC, iConstAdd );
228 }
229}
230static inline void Gia_PolynBuildAdd( Pln_Man_t * p, Vec_Int_t * vTempC, Vec_Int_t * vTempM )
231{
232 int iConst, iConstNew, iMono = vTempM ? Hsh_VecManAdd(p->pHashM, vTempM) : 0;
233 p->nBuilds++;
234 if ( iMono == Vec_IntSize(p->vCoefs) ) // new monomial
235 {
236 iConst = Hsh_VecManAdd( p->pHashC, vTempC );
237 Vec_IntPush( p->vCoefs, iConst );
238// Vec_FltPush( p->vCounts, Vec_IntEntryLast(vTempM) );
239 Vec_FltPush( p->vCounts, (float)Vec_IntEntry(p->vOrder, Vec_IntEntryLast(vTempM)) );
240 Vec_QuePush( p->vQue, iMono );
241// Vec_QueUpdate( p->vQue, iMono );
242 if ( iConst )
243 p->nUsed++;
244 return;
245 }
246 // this monomial exists
247 iConst = Vec_IntEntry( p->vCoefs, iMono );
248 if ( iConst )
249 Gia_PolynMergeConst( vTempC, p, iConst );
250 iConstNew = Hsh_VecManAdd( p->pHashC, vTempC );
251 Vec_IntWriteEntry( p->vCoefs, iMono, iConstNew );
252 if ( iConst && !iConstNew )
253 p->nUsed--;
254 else if ( !iConst && iConstNew )
255 p->nUsed++;
256 //assert( p->nUsed == Vec_IntSize(p->vCoefs) - Vec_IntCountZero(p->vCoefs) );
257}
258void Gia_PolynBuildOne( Pln_Man_t * p, int iMono )
259{
260 Gia_Obj_t * pObj;
261 Vec_Int_t * vArray, * vConst;
262 int iFan0, iFan1;
263 int k, iConst, iDriver;
264
265 assert( Vec_IntSize(p->vCoefs) == Hsh_VecSize(p->pHashM) );
266 vArray = Hsh_VecReadEntry( p->pHashM, iMono );
267 iDriver = Vec_IntEntryLast( vArray );
268 pObj = Gia_ManObj( p->pGia, iDriver );
269 if ( !Gia_ObjIsAnd(pObj) )
270 return;
271 assert( !Gia_ObjIsMux(p->pGia, pObj) );
272
273 iConst = Vec_IntEntry( p->vCoefs, iMono );
274 if ( iConst == 0 )
275 return;
276 Vec_IntWriteEntry( p->vCoefs, iMono, 0 );
277 p->nUsed--;
278
279 iFan0 = Gia_ObjFaninId0p(p->pGia, pObj);
280 iFan1 = Gia_ObjFaninId1p(p->pGia, pObj);
281 for ( k = 0; k < 4; k++ )
282 {
283 Vec_IntClear( p->vTempM[k] );
284 Vec_IntAppend( p->vTempM[k], vArray );
285 Vec_IntPop( p->vTempM[k] );
286 if ( k == 1 || k == 3 )
287 Vec_IntPushUniqueOrderCost( p->vTempM[k], iFan0, p->vOrder ); // x
288// Vec_IntPushUniqueOrder( p->vTempM[k], iFan0 ); // x
289 if ( k == 2 || k == 3 )
290 Vec_IntPushUniqueOrderCost( p->vTempM[k], iFan1, p->vOrder ); // y
291// Vec_IntPushUniqueOrder( p->vTempM[k], iFan1 ); // y
292 }
293
294 vConst = Hsh_VecReadEntry( p->pHashC, iConst );
295
296 if ( !Gia_ObjIsXor(pObj) )
297 for ( k = 0; k < 2; k++ )
298 Vec_IntAppendMinus( p->vTempC[k], vConst, k );
299
300 if ( Gia_ObjIsXor(pObj) )
301 {
302 vConst = Hsh_VecReadEntry( p->pHashC, iConst );
303 Vec_IntAppendMinus( p->vTempC[0], vConst, 0 );
304 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[1] ); // C * x
305
306 vConst = Hsh_VecReadEntry( p->pHashC, iConst );
307 Vec_IntAppendMinus( p->vTempC[0], vConst, 0 );
308 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[2] ); // C * y
309
310 //if ( !p->pGia->vXors || Vec_IntFind(p->pGia->vXors, iDriver) == -1 || Vec_IntFind(p->pGia->vXors, iDriver) == 5 )
311 {
312 vConst = Hsh_VecReadEntry( p->pHashC, iConst );
313 Vec_IntAppendMinus2x( p->vTempC[0], vConst );
314 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[3] ); // -C * x * y
315 }
316 }
317 else if ( Gia_ObjFaninC0(pObj) && Gia_ObjFaninC1(pObj) ) // C * (1 - x) * (1 - y)
318 {
319 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[0] ); // C * 1
320 Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[1] ); // -C * x
321 vConst = Hsh_VecReadEntry( p->pHashC, iConst );
322 for ( k = 0; k < 2; k++ )
323 Vec_IntAppendMinus( p->vTempC[k], vConst, k );
324 Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[2] ); // -C * y
325 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[3] ); // C * x * y
326 }
327 else if ( Gia_ObjFaninC0(pObj) && !Gia_ObjFaninC1(pObj) ) // C * (1 - x) * y
328 {
329 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[2] ); // C * y
330 Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[3] ); // -C * x * y
331 }
332 else if ( !Gia_ObjFaninC0(pObj) && Gia_ObjFaninC1(pObj) ) // C * x * (1 - y)
333 {
334 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[1] ); // C * x
335 Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[3] ); // -C * x * y
336 }
337 else
338 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[3] ); // C * x * y
339}
340void Gia_PolynBuild( Gia_Man_t * pGia, Vec_Int_t * vOrder, int fSigned, int fVerbose, int fVeryVerbose )
341{
342 abctime clk = Abc_Clock();//, clk2 = 0;
343 Gia_Obj_t * pObj;
344 Vec_Bit_t * vPres = Vec_BitStart( Gia_ManObjNum(pGia) );
345 int i, iMono, iDriver, LevPrev, LevCur, Iter, Line = 0;
346 Pln_Man_t * p = Pln_ManAlloc( pGia, vOrder );
347 Gia_ManForEachCoReverse( pGia, pObj, i )
348 {
349 Vec_IntFill( p->vTempC[0], 1, i+1 ); // 2^i
350 Vec_IntFill( p->vTempC[1], 1, -i-1 ); // -2^i
351
352 iDriver = Gia_ObjFaninId0p( pGia, pObj );
353 Vec_IntFill( p->vTempM[0], 1, iDriver ); // Driver
354
355 if ( fSigned && i == Gia_ManCoNum(pGia)-1 )
356 {
357 if ( Gia_ObjFaninC0(pObj) )
358 {
359 Gia_PolynBuildAdd( p, p->vTempC[1], NULL ); // -C
360 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[0] ); // C * Driver
361 }
362 else
363 Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[0] ); // -C * Driver
364 }
365 else
366 {
367 if ( Gia_ObjFaninC0(pObj) )
368 {
369 Gia_PolynBuildAdd( p, p->vTempC[0], NULL ); // C
370 Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[0] ); // -C * Driver
371 }
372 else
373 Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[0] ); // C * Driver
374 }
375 }
376 LevPrev = -1;
377 for ( Iter = 0; ; Iter++ )
378 {
379 Vec_Int_t * vTempM;
380 //abctime temp = Abc_Clock();
381 if ( Vec_QueSize(p->vQue) == 0 )
382 break;
383 iMono = Vec_QuePop(p->vQue);
384
385 // report
386 vTempM = Hsh_VecReadEntry( p->pHashM, iMono );
387 //printf( "Removing var %d\n", Vec_IntEntryLast(vTempM) );
388 LevCur = Vec_IntEntryLast(vTempM);
389 if ( !Gia_ObjIsAnd(Gia_ManObj(pGia, LevCur)) )
390 continue;
391
392 if ( LevPrev != LevCur )
393 {
394 if ( Vec_BitEntry( vPres, LevCur ) && fVerbose )
395 printf( "Repeating entry %d\n", LevCur );
396 else
397 Vec_BitSetEntry( vPres, LevCur, 1 );
398
399 if ( fVeryVerbose )
400 printf( "Line%5d Iter%10d : Obj =%6d. Order =%6d. HashC =%6d. HashM =%10d. Total =%10d. Used =%10d.\n",
401 Line++, Iter, LevCur, Vec_IntEntry(p->vOrder, LevCur), Hsh_VecSize(p->pHashC), Hsh_VecSize(p->pHashM), p->nBuilds, p->nUsed );
402 }
403 LevPrev = LevCur;
404
405 Gia_PolynBuildOne( p, iMono );
406 //clk2 += Abc_Clock() - temp;
407 }
408 //Abc_PrintTime( 1, "Time2", clk2 );
409 Pln_ManPrintFinal( p, fVerbose, fVeryVerbose );
410 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
411 Pln_ManStop( p );
412 Vec_BitFree( vPres );
413}
414
415
416
428void Gia_PolynBuild2( Gia_Man_t * pGia, int fSigned, int fVerbose, int fVeryVerbose )
429{
430 Hsh_VecMan_t * pHashC = Hsh_VecManStart( 1000 ); // hash table for constants
431 Hsh_VecMan_t * pHashM = Hsh_VecManStart( 1000 ); // hash table for monomials
432 //Vec_Wec_t * vLit2Mono = Vec_WecStart( Gia_ManObjNum(pGia) * 2 );
433
434 Hsh_VecManStop( pHashC );
435 Hsh_VecManStop( pHashM );
436}
437
438
442
443
445
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
void Pln_ManStop(Pln_Man_t *p)
Definition acecPolyn.c:101
void Pln_ManPrintFinal(Pln_Man_t *p, int fVerbose, int fVeryVerbose)
Definition acecPolyn.c:125
void Gia_PolynBuildOne(Pln_Man_t *p, int iMono)
Definition acecPolyn.c:258
int Pln_ManCompare3(int *pData0, int *pData1)
Definition acecPolyn.c:117
typedefABC_NAMESPACE_IMPL_START struct Pln_Man_t_ Pln_Man_t
DECLARATIONS ///.
Definition acecPolyn.c:45
Pln_Man_t * Pln_ManAlloc(Gia_Man_t *pGia, Vec_Int_t *vOrder)
FUNCTION DEFINITIONS ///.
Definition acecPolyn.c:76
void Gia_PolynBuild(Gia_Man_t *pGia, Vec_Int_t *vOrder, int fSigned, int fVerbose, int fVeryVerbose)
Definition acecPolyn.c:340
void Gia_PolynBuild2(Gia_Man_t *pGia, int fSigned, int fVerbose, int fVeryVerbose)
Definition acecPolyn.c:428
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
#define Gia_ManForEachCoReverse(p, pObj, i)
Definition gia.h:1242
struct Gia_Obj_t_ Gia_Obj_t
Definition gia.h:76
struct Gia_Man_t_ Gia_Man_t
Definition gia.h:96
Vec_Int_t * vTempC[2]
Definition acecPolyn.c:54
Vec_Int_t * vTempM[4]
Definition acecPolyn.c:55
Vec_Int_t * vOrder
Definition acecPolyn.c:56
int nBuilds
Definition acecPolyn.c:57
Vec_Flt_t * vCounts
Definition acecPolyn.c:52
Gia_Man_t * pGia
Definition acecPolyn.c:48
Hsh_VecMan_t * pHashC
Definition acecPolyn.c:49
Hsh_VecMan_t * pHashM
Definition acecPolyn.c:50
Vec_Que_t * vQue
Definition acecPolyn.c:51
Vec_Int_t * vCoefs
Definition acecPolyn.c:53
#define assert(ex)
Definition util_old.h:213
typedefABC_NAMESPACE_HEADER_START struct Vec_Bit_t_ Vec_Bit_t
INCLUDES ///.
Definition vecBit.h:42
typedefABC_NAMESPACE_HEADER_START struct Vec_Flt_t_ Vec_Flt_t
INCLUDES ///.
Definition vecFlt.h:42
struct Hsh_VecMan_t_ Hsh_VecMan_t
Definition vecHsh.h:85
#define Vec_IntForEachEntryDouble(vVec, Entry1, Entry2, i)
Definition vecInt.h:72
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54
typedefABC_NAMESPACE_HEADER_START struct Vec_Que_t_ Vec_Que_t
INCLUDES ///.
Definition vecQue.h:40