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 
23 ABC_NAMESPACE_IMPL_START
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 ***********************************************************************/
Gia_AigerReadEquivClasses(unsigned char ** ppPos,int nSize)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 }
Gia_WriteEquivClassesInt(Gia_Man_t * p,int * pEquivSize)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 }
Gia_WriteEquivClasses(Gia_Man_t * p)118 Vec_Str_t * Gia_WriteEquivClasses( Gia_Man_t * p )
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 ***********************************************************************/
Gia_AigerReadDiffValue(unsigned char ** ppPos,int iPrev)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 }
Gia_AigerReadMapping(unsigned char ** ppPos,int nSize)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 }
Gia_AigerWriteDiffValue(unsigned char * pPos,int iPos,int iPrev,int iThis)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 }
Gia_AigerWriteMappingInt(Gia_Man_t * p,int * pMapSize)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;
174     assert( Gia_ManHasMapping(p) );
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 }
Gia_AigerWriteMapping(Gia_Man_t * p)201 Vec_Str_t * Gia_AigerWriteMapping( Gia_Man_t * p )
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 ***********************************************************************/
Gia_AigerReadMappingSimple(unsigned char ** ppPos,int nSize)219 int * Gia_AigerReadMappingSimple( unsigned char ** ppPos, int nSize )
220 {
221     int * pMapping = ABC_ALLOC( int, (size_t)nSize/4 );
222     memcpy( pMapping, *ppPos, nSize );
223     assert( nSize % 4 == 0 );
224     return pMapping;
225 }
Gia_AigerWriteMappingSimple(Gia_Man_t * p)226 Vec_Str_t * Gia_AigerWriteMappingSimple( Gia_Man_t * p )
227 {
228     unsigned char * pBuffer = ABC_ALLOC( unsigned char, 4*Vec_IntSize(p->vMapping) );
229     memcpy( pBuffer, Vec_IntArray(p->vMapping), (size_t)4*Vec_IntSize(p->vMapping) );
230     assert( Vec_IntSize(p->vMapping) >= Gia_ManObjNum(p) );
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 ***********************************************************************/
Gia_AigerReadMappingDoc(unsigned char ** ppPos,int nObjs)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 }
Gia_AigerWriteMappingDoc(Gia_Man_t * p)267 Vec_Str_t * Gia_AigerWriteMappingDoc( Gia_Man_t * p )
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 ***********************************************************************/
Gia_AigerReadPacking(unsigned char ** ppPos,int nSize)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 }
Gia_WritePacking(Vec_Int_t * vPacking)311 Vec_Str_t * Gia_WritePacking( Vec_Int_t * vPacking )
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 
325 ABC_NAMESPACE_IMPL_END
326 
327