abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
giaAigerExt.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [giaAigerExt.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Scalable AIG package.]
8 
9  Synopsis [Custom AIGER extensions.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: giaAigerExt.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "gia.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Read/write equivalence classes information.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
45 Gia_Rpr_t * Gia_AigerReadEquivClasses( unsigned char ** ppPos, int nSize )
46 {
47  Gia_Rpr_t * pReprs;
48  unsigned char * pStop;
49  int i, Item, fProved, iRepr, iNode;
50  pStop = *ppPos;
51  pStop += Gia_AigerReadInt( *ppPos ); *ppPos += 4;
52  pReprs = ABC_CALLOC( Gia_Rpr_t, nSize );
53  for ( i = 0; i < nSize; i++ )
54  pReprs[i].iRepr = GIA_VOID;
55  iRepr = iNode = 0;
56  while ( *ppPos < pStop )
57  {
58  Item = Gia_AigerReadUnsigned( ppPos );
59  if ( Item & 1 )
60  {
61  iRepr += (Item >> 1);
62  iNode = iRepr;
63  continue;
64  }
65  Item >>= 1;
66  fProved = (Item & 1);
67  Item >>= 1;
68  iNode += Item;
69  pReprs[iNode].fProved = fProved;
70  pReprs[iNode].iRepr = iRepr;
71  assert( iRepr < iNode );
72  }
73  return pReprs;
74 }
75 unsigned char * Gia_WriteEquivClassesInt( Gia_Man_t * p, int * pEquivSize )
76 {
77  unsigned char * pBuffer;
78  int iRepr, iNode, iPrevRepr, iPrevNode, iLit, nItems, iPos;
79  assert( p->pReprs && p->pNexts );
80  // count the number of entries to be written
81  nItems = 0;
82  for ( iRepr = 1; iRepr < Gia_ManObjNum(p); iRepr++ )
83  {
84  nItems += Gia_ObjIsConst( p, iRepr );
85  if ( !Gia_ObjIsHead(p, iRepr) )
86  continue;
87  Gia_ClassForEachObj( p, iRepr, iNode )
88  nItems++;
89  }
90  pBuffer = ABC_ALLOC( unsigned char, sizeof(int) * (nItems + 10) );
91  // write constant class
92  iPos = Gia_AigerWriteUnsignedBuffer( pBuffer, 4, Abc_Var2Lit(0, 1) );
93  iPrevNode = 0;
94  for ( iNode = 1; iNode < Gia_ManObjNum(p); iNode++ )
95  if ( Gia_ObjIsConst(p, iNode) )
96  {
97  iLit = Abc_Var2Lit( iNode - iPrevNode, Gia_ObjProved(p, iNode) );
98  iPrevNode = iNode;
99  iPos = Gia_AigerWriteUnsignedBuffer( pBuffer, iPos, Abc_Var2Lit(iLit, 0) );
100  }
101  // write non-constant classes
102  iPrevRepr = 0;
103  Gia_ManForEachClass( p, iRepr )
104  {
105  iPos = Gia_AigerWriteUnsignedBuffer( pBuffer, iPos, Abc_Var2Lit(iRepr - iPrevRepr, 1) );
106  iPrevRepr = iPrevNode = iRepr;
107  Gia_ClassForEachObj1( p, iRepr, iNode )
108  {
109  iLit = Abc_Var2Lit( iNode - iPrevNode, Gia_ObjProved(p, iNode) );
110  iPrevNode = iNode;
111  iPos = Gia_AigerWriteUnsignedBuffer( pBuffer, iPos, Abc_Var2Lit(iLit, 0) );
112  }
113  }
114  Gia_AigerWriteInt( pBuffer, iPos );
115  *pEquivSize = iPos;
116  return pBuffer;
117 }
119 {
120  int nEquivSize;
121  unsigned char * pBuffer = Gia_WriteEquivClassesInt( p, &nEquivSize );
122  return Vec_StrAllocArray( (char *)pBuffer, nEquivSize );
123 }
124 
125 /**Function*************************************************************
126 
127  Synopsis [Read/write mapping information.]
128 
129  Description []
130 
131  SideEffects []
132 
133  SeeAlso []
134 
135 ***********************************************************************/
136 static inline unsigned Gia_AigerReadDiffValue( unsigned char ** ppPos, int iPrev )
137 {
138  int Item = Gia_AigerReadUnsigned( ppPos );
139  if ( Item & 1 )
140  return iPrev + (Item >> 1);
141  return iPrev - (Item >> 1);
142 }
143 int * Gia_AigerReadMapping( unsigned char ** ppPos, int nSize )
144 {
145  int * pMapping;
146  unsigned char * pStop;
147  int k, j, nFanins, nAlloc, iNode = 0, iOffset = nSize;
148  pStop = *ppPos;
149  pStop += Gia_AigerReadInt( *ppPos ); *ppPos += 4;
150  nAlloc = nSize + pStop - *ppPos;
151  pMapping = ABC_CALLOC( int, nAlloc );
152  while ( *ppPos < pStop )
153  {
154  k = iOffset;
155  pMapping[k++] = nFanins = Gia_AigerReadUnsigned( ppPos );
156  for ( j = 0; j <= nFanins; j++ )
157  pMapping[k++] = iNode = Gia_AigerReadDiffValue( ppPos, iNode );
158  pMapping[iNode] = iOffset;
159  iOffset = k;
160  }
161  assert( iOffset <= nAlloc );
162  return pMapping;
163 }
164 static inline int Gia_AigerWriteDiffValue( unsigned char * pPos, int iPos, int iPrev, int iThis )
165 {
166  if ( iPrev < iThis )
167  return Gia_AigerWriteUnsignedBuffer( pPos, iPos, Abc_Var2Lit(iThis - iPrev, 1) );
168  return Gia_AigerWriteUnsignedBuffer( pPos, iPos, Abc_Var2Lit(iPrev - iThis, 0) );
169 }
170 unsigned char * Gia_AigerWriteMappingInt( Gia_Man_t * p, int * pMapSize )
171 {
172  unsigned char * pBuffer;
173  int i, k, iPrev, iFan, nItems, iPos = 4;
175  // count the number of entries to be written
176  nItems = 0;
177  Gia_ManForEachLut( p, i )
178  nItems += 2 + Gia_ObjLutSize( p, i );
179  pBuffer = ABC_ALLOC( unsigned char, sizeof(int) * (nItems + 1) );
180  // write non-constant classes
181  iPrev = 0;
182  Gia_ManForEachLut( p, i )
183  {
184 //printf( "\nSize = %d ", Gia_ObjLutSize(p, i) );
185  iPos = Gia_AigerWriteUnsignedBuffer( pBuffer, iPos, Gia_ObjLutSize(p, i) );
186  Gia_LutForEachFanin( p, i, iFan, k )
187  {
188 //printf( "Fan = %d ", iFan );
189  iPos = Gia_AigerWriteDiffValue( pBuffer, iPos, iPrev, iFan );
190  iPrev = iFan;
191  }
192  iPos = Gia_AigerWriteDiffValue( pBuffer, iPos, iPrev, i );
193  iPrev = i;
194 //printf( "Node = %d ", i );
195  }
196 //printf( "\n" );
197  Gia_AigerWriteInt( pBuffer, iPos );
198  *pMapSize = iPos;
199  return pBuffer;
200 }
202 {
203  int nMapSize;
204  unsigned char * pBuffer = Gia_AigerWriteMappingInt( p, &nMapSize );
205  return Vec_StrAllocArray( (char *)pBuffer, nMapSize );
206 }
207 
208 /**Function*************************************************************
209 
210  Synopsis [Read/write mapping information.]
211 
212  Description []
213 
214  SideEffects []
215 
216  SeeAlso []
217 
218 ***********************************************************************/
219 int * Gia_AigerReadMappingSimple( unsigned char ** ppPos, int nSize )
220 {
221  int * pMapping = ABC_ALLOC( int, nSize/4 );
222  memcpy( pMapping, *ppPos, nSize );
223  assert( nSize % 4 == 0 );
224  return pMapping;
225 }
227 {
228  unsigned char * pBuffer = ABC_ALLOC( unsigned char, 4*Vec_IntSize(p->vMapping) );
229  memcpy( pBuffer, Vec_IntArray(p->vMapping), 4*Vec_IntSize(p->vMapping) );
231  return Vec_StrAllocArray( (char *)pBuffer, 4*Vec_IntSize(p->vMapping) );
232 }
233 
234 /**Function*************************************************************
235 
236  Synopsis [Read/write mapping information.]
237 
238  Description []
239 
240  SideEffects []
241 
242  SeeAlso []
243 
244 ***********************************************************************/
245 Vec_Int_t * Gia_AigerReadMappingDoc( unsigned char ** ppPos, int nObjs )
246 {
247  int * pMapping, nLuts, LutSize, iRoot, nFanins, i, k, nOffset;
248  nLuts = Gia_AigerReadInt( *ppPos ); *ppPos += 4;
249  LutSize = Gia_AigerReadInt( *ppPos ); *ppPos += 4;
250  pMapping = ABC_CALLOC( int, nObjs + (LutSize + 2) * nLuts );
251  nOffset = nObjs;
252  for ( i = 0; i < nLuts; i++ )
253  {
254  iRoot = Gia_AigerReadInt( *ppPos ); *ppPos += 4;
255  nFanins = Gia_AigerReadInt( *ppPos ); *ppPos += 4;
256  pMapping[iRoot] = nOffset;
257  // write one
258  pMapping[ nOffset++ ] = nFanins;
259  for ( k = 0; k < nFanins; k++ )
260  {
261  pMapping[ nOffset++ ] = Gia_AigerReadInt( *ppPos ); *ppPos += 4;
262  }
263  pMapping[ nOffset++ ] = iRoot;
264  }
265  return Vec_IntAllocArray( pMapping, nOffset );
266 }
268 {
269  unsigned char * pBuffer;
270  int i, k, iFan, nLuts = 0, LutSize = 0, nSize = 2, nSize2 = 0;
271  Gia_ManForEachLut( p, i )
272  {
273  nLuts++;
274  nSize += Gia_ObjLutSize(p, i) + 2;
275  LutSize = Abc_MaxInt( LutSize, Gia_ObjLutSize(p, i) );
276  }
277  pBuffer = ABC_ALLOC( unsigned char, 4 * nSize );
278  Gia_AigerWriteInt( pBuffer + 4 * nSize2++, nLuts );
279  Gia_AigerWriteInt( pBuffer + 4 * nSize2++, LutSize );
280  Gia_ManForEachLut( p, i )
281  {
282  Gia_AigerWriteInt( pBuffer + 4 * nSize2++, i );
283  Gia_AigerWriteInt( pBuffer + 4 * nSize2++, Gia_ObjLutSize(p, i) );
284  Gia_LutForEachFanin( p, i, iFan, k )
285  Gia_AigerWriteInt( pBuffer + 4 * nSize2++, iFan );
286  }
287  assert( nSize2 == nSize );
288  return Vec_StrAllocArray( (char *)pBuffer, 4*nSize );
289 }
290 
291 /**Function*************************************************************
292 
293  Synopsis [Read/write packing information.]
294 
295  Description []
296 
297  SideEffects []
298 
299  SeeAlso []
300 
301 ***********************************************************************/
302 Vec_Int_t * Gia_AigerReadPacking( unsigned char ** ppPos, int nSize )
303 {
304  Vec_Int_t * vPacking = Vec_IntAlloc( nSize/4 );
305  int i;
306  assert( nSize % 4 == 0 );
307  for ( i = 0; i < nSize/4; i++, *ppPos += 4 )
308  Vec_IntPush( vPacking, Gia_AigerReadInt( *ppPos ) );
309  return vPacking;
310 }
312 {
313  unsigned char * pBuffer = ABC_ALLOC( unsigned char, 4*Vec_IntSize(vPacking) );
314  int i, Entry, nSize = 0;
315  Vec_IntForEachEntry( vPacking, Entry, i )
316  Gia_AigerWriteInt( pBuffer + 4 * nSize++, Entry );
317  return Vec_StrAllocArray( (char *)pBuffer, 4*Vec_IntSize(vPacking) );
318 }
319 
320 ////////////////////////////////////////////////////////////////////////
321 /// END OF FILE ///
322 ////////////////////////////////////////////////////////////////////////
323 
324 
326 
static int * Vec_IntArray(Vec_Int_t *p)
Definition: vecInt.h:328
static void Gia_AigerWriteInt(unsigned char *pPos, int Value)
Definition: gia.h:834
int * pNexts
Definition: gia.h:122
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static Vec_Int_t * Vec_IntAllocArray(int *pArray, int nSize)
Definition: bblif.c:214
Vec_Str_t * Gia_AigerWriteMappingDoc(Gia_Man_t *p)
Definition: giaAigerExt.c:267
static int Abc_Var2Lit(int Var, int fCompl)
Definition: abc_global.h:263
unsigned char * Gia_AigerWriteMappingInt(Gia_Man_t *p, int *pMapSize)
Definition: giaAigerExt.c:170
static int Gia_ObjIsHead(Gia_Man_t *p, int Id)
Definition: gia.h:916
static unsigned Gia_AigerReadDiffValue(unsigned char **ppPos, int iPrev)
Definition: giaAigerExt.c:136
char * memcpy()
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
ABC_NAMESPACE_IMPL_START Gia_Rpr_t * Gia_AigerReadEquivClasses(unsigned char **ppPos, int nSize)
DECLARATIONS ///.
Definition: giaAigerExt.c:45
unsigned iRepr
Definition: gia.h:58
int * Gia_AigerReadMapping(unsigned char **ppPos, int nSize)
Definition: giaAigerExt.c:143
static int Abc_MaxInt(int a, int b)
Definition: abc_global.h:238
#define Gia_ManForEachLut(p, i)
Definition: gia.h:968
unsigned char * Gia_WriteEquivClassesInt(Gia_Man_t *p, int *pEquivSize)
Definition: giaAigerExt.c:75
Vec_Str_t * Gia_AigerWriteMappingSimple(Gia_Man_t *p)
Definition: giaAigerExt.c:226
static int Gia_AigerReadInt(unsigned char *pPos)
Definition: gia.h:827
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static int Gia_ObjLutSize(Gia_Man_t *p, int Id)
Definition: gia.h:953
static int Gia_ObjIsConst(Gia_Man_t *p, int Id)
Definition: gia.h:915
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
Vec_Str_t * Gia_AigerWriteMapping(Gia_Man_t *p)
Definition: giaAigerExt.c:201
#define Gia_ClassForEachObj1(p, i, iObj)
Definition: gia.h:933
Vec_Str_t * Gia_WriteEquivClasses(Gia_Man_t *p)
Definition: giaAigerExt.c:118
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
Vec_Int_t * Gia_AigerReadMappingDoc(unsigned char **ppPos, int nObjs)
Definition: giaAigerExt.c:245
#define Gia_ClassForEachObj(p, i, iObj)
Definition: gia.h:931
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define GIA_VOID
Definition: gia.h:45
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
static int Gia_ManHasMapping(Gia_Man_t *p)
Definition: gia.h:951
static int Gia_AigerWriteDiffValue(unsigned char *pPos, int iPos, int iPrev, int iThis)
Definition: giaAigerExt.c:164
Definition: gia.h:95
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
#define Gia_ManForEachClass(p, i)
Definition: gia.h:927
static int Gia_ObjProved(Gia_Man_t *p, int Id)
Definition: gia.h:896
int * Gia_AigerReadMappingSimple(unsigned char **ppPos, int nSize)
Definition: giaAigerExt.c:219
Gia_Rpr_t * pReprs
Definition: gia.h:121
#define assert(ex)
Definition: util_old.h:213
static unsigned Gia_AigerReadUnsigned(unsigned char **ppPos)
Definition: gia.h:840
unsigned fProved
Definition: gia.h:59
static int Gia_AigerWriteUnsignedBuffer(unsigned char *pBuffer, int Pos, unsigned x)
Definition: gia.h:872
Vec_Str_t * Gia_WritePacking(Vec_Int_t *vPacking)
Definition: giaAigerExt.c:311
Vec_Int_t * vMapping
Definition: gia.h:131
Definition: gia.h:56
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecInt.h:54
#define Gia_LutForEachFanin(p, i, iFan, k)
Definition: gia.h:970
static Vec_Str_t * Vec_StrAllocArray(char *pArray, int nSize)
Definition: bblif.c:518
Vec_Int_t * Gia_AigerReadPacking(unsigned char **ppPos, int nSize)
Definition: giaAigerExt.c:302
static int Gia_ManObjNum(Gia_Man_t *p)
Definition: gia.h:388