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
|
/**CFile****************************************************************
FileName [giaTruth.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Scalable AIG package.]
Synopsis [Truth table computation.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: giaTruth.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
***********************************************************************/
#include "gia.h"
ABC_NAMESPACE_IMPL_START
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
static inline word * Gla_ObjTruthElem( Gia_Man_t * p, int i ) { return (word *)Vec_PtrEntry( p->vTtInputs, i ); }
static inline word * Gla_ObjTruthNode( Gia_Man_t * p, Gia_Obj_t * pObj ) { return Vec_WrdArray(p->vTtMemory) + p->nTtWords * Gia_ObjNum(p, pObj); }
static inline word * Gla_ObjTruthFree1( Gia_Man_t * p ) { return Vec_WrdArray(p->vTtMemory) + p->nTtWords * 254; }
static inline word * Gla_ObjTruthFree2( Gia_Man_t * p ) { return Vec_WrdArray(p->vTtMemory) + p->nTtWords * 255; }
static inline word * Gla_ObjTruthConst0( Gia_Man_t * p, word * pDst ) { int w; for ( w = 0; w < p->nTtWords; w++ ) pDst[w] = 0; return pDst; }
static inline word * Gla_ObjTruthDup( Gia_Man_t * p, word * pDst, word * pSrc, int c ) { int w; for ( w = 0; w < p->nTtWords; w++ ) pDst[w] = c ? ~pSrc[w] : pSrc[w]; return pDst; }
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis [Computes truth table up to 6 inputs.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Gia_ObjComputeTruthTable6_rec( Gia_Man_t * p, Gia_Obj_t * pObj, Vec_Wrd_t * vTruths )
{
word uTruth0, uTruth1;
if ( Gia_ObjIsTravIdCurrent(p, pObj) )
return;
Gia_ObjSetTravIdCurrent(p, pObj);
assert( !pObj->fMark0 );
assert( Gia_ObjIsAnd(pObj) );
Gia_ObjComputeTruthTable6_rec( p, Gia_ObjFanin0(pObj), vTruths );
Gia_ObjComputeTruthTable6_rec( p, Gia_ObjFanin1(pObj), vTruths );
uTruth0 = Vec_WrdEntry( vTruths, Gia_ObjFanin0(pObj)->Value );
uTruth0 = Gia_ObjFaninC0(pObj) ? ~uTruth0 : uTruth0;
uTruth1 = Vec_WrdEntry( vTruths, Gia_ObjFanin1(pObj)->Value );
uTruth1 = Gia_ObjFaninC1(pObj) ? ~uTruth1 : uTruth1;
pObj->Value = Vec_WrdSize(vTruths);
Vec_WrdPush( vTruths, uTruth0 & uTruth1 );
}
word Gia_ObjComputeTruthTable6( Gia_Man_t * p, Gia_Obj_t * pObj, Vec_Int_t * vSupp, Vec_Wrd_t * vTruths )
{
static word s_Truth6[6] = {
0xAAAAAAAAAAAAAAAA,
0xCCCCCCCCCCCCCCCC,
0xF0F0F0F0F0F0F0F0,
0xFF00FF00FF00FF00,
0xFFFF0000FFFF0000,
0xFFFFFFFF00000000
};
Gia_Obj_t * pLeaf;
int i;
assert( Vec_IntSize(vSupp) <= 6 );
assert( Gia_ObjIsAnd(pObj) );
assert( !pObj->fMark0 );
Vec_WrdClear( vTruths );
Gia_ManIncrementTravId( p );
Gia_ManForEachObjVec( vSupp, p, pLeaf, i )
{
assert( pLeaf->fMark0 || Gia_ObjIsRo(p, pLeaf) );
pLeaf->Value = Vec_WrdSize(vTruths);
Vec_WrdPush( vTruths, s_Truth6[i] );
Gia_ObjSetTravIdCurrent(p, pLeaf);
}
Gia_ObjComputeTruthTable6_rec( p, pObj, vTruths );
return Vec_WrdEntryLast( vTruths );
}
/**Function*************************************************************
Synopsis [Collects internal nodes reachable from the given node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Gia_ObjCollectInternal_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
if ( !Gia_ObjIsAnd(pObj) )
return 0;
if ( pObj->fMark0 )
return 0;
pObj->fMark0 = 1;
Gia_ObjCollectInternal_rec( p, Gia_ObjFanin0(pObj) );
Gia_ObjCollectInternal_rec( p, Gia_ObjFanin1(pObj) );
if ( Vec_IntSize(p->vTtNodes) > 253 )
return 1;
Gia_ObjSetNum( p, pObj, Vec_IntSize(p->vTtNodes) );
Vec_IntPush( p->vTtNodes, Gia_ObjId(p, pObj) );
return 0;
}
int Gia_ObjCollectInternal( Gia_Man_t * p, Gia_Obj_t * pObj )
{
int RetValue;
Vec_IntClear( p->vTtNodes );
RetValue = Gia_ObjCollectInternal_rec( p, pObj );
assert( Vec_IntSize(p->vTtNodes) < 254 );
return RetValue;
}
/**Function*************************************************************
Synopsis [Computing the truth table for GIA object.]
Description [The truth table should be used by the calling application
(or saved into the user's storage) before this procedure is called again.]
SideEffects []
SeeAlso []
***********************************************************************/
word * Gia_ObjComputeTruthTable( Gia_Man_t * p, Gia_Obj_t * pObj )
{
Gia_Obj_t * pTemp, * pRoot;
word * pTruth, * pTruthL, * pTruth0, * pTruth1;
int i;
if ( p->vTtMemory == NULL )
{
p->nTtVars = Gia_ManPiNum( p );
p->nTtWords = (p->nTtVars <= 6 ? 1 : (1 << (p->nTtVars - 6)));
p->vTtNums = Vec_StrAlloc( Gia_ManObjNum(p) + 1000 );
p->vTtNodes = Vec_IntAlloc( 256 );
p->vTtInputs = Vec_PtrAllocTruthTables( p->nTtVars );
p->vTtMemory = Vec_WrdStart( p->nTtWords * 256 );
}
else
{
// make sure the number of primary inputs did not change
// since the truth table computation storage was prepared
assert( p->nTtVars == Gia_ManPiNum(p) );
}
// collect internal nodes
pRoot = Gia_ObjIsCo(pObj) ? Gia_ObjFanin0(pObj) : pObj;
if ( Gia_ObjCollectInternal( p, pRoot ) )
{
Gia_ManForEachObjVec( p->vTtNodes, p, pTemp, i )
pTemp->fMark0 = 0;
return NULL;
}
// compute the truth table for internal nodes
Gia_ManForEachObjVec( p->vTtNodes, p, pTemp, i )
{
pTemp->fMark0 = 0; // unmark nodes marked by Gia_ObjCollectInternal()
pTruth = Gla_ObjTruthNode(p, pTemp);
pTruthL = pTruth + p->nTtWords;
pTruth0 = Gia_ObjIsAnd(Gia_ObjFanin0(pTemp)) ? Gla_ObjTruthNode(p, Gia_ObjFanin0(pTemp)) : Gla_ObjTruthElem(p, Gia_ObjCioId(Gia_ObjFanin0(pTemp)) );
pTruth1 = Gia_ObjIsAnd(Gia_ObjFanin1(pTemp)) ? Gla_ObjTruthNode(p, Gia_ObjFanin1(pTemp)) : Gla_ObjTruthElem(p, Gia_ObjCioId(Gia_ObjFanin1(pTemp)) );
if ( Gia_ObjFaninC0(pTemp) )
{
if ( Gia_ObjFaninC1(pTemp) )
while ( pTruth < pTruthL )
*pTruth++ = ~*pTruth0++ & ~*pTruth1++;
else
while ( pTruth < pTruthL )
*pTruth++ = ~*pTruth0++ & *pTruth1++;
}
else
{
if ( Gia_ObjFaninC1(pTemp) )
while ( pTruth < pTruthL )
*pTruth++ = *pTruth0++ & ~*pTruth1++;
else
while ( pTruth < pTruthL )
*pTruth++ = *pTruth0++ & *pTruth1++;
}
}
// compute the final table
if ( Gia_ObjIsConst0(pRoot) )
pTruth = Gla_ObjTruthConst0( p, Gla_ObjTruthFree1(p) );
else if ( Gia_ObjIsPi(p, pRoot) )
pTruth = Gla_ObjTruthElem( p, Gia_ObjCioId(pRoot) );
else if ( Gia_ObjIsAnd(pRoot) )
pTruth = Gla_ObjTruthNode( p, pRoot );
else
pTruth = NULL;
return Gla_ObjTruthDup( p, Gla_ObjTruthFree2(p), pTruth, Gia_ObjIsCo(pObj) && Gia_ObjFaninC0(pObj) );
}
/**Function*************************************************************
Synopsis [Testing truth table computation.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Gia_ObjComputeTruthTableTest( Gia_Man_t * p )
{
Gia_Obj_t * pObj;
unsigned * pTruth;
clock_t clk = clock();
int i;
Gia_ManForEachPo( p, pObj, i )
{
pTruth = (unsigned *)Gia_ObjComputeTruthTable( p, pObj );
// Extra_PrintHex( stdout, pTruth, Gia_ManPiNum(p) ); printf( "\n" );
}
Abc_PrintTime( 1, "Time", clock() - clk );
}
/**Function*************************************************************
Synopsis [Collects internal nodes reachable from the given node.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Gia_ObjCollectInternalCut_rec( Gia_Man_t * p, Gia_Obj_t * pObj )
{
if ( pObj->fMark0 )
return 0;
assert( Gia_ObjIsAnd(pObj) );
if ( Gia_ObjCollectInternalCut_rec( p, Gia_ObjFanin0(pObj) ) )
return 1;
if ( Gia_ObjCollectInternalCut_rec( p, Gia_ObjFanin1(pObj) ) )
return 1;
pObj->fMark0 = 1;
Gia_ObjSetNum( p, pObj, Vec_IntSize(p->vTtNodes) );
Vec_IntPush( p->vTtNodes, Gia_ObjId(p, pObj) );
return (Vec_IntSize(p->vTtNodes) >= 254);
}
int Gia_ObjCollectInternalCut( Gia_Man_t * p, Gia_Obj_t * pRoot, Vec_Int_t * vLeaves )
{
Gia_Obj_t * pObj;
int i;
assert( Gia_ObjIsAnd(pRoot) );
assert( pRoot->fMark0 == 0 );
Gia_ManForEachObjVec( vLeaves, p, pObj, i )
{
assert( pObj->fMark0 == 0 );
pObj->fMark0 = 1;
Gia_ObjSetNum( p, pObj, i );
}
assert( pRoot->fMark0 == 0 ); // the root cannot be one of the leaves
Vec_IntClear( p->vTtNodes );
return Gia_ObjCollectInternalCut_rec( p, pRoot );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Gia_ObjComputeTruthTableStart( Gia_Man_t * p, int nVarsMax )
{
assert( p->vTtMemory == NULL );
p->nTtVars = nVarsMax;
p->nTtWords = (p->nTtVars <= 6 ? 1 : (1 << (p->nTtVars - 6)));
p->vTtNums = Vec_StrAlloc( Gia_ManObjNum(p) + 1000 );
p->vTtNodes = Vec_IntAlloc( 256 );
p->vTtInputs = Vec_PtrAllocTruthTables( p->nTtVars );
p->vTtMemory = Vec_WrdStart( p->nTtWords * 256 );
}
void Gia_ObjComputeTruthTableStop( Gia_Man_t * p )
{
p->nTtVars = 0;
p->nTtWords = 0;
Vec_StrFreeP( &p->vTtNums );
Vec_IntFreeP( &p->vTtNodes );
Vec_PtrFreeP( &p->vTtInputs );
Vec_WrdFreeP( &p->vTtMemory );
}
/**Function*************************************************************
Synopsis [Computes the truth table of pRoot in terms of leaves.]
Description [The root cannot be one of the leaves.]
SideEffects []
SeeAlso []
***********************************************************************/
word * Gia_ObjComputeTruthTableCut( Gia_Man_t * p, Gia_Obj_t * pRoot, Vec_Int_t * vLeaves )
{
Gia_Obj_t * pTemp;
word * pTruth, * pTruthL, * pTruth0, * pTruth1;
int i;
assert( p->vTtMemory != NULL );
assert( Vec_IntSize(vLeaves) <= p->nTtVars );
// collect internal nodes
if ( Gia_ObjCollectInternalCut( p, pRoot, vLeaves ) )
{
// unmark nodes makred by Gia_ObjCollectInternal()
Gia_ManForEachObjVec( p->vTtNodes, p, pTemp, i )
pTemp->fMark0 = 0;
// unmark leaves marked by Gia_ObjCollectInternal()
Gia_ManForEachObjVec( vLeaves, p, pTemp, i )
{
assert( pTemp->fMark0 == 1 );
pTemp->fMark0 = 0;
}
return NULL;
}
// compute the truth table for internal nodes
assert( Vec_IntSize(p->vTtNodes) < 254 );
Gia_ManForEachObjVec( p->vTtNodes, p, pTemp, i )
{
pTemp->fMark0 = 0; // unmark nodes marked by Gia_ObjCollectInternal()
pTruth = Gla_ObjTruthNode(p, pTemp);
pTruthL = pTruth + p->nTtWords;
pTruth0 = !Gia_ObjFanin0(pTemp)->fMark0 ? Gla_ObjTruthNode(p, Gia_ObjFanin0(pTemp)) : Gla_ObjTruthElem(p, Gia_ObjNum(p, Gia_ObjFanin0(pTemp)) );
pTruth1 = !Gia_ObjFanin1(pTemp)->fMark0 ? Gla_ObjTruthNode(p, Gia_ObjFanin1(pTemp)) : Gla_ObjTruthElem(p, Gia_ObjNum(p, Gia_ObjFanin1(pTemp)) );
if ( Gia_ObjFaninC0(pTemp) )
{
if ( Gia_ObjFaninC1(pTemp) )
while ( pTruth < pTruthL )
*pTruth++ = ~*pTruth0++ & ~*pTruth1++;
else
while ( pTruth < pTruthL )
*pTruth++ = ~*pTruth0++ & *pTruth1++;
}
else
{
if ( Gia_ObjFaninC1(pTemp) )
while ( pTruth < pTruthL )
*pTruth++ = *pTruth0++ & ~*pTruth1++;
else
while ( pTruth < pTruthL )
*pTruth++ = *pTruth0++ & *pTruth1++;
}
}
// unmark leaves marked by Gia_ObjCollectInternal()
Gia_ManForEachObjVec( vLeaves, p, pTemp, i )
{
assert( pTemp->fMark0 == 1 );
pTemp->fMark0 = 0;
}
return Gla_ObjTruthNode( p, pRoot );
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|