abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hopTruth.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [hopTruth.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Minimalistic And-Inverter Graph package.]
8 
9  Synopsis []
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - May 11, 2006.]
16 
17  Revision [$Id: hopTruth.c,v 1.00 2006/05/11 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "hop.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 static inline int Hop_ManTruthWordNum( int nVars ) { return nVars <= 5 ? 1 : (1 << (nVars - 5)); }
31 
32 static inline void Hop_ManTruthCopy( unsigned * pOut, unsigned * pIn, int nVars )
33 {
34  int w;
35  for ( w = Hop_ManTruthWordNum(nVars)-1; w >= 0; w-- )
36  pOut[w] = pIn[w];
37 }
38 static inline void Hop_ManTruthClear( unsigned * pOut, int nVars )
39 {
40  int w;
41  for ( w = Hop_ManTruthWordNum(nVars)-1; w >= 0; w-- )
42  pOut[w] = 0;
43 }
44 static inline void Hop_ManTruthFill( unsigned * pOut, int nVars )
45 {
46  int w;
47  for ( w = Hop_ManTruthWordNum(nVars)-1; w >= 0; w-- )
48  pOut[w] = ~(unsigned)0;
49 }
50 static inline void Hop_ManTruthNot( unsigned * pOut, unsigned * pIn, int nVars )
51 {
52  int w;
53  for ( w = Hop_ManTruthWordNum(nVars)-1; w >= 0; w-- )
54  pOut[w] = ~pIn[w];
55 }
56 
57 ////////////////////////////////////////////////////////////////////////
58 /// FUNCTION DEFINITIONS ///
59 ////////////////////////////////////////////////////////////////////////
60 
61 
62 /**Function*************************************************************
63 
64  Synopsis [Construct BDDs and mark AIG nodes.]
65 
66  Description []
67 
68  SideEffects []
69 
70  SeeAlso []
71 
72 ***********************************************************************/
74 {
75  int Counter = 0;
76  assert( !Hop_IsComplement(pObj) );
77  if ( !Hop_ObjIsNode(pObj) || Hop_ObjIsMarkA(pObj) )
78  return 0;
79  Counter += Hop_ManConvertAigToTruth_rec1( Hop_ObjFanin0(pObj) );
80  Counter += Hop_ManConvertAigToTruth_rec1( Hop_ObjFanin1(pObj) );
81  assert( !Hop_ObjIsMarkA(pObj) ); // loop detection
82  Hop_ObjSetMarkA( pObj );
83  return Counter + 1;
84 }
85 
86 /**Function*************************************************************
87 
88  Synopsis [Computes truth table of the cut.]
89 
90  Description []
91 
92  SideEffects []
93 
94  SeeAlso []
95 
96 ***********************************************************************/
97 unsigned * Hop_ManConvertAigToTruth_rec2( Hop_Obj_t * pObj, Vec_Int_t * vTruth, int nWords )
98 {
99  unsigned * pTruth, * pTruth0, * pTruth1;
100  int i;
101  assert( !Hop_IsComplement(pObj) );
102  if ( !Hop_ObjIsNode(pObj) || !Hop_ObjIsMarkA(pObj) )
103  return (unsigned *)pObj->pData;
104  // compute the truth tables of the fanins
105  pTruth0 = Hop_ManConvertAigToTruth_rec2( Hop_ObjFanin0(pObj), vTruth, nWords );
106  pTruth1 = Hop_ManConvertAigToTruth_rec2( Hop_ObjFanin1(pObj), vTruth, nWords );
107  // creat the truth table of the node
108  pTruth = Vec_IntFetch( vTruth, nWords );
109  if ( Hop_ObjIsExor(pObj) )
110  for ( i = 0; i < nWords; i++ )
111  pTruth[i] = pTruth0[i] ^ pTruth1[i];
112  else if ( !Hop_ObjFaninC0(pObj) && !Hop_ObjFaninC1(pObj) )
113  for ( i = 0; i < nWords; i++ )
114  pTruth[i] = pTruth0[i] & pTruth1[i];
115  else if ( !Hop_ObjFaninC0(pObj) && Hop_ObjFaninC1(pObj) )
116  for ( i = 0; i < nWords; i++ )
117  pTruth[i] = pTruth0[i] & ~pTruth1[i];
118  else if ( Hop_ObjFaninC0(pObj) && !Hop_ObjFaninC1(pObj) )
119  for ( i = 0; i < nWords; i++ )
120  pTruth[i] = ~pTruth0[i] & pTruth1[i];
121  else // if ( Hop_ObjFaninC0(pObj) && Hop_ObjFaninC1(pObj) )
122  for ( i = 0; i < nWords; i++ )
123  pTruth[i] = ~pTruth0[i] & ~pTruth1[i];
124  assert( Hop_ObjIsMarkA(pObj) ); // loop detection
125  Hop_ObjClearMarkA( pObj );
126  pObj->pData = pTruth;
127  return pTruth;
128 }
129 
130 /**Function*************************************************************
131 
132  Synopsis [Computes truth table of the node.]
133 
134  Description [Assumes that the structural support is no more than 8 inputs.
135  Uses array vTruth to store temporary truth tables. The returned pointer should
136  be used immediately.]
137 
138  SideEffects []
139 
140  SeeAlso []
141 
142 ***********************************************************************/
143 unsigned * Hop_ManConvertAigToTruth( Hop_Man_t * p, Hop_Obj_t * pRoot, int nVars, Vec_Int_t * vTruth, int fMsbFirst )
144 {
145  static unsigned uTruths[8][8] = { // elementary truth tables
146  { 0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA },
147  { 0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC },
148  { 0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0 },
149  { 0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00 },
150  { 0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000 },
151  { 0x00000000,0xFFFFFFFF,0x00000000,0xFFFFFFFF,0x00000000,0xFFFFFFFF,0x00000000,0xFFFFFFFF },
152  { 0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFF,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFF },
153  { 0x00000000,0x00000000,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF }
154  };
155  Hop_Obj_t * pObj;
156  unsigned * pTruth, * pTruth2;
157  int i, nWords, nNodes;
158  Vec_Ptr_t * vTtElems;
159 
160  // if the number of variables is more than 8, allocate truth tables
161  if ( nVars > 8 )
162  vTtElems = Vec_PtrAllocTruthTables( nVars );
163  else
164  vTtElems = NULL;
165 
166  // clear the data fields and set marks
167  nNodes = Hop_ManConvertAigToTruth_rec1( Hop_Regular(pRoot) );
168  // prepare memory
169  nWords = Hop_TruthWordNum( nVars );
170  Vec_IntClear( vTruth );
171  Vec_IntGrow( vTruth, nWords * (nNodes+1) );
172  pTruth = Vec_IntFetch( vTruth, nWords );
173  // check the case of a constant
174  if ( Hop_ObjIsConst1( Hop_Regular(pRoot) ) )
175  {
176  assert( nNodes == 0 );
177  if ( Hop_IsComplement(pRoot) )
178  Hop_ManTruthClear( pTruth, nVars );
179  else
180  Hop_ManTruthFill( pTruth, nVars );
181  return pTruth;
182  }
183  // set elementary truth tables at the leaves
184  assert( nVars <= Hop_ManPiNum(p) );
185 // assert( Hop_ManPiNum(p) <= 8 );
186  if ( fMsbFirst )
187  {
188 // Hop_ManForEachPi( p, pObj, i )
189  for ( i = 0; i < nVars; i++ )
190  {
191  pObj = Hop_ManPi( p, i );
192  if ( vTtElems )
193  pObj->pData = Vec_PtrEntry(vTtElems, nVars-1-i);
194  else
195  pObj->pData = (void *)uTruths[nVars-1-i];
196  }
197  }
198  else
199  {
200 // Hop_ManForEachPi( p, pObj, i )
201  for ( i = 0; i < nVars; i++ )
202  {
203  pObj = Hop_ManPi( p, i );
204  if ( vTtElems )
205  pObj->pData = Vec_PtrEntry(vTtElems, i);
206  else
207  pObj->pData = (void *)uTruths[i];
208  }
209  }
210  // clear the marks and compute the truth table
211  pTruth2 = Hop_ManConvertAigToTruth_rec2( Hop_Regular(pRoot), vTruth, nWords );
212  // copy the result
213  Hop_ManTruthCopy( pTruth, pTruth2, nVars );
214  if ( Hop_IsComplement(pRoot) )
215  Hop_ManTruthNot( pTruth, pTruth, nVars );
216  if ( vTtElems )
217  Vec_PtrFree( vTtElems );
218  return pTruth;
219 }
220 
221 
222 /**Function*************************************************************
223 
224  Synopsis [Compute truth table.]
225 
226  Description []
227 
228  SideEffects []
229 
230  SeeAlso []
231 
232 ***********************************************************************/
233 static word Truth[8] =
234 {
235  ABC_CONST(0xAAAAAAAAAAAAAAAA),
236  ABC_CONST(0xCCCCCCCCCCCCCCCC),
237  ABC_CONST(0xF0F0F0F0F0F0F0F0),
238  ABC_CONST(0xFF00FF00FF00FF00),
239  ABC_CONST(0xFFFF0000FFFF0000),
240  ABC_CONST(0xFFFFFFFF00000000),
241  ABC_CONST(0x0000000000000000),
242  ABC_CONST(0xFFFFFFFFFFFFFFFF)
243 };
245 {
246  word Truth0, Truth1;
247  if ( Hop_ObjIsPi(pObj) )
248  return Truth[pObj->iData];
249  assert( Hop_ObjIsNode(pObj) );
250  Truth0 = Hop_ManComputeTruth6_rec( p, Hop_ObjFanin0(pObj) );
251  Truth1 = Hop_ManComputeTruth6_rec( p, Hop_ObjFanin1(pObj) );
252  Truth0 = Hop_ObjFaninC0(pObj) ? ~Truth0 : Truth0;
253  Truth1 = Hop_ObjFaninC1(pObj) ? ~Truth1 : Truth1;
254  return Truth0 & Truth1;
255 }
257 {
258  word Truth;
259  int i;
260  if ( Hop_ObjIsConst1( Hop_Regular(pObj) ) )
261  return Hop_IsComplement(pObj) ? 0 : ~(word)0;
262  for ( i = 0; i < nVars; i++ )
263  Hop_ManPi( p, i )->iData = i;
264  Truth = Hop_ManComputeTruth6_rec( p, Hop_Regular(pObj) );
265  return Hop_IsComplement(pObj) ? ~Truth : Truth;
266 }
267 
268 ////////////////////////////////////////////////////////////////////////
269 /// END OF FILE ///
270 ////////////////////////////////////////////////////////////////////////
271 
272 
274 
static Hop_Obj_t * Hop_ObjFanin1(Hop_Obj_t *pObj)
Definition: hop.h:183
static int Hop_ObjIsMarkA(Hop_Obj_t *pObj)
Definition: hop.h:164
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static void Hop_ObjSetMarkA(Hop_Obj_t *pObj)
Definition: hop.h:165
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static word Truth[8]
Definition: hopTruth.c:233
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static int Hop_ObjIsPi(Hop_Obj_t *pObj)
Definition: hop.h:156
static int Hop_ObjIsNode(Hop_Obj_t *pObj)
Definition: hop.h:160
int iData
Definition: hop.h:69
unsigned * Hop_ManConvertAigToTruth_rec2(Hop_Obj_t *pObj, Vec_Int_t *vTruth, int nWords)
Definition: hopTruth.c:97
static unsigned * Vec_IntFetch(Vec_Int_t *p, int nWords)
Definition: vecInt.h:853
static int Hop_ObjFaninC1(Hop_Obj_t *pObj)
Definition: hop.h:181
int nWords
Definition: abcNpn.c:127
Definition: hop.h:65
static ABC_NAMESPACE_IMPL_START int Hop_ManTruthWordNum(int nVars)
DECLARATIONS ///.
Definition: hopTruth.c:30
static Vec_Ptr_t * Vec_PtrAllocTruthTables(int nVars)
Definition: vecPtr.h:1065
static void Vec_IntGrow(Vec_Int_t *p, int nCapMin)
Definition: bblif.c:336
static Hop_Obj_t * Hop_ManPi(Hop_Man_t *p, int i)
Definition: hop.h:134
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static int Hop_IsComplement(Hop_Obj_t *p)
Definition: hop.h:129
static int Counter
static void Hop_ObjClearMarkA(Hop_Obj_t *pObj)
Definition: hop.h:166
void * pData
Definition: hop.h:68
static int Hop_ObjIsExor(Hop_Obj_t *pObj)
Definition: hop.h:159
static Hop_Obj_t * Hop_ObjFanin0(Hop_Obj_t *pObj)
Definition: hop.h:182
static int Hop_ObjIsConst1(Hop_Obj_t *pObj)
Definition: hop.h:155
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
static void Hop_ManTruthClear(unsigned *pOut, int nVars)
Definition: hopTruth.c:38
static int Hop_TruthWordNum(int nVars)
Definition: hop.h:119
static int Hop_ObjFaninC0(Hop_Obj_t *pObj)
Definition: hop.h:180
#define ABC_CONST(number)
PARAMETERS ///.
Definition: abc_global.h:206
static void Hop_ManTruthFill(unsigned *pOut, int nVars)
Definition: hopTruth.c:44
static void Hop_ManTruthCopy(unsigned *pOut, unsigned *pIn, int nVars)
Definition: hopTruth.c:32
word Hop_ManComputeTruth6(Hop_Man_t *p, Hop_Obj_t *pObj, int nVars)
Definition: hopTruth.c:256
word Hop_ManComputeTruth6_rec(Hop_Man_t *p, Hop_Obj_t *pObj)
Definition: hopTruth.c:244
#define assert(ex)
Definition: util_old.h:213
static void Hop_ManTruthNot(unsigned *pOut, unsigned *pIn, int nVars)
Definition: hopTruth.c:50
static Hop_Obj_t * Hop_Regular(Hop_Obj_t *p)
Definition: hop.h:126
static void Vec_IntClear(Vec_Int_t *p)
Definition: bblif.c:452
unsigned * Hop_ManConvertAigToTruth(Hop_Man_t *p, Hop_Obj_t *pRoot, int nVars, Vec_Int_t *vTruth, int fMsbFirst)
Definition: hopTruth.c:143
typedefABC_NAMESPACE_HEADER_START struct Hop_Man_t_ Hop_Man_t
INCLUDES ///.
Definition: hop.h:49
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223
static int Hop_ManPiNum(Hop_Man_t *p)
Definition: hop.h:145
int Hop_ManConvertAigToTruth_rec1(Hop_Obj_t *pObj)
FUNCTION DEFINITIONS ///.
Definition: hopTruth.c:73