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
|
/**CFile****************************************************************
FileName [miniaig.h]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Minimalistic AIG package.]
Synopsis [External declarations.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - September 29, 2012.]
Revision [$Id: miniaig.h,v 1.00 2012/09/29 00:00:00 alanmi Exp $]
***********************************************************************/
#ifndef MINI_AIG__mini_aig_h
#define MINI_AIG__mini_aig_h
////////////////////////////////////////////////////////////////////////
/// INCLUDES ///
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
ABC_NAMESPACE_HEADER_START
////////////////////////////////////////////////////////////////////////
/// PARAMETERS ///
////////////////////////////////////////////////////////////////////////
#define MINI_AIG_NULL (0x7FFFFFFF)
#define MINI_AIG_START_SIZE (0x000000FF)
////////////////////////////////////////////////////////////////////////
/// BASIC TYPES ///
////////////////////////////////////////////////////////////////////////
typedef struct Mini_Aig_t_ Mini_Aig_t;
struct Mini_Aig_t_
{
int nCap;
int nSize;
int nRegs;
int * pArray;
};
////////////////////////////////////////////////////////////////////////
/// MACRO DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
// memory management
#define MINI_AIG_ALLOC(type, num) ((type *) malloc(sizeof(type) * (num)))
#define MINI_AIG_CALLOC(type, num) ((type *) calloc((num), sizeof(type)))
#define MINI_AIG_FALLOC(type, num) ((type *) memset(malloc(sizeof(type) * (num)), 0xff, sizeof(type) * (num)))
#define MINI_AIG_FREE(obj) ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
#define MINI_AIG_REALLOC(type, obj, num) \
((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
((type *) malloc(sizeof(type) * (num))))
// internal procedures
static void Mini_AigGrow( Mini_Aig_t * p, int nCapMin )
{
if ( p->nCap >= nCapMin )
return;
p->pArray = MINI_AIG_REALLOC( int, p->pArray, nCapMin );
assert( p->pArray );
p->nCap = nCapMin;
}
static void Mini_AigPush( Mini_Aig_t * p, int Lit0, int Lit1 )
{
if ( p->nSize + 2 > p->nCap )
{
assert( p->nSize < MINI_AIG_NULL/4 );
if ( p->nCap < MINI_AIG_START_SIZE )
Mini_AigGrow( p, MINI_AIG_START_SIZE );
else
Mini_AigGrow( p, 2 * p->nCap );
}
p->pArray[p->nSize++] = Lit0;
p->pArray[p->nSize++] = Lit1;
}
// accessing fanins
static int Mini_AigNodeFanin0( Mini_Aig_t * p, int Id )
{
assert( Id >= 0 && 2*Id < p->nSize );
return p->pArray[2*Id];
}
static int Mini_AigNodeFanin1( Mini_Aig_t * p, int Id )
{
assert( Id >= 0 && 2*Id < p->nSize );
return p->pArray[2*Id+1];
}
// working with variables and literals
static int Mini_AigVar2Lit( int Var, int fCompl ) { return Var + Var + fCompl; }
static int Mini_AigLit2Var( int Lit ) { return Lit >> 1; }
static int Mini_AigLitIsCompl( int Lit ) { return Lit & 1; }
static int Mini_AigLitNot( int Lit ) { return Lit ^ 1; }
static int Mini_AigLitNotCond( int Lit, int c ) { return Lit ^ (int)(c > 0); }
static int Mini_AigLitRegular( int Lit ) { return Lit & ~01; }
static int Mini_AigLitConst0() { return 0; }
static int Mini_AigLitConst1() { return 1; }
static int Mini_AigLitIsConst0( int Lit ) { return Lit == 0; }
static int Mini_AigLitIsConst1( int Lit ) { return Lit == 1; }
static int Mini_AigLitIsConst( int Lit ) { return Lit == 0 || Lit == 1; }
static int Mini_AigNodeNum( Mini_Aig_t * p ) { return p->nSize/2; }
static int Mini_AigNodeIsConst( Mini_Aig_t * p, int Id ) { assert( Id >= 0 ); return Id == 0; }
static int Mini_AigNodeIsPi( Mini_Aig_t * p, int Id ) { assert( Id >= 0 ); return Id > 0 && Mini_AigNodeFanin0( p, Id ) == MINI_AIG_NULL; }
static int Mini_AigNodeIsPo( Mini_Aig_t * p, int Id ) { assert( Id >= 0 ); return Id > 0 && Mini_AigNodeFanin0( p, Id ) != MINI_AIG_NULL && Mini_AigNodeFanin1( p, Id ) == MINI_AIG_NULL; }
static int Mini_AigNodeIsAnd( Mini_Aig_t * p, int Id ) { assert( Id >= 0 ); return Id > 0 && Mini_AigNodeFanin0( p, Id ) != MINI_AIG_NULL && Mini_AigNodeFanin1( p, Id ) != MINI_AIG_NULL; }
// working with sequential AIGs
static int Mini_AigRegNum( Mini_Aig_t * p ) { return p->nRegs; }
static void Mini_AigSetRegNum( Mini_Aig_t * p, int n ) { p->nRegs = n; }
// iterators through objects
#define Mini_AigForEachPi( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( !Mini_AigNodeIsPi(p, i) ) {} else
#define Mini_AigForEachPo( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( !Mini_AigNodeIsPo(p, i) ) {} else
#define Mini_AigForEachAnd( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( !Mini_AigNodeIsAnd(p, i) ) {} else
// constructor/destructor
static Mini_Aig_t * Mini_AigStart()
{
Mini_Aig_t * p;
p = MINI_AIG_CALLOC( Mini_Aig_t, 1 );
p->nCap = MINI_AIG_START_SIZE;
p->pArray = MINI_AIG_ALLOC( int, p->nCap );
Mini_AigPush( p, MINI_AIG_NULL, MINI_AIG_NULL );
return p;
}
static void Mini_AigStop( Mini_Aig_t * p )
{
MINI_AIG_FREE( p->pArray );
MINI_AIG_FREE( p );
}
static void Mini_AigPrintStats( Mini_Aig_t * p )
{
int i, nPis, nPos, nNodes;
nPis = 0;
Mini_AigForEachPi( p, i )
nPis++;
nPos = 0;
Mini_AigForEachPo( p, i )
nPos++;
nNodes = 0;
Mini_AigForEachAnd( p, i )
nNodes++;
printf( "PI = %d. PO = %d. Node = %d.\n", nPis, nPos, nNodes );
}
// serialization
static void Mini_AigDump( Mini_Aig_t * p, char * pFileName )
{
FILE * pFile;
int RetValue;
pFile = fopen( pFileName, "wb" );
if ( pFile == NULL )
{
printf( "Cannot open file for writing \"%s\".\n", pFileName );
return;
}
RetValue = (int)fwrite( &p->nSize, sizeof(int), 1, pFile );
RetValue = (int)fwrite( &p->nRegs, sizeof(int), 1, pFile );
RetValue = (int)fwrite( p->pArray, sizeof(int), p->nSize, pFile );
fclose( pFile );
}
static Mini_Aig_t * Mini_AigLoad( char * pFileName )
{
Mini_Aig_t * p;
FILE * pFile;
int RetValue, nSize;
pFile = fopen( pFileName, "rb" );
if ( pFile == NULL )
{
printf( "Cannot open file for reading \"%s\".\n", pFileName );
return NULL;
}
RetValue = (int)fread( &nSize, sizeof(int), 1, pFile );
p = MINI_AIG_CALLOC( Mini_Aig_t, 1 );
p->nSize = p->nCap = nSize;
p->pArray = MINI_AIG_ALLOC( int, p->nCap );
RetValue = (int)fread( &p->nRegs, sizeof(int), 1, pFile );
RetValue = (int)fread( p->pArray, sizeof(int), p->nSize, pFile );
fclose( pFile );
return p;
}
// creating nodes
// (constant node is created when AIG manager is created)
static int Mini_AigCreatePi( Mini_Aig_t * p )
{
int Lit = p->nSize;
Mini_AigPush( p, MINI_AIG_NULL, MINI_AIG_NULL );
return Lit;
}
static int Mini_AigCreatePo( Mini_Aig_t * p, int Lit0 )
{
int Lit = p->nSize;
assert( Lit0 >= 0 && Lit0 < Lit );
Mini_AigPush( p, Lit0, MINI_AIG_NULL );
return Lit;
}
// boolean operations
static int Mini_AigAnd( Mini_Aig_t * p, int Lit0, int Lit1 )
{
int Lit = p->nSize;
assert( Lit0 >= 0 && Lit0 < Lit );
assert( Lit1 >= 0 && Lit1 < Lit );
Mini_AigPush( p, Lit0, Lit1 );
return Lit;
}
static int Mini_AigOr( Mini_Aig_t * p, int Lit0, int Lit1 )
{
return Mini_AigLitNot( Mini_AigAnd( p, Mini_AigLitNot(Lit0), Mini_AigLitNot(Lit1) ) );
}
static int Mini_AigMux( Mini_Aig_t * p, int LitC, int Lit1, int Lit0 )
{
int Res0 = Mini_AigAnd( p, LitC, Lit1 );
int Res1 = Mini_AigAnd( p, Mini_AigLitNot(LitC), Lit0 );
return Mini_AigOr( p, Res0, Res1 );
}
static int Mini_AigXor( Mini_Aig_t * p, int Lit0, int Lit1 )
{
return Mini_AigMux( p, Lit0, Mini_AigLitNot(Lit1), Lit1 );
}
// procedure to check the topological order during AIG construction
static int Mini_AigCheck( Mini_Aig_t * p )
{
int status = 1;
int i, iFaninLit0, iFaninLit1;
Mini_AigForEachAnd( p, i )
{
// get the fanin literals of this AND node
iFaninLit0 = Mini_AigNodeFanin0( p, i );
iFaninLit1 = Mini_AigNodeFanin1( p, i );
// compare the fanin literals with the literal of the current node (2 * i)
if ( iFaninLit0 >= 2 * i )
printf( "Fanin0 of AND node %d is not in a topological order.\n", i ), status = 0;
if ( iFaninLit1 >= 2 * i )
printf( "Fanin0 of AND node %d is not in a topological order.\n", i ), status = 0;
}
Mini_AigForEachPo( p, i )
{
// get the fanin literal of this PO node
iFaninLit0 = Mini_AigNodeFanin0( p, i );
// compare the fanin literal with the literal of the current node (2 * i)
if ( iFaninLit0 >= 2 * i )
printf( "Fanin0 of PO node %d is not in a topological order.\n", i ), status = 0;
}
return status;
}
////////////////////////////////////////////////////////////////////////
/// FUNCTION DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_HEADER_END
#endif
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
|