abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
miniaig.h
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [miniaig.h]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Minimalistic AIG package.]
8 
9  Synopsis [External declarations.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - September 29, 2012.]
16 
17  Revision [$Id: miniaig.h,v 1.00 2012/09/29 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef MINI_AIG__mini_aig_h
22 #define MINI_AIG__mini_aig_h
23 
24 ////////////////////////////////////////////////////////////////////////
25 /// INCLUDES ///
26 ////////////////////////////////////////////////////////////////////////
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 
33 ////////////////////////////////////////////////////////////////////////
34 /// PARAMETERS ///
35 ////////////////////////////////////////////////////////////////////////
36 
37 #define MINI_AIG_NULL (0x7FFFFFFF)
38 #define MINI_AIG_START_SIZE (0x000000FF)
39 
40 ////////////////////////////////////////////////////////////////////////
41 /// BASIC TYPES ///
42 ////////////////////////////////////////////////////////////////////////
43 
44 typedef struct Mini_Aig_t_ Mini_Aig_t;
45 struct Mini_Aig_t_
46 {
47  int nCap;
48  int nSize;
49  int nRegs;
50  int * pArray;
51 };
52 
53 ////////////////////////////////////////////////////////////////////////
54 /// MACRO DEFINITIONS ///
55 ////////////////////////////////////////////////////////////////////////
56 
57 // memory management
58 #define MINI_AIG_ALLOC(type, num) ((type *) malloc(sizeof(type) * (num)))
59 #define MINI_AIG_CALLOC(type, num) ((type *) calloc((num), sizeof(type)))
60 #define MINI_AIG_FALLOC(type, num) ((type *) memset(malloc(sizeof(type) * (num)), 0xff, sizeof(type) * (num)))
61 #define MINI_AIG_FREE(obj) ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
62 #define MINI_AIG_REALLOC(type, obj, num) \
63  ((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
64  ((type *) malloc(sizeof(type) * (num))))
65 
66 // internal procedures
67 static void Mini_AigGrow( Mini_Aig_t * p, int nCapMin )
68 {
69  if ( p->nCap >= nCapMin )
70  return;
71  p->pArray = MINI_AIG_REALLOC( int, p->pArray, nCapMin );
72  assert( p->pArray );
73  p->nCap = nCapMin;
74 }
75 static void Mini_AigPush( Mini_Aig_t * p, int Lit0, int Lit1 )
76 {
77  if ( p->nSize + 2 > p->nCap )
78  {
79  if ( p->nCap < MINI_AIG_START_SIZE )
81  else
82  Mini_AigGrow( p, 2 * p->nCap );
83  }
84  p->pArray[p->nSize++] = Lit0;
85  p->pArray[p->nSize++] = Lit1;
86 }
87 
88 // accessing fanins
89 static int Mini_AigNodeFanin0( Mini_Aig_t * p, int Id )
90 {
91  assert( Id >= 0 && 2*Id < p->nSize );
92  return p->pArray[2*Id];
93 }
94 static int Mini_AigNodeFanin1( Mini_Aig_t * p, int Id )
95 {
96  assert( Id >= 0 && 2*Id < p->nSize );
97  return p->pArray[2*Id+1];
98 }
99 
100 // working with variables and literals
101 static int Mini_AigVar2Lit( int Var, int fCompl ) { return Var + Var + fCompl; }
102 static int Mini_AigLit2Var( int Lit ) { return Lit >> 1; }
103 static int Mini_AigLitIsCompl( int Lit ) { return Lit & 1; }
104 static int Mini_AigLitNot( int Lit ) { return Lit ^ 1; }
105 static int Mini_AigLitNotCond( int Lit, int c ) { return Lit ^ (int)(c > 0); }
106 static int Mini_AigLitRegular( int Lit ) { return Lit & ~01; }
107 
108 static int Mini_AigLitConst0() { return 0; }
109 static int Mini_AigLitConst1() { return 1; }
110 static int Mini_AigLitIsConst0( int Lit ) { return Lit == 0; }
111 static int Mini_AigLitIsConst1( int Lit ) { return Lit == 1; }
112 static int Mini_AigLitIsConst( int Lit ) { return Lit == 0 || Lit == 1; }
113 
114 static int Mini_AigNodeNum( Mini_Aig_t * p ) { return p->nSize/2; }
115 static int Mini_AigNodeIsConst( Mini_Aig_t * p, int Id ) { assert( Id >= 0 ); return Id == 0; }
116 static int Mini_AigNodeIsPi( Mini_Aig_t * p, int Id ) { assert( Id >= 0 ); return Id > 0 && Mini_AigNodeFanin0( p, Id ) == MINI_AIG_NULL; }
117 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; }
118 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; }
119 
120 // working with sequential AIGs
121 static int Mini_AigRegNum( Mini_Aig_t * p ) { return p->nRegs; }
122 static void Mini_AigSetRegNum( Mini_Aig_t * p, int n ) { p->nRegs = n; }
123 
124 // iterators through objects
125 #define Mini_AigForEachPi( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( !Mini_AigNodeIsPi(p, i) ) {} else
126 #define Mini_AigForEachPo( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( !Mini_AigNodeIsPo(p, i) ) {} else
127 #define Mini_AigForEachAnd( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( !Mini_AigNodeIsAnd(p, i) ) {} else
128 
129 
130 // constructor/destructor
132 {
133  Mini_Aig_t * p;
134  p = MINI_AIG_CALLOC( Mini_Aig_t, 1 );
136  p->pArray = MINI_AIG_ALLOC( int, p->nCap );
138  return p;
139 }
140 static void Mini_AigStop( Mini_Aig_t * p )
141 {
142  MINI_AIG_FREE( p->pArray );
143  MINI_AIG_FREE( p );
144 }
146 {
147  int i, nPis, nPos, nNodes;
148  nPis = 0;
149  Mini_AigForEachPi( p, i )
150  nPis++;
151  nPos = 0;
152  Mini_AigForEachPo( p, i )
153  nPos++;
154  nNodes = 0;
155  Mini_AigForEachAnd( p, i )
156  nNodes++;
157  printf( "PI = %d. PO = %d. Node = %d.\n", nPis, nPos, nNodes );
158 }
159 
160 // serialization
161 static void Mini_AigDump( Mini_Aig_t * p, char * pFileName )
162 {
163  FILE * pFile;
164  int RetValue;
165  pFile = fopen( pFileName, "wb" );
166  if ( pFile == NULL )
167  {
168  printf( "Cannot open file for writing \"%s\".\n", pFileName );
169  return;
170  }
171  RetValue = fwrite( &p->nSize, sizeof(int), 1, pFile );
172  RetValue = fwrite( &p->nRegs, sizeof(int), 1, pFile );
173  RetValue = fwrite( p->pArray, sizeof(int), p->nSize, pFile );
174  fclose( pFile );
175 }
176 static Mini_Aig_t * Mini_AigLoad( char * pFileName )
177 {
178  Mini_Aig_t * p;
179  FILE * pFile;
180  int RetValue, nSize;
181  pFile = fopen( pFileName, "rb" );
182  if ( pFile == NULL )
183  {
184  printf( "Cannot open file for reading \"%s\".\n", pFileName );
185  return NULL;
186  }
187  RetValue = fread( &nSize, sizeof(int), 1, pFile );
188  p = MINI_AIG_CALLOC( Mini_Aig_t, 1 );
189  p->nSize = p->nCap = nSize;
190  p->pArray = MINI_AIG_ALLOC( int, p->nCap );
191  RetValue = fread( &p->nRegs, sizeof(int), 1, pFile );
192  RetValue = fread( p->pArray, sizeof(int), p->nSize, pFile );
193  fclose( pFile );
194  return p;
195 }
196 
197 
198 // creating nodes
199 // (constant node is created when AIG manager is created)
201 {
202  int Lit = p->nSize;
204  return Lit;
205 }
206 static int Mini_AigCreatePo( Mini_Aig_t * p, int Lit0 )
207 {
208  int Lit = p->nSize;
209  assert( Lit0 >= 0 && Lit0 < Lit );
210  Mini_AigPush( p, Lit0, MINI_AIG_NULL );
211  return Lit;
212 }
213 
214 // boolean operations
215 static int Mini_AigAnd( Mini_Aig_t * p, int Lit0, int Lit1 )
216 {
217  int Lit = p->nSize;
218  assert( Lit0 >= 0 && Lit0 < Lit );
219  assert( Lit1 >= 0 && Lit1 < Lit );
220  Mini_AigPush( p, Lit0, Lit1 );
221  return Lit;
222 }
223 static int Mini_AigOr( Mini_Aig_t * p, int Lit0, int Lit1 )
224 {
225  return Mini_AigLitNot( Mini_AigAnd( p, Mini_AigLitNot(Lit0), Mini_AigLitNot(Lit1) ) );
226 }
227 static int Mini_AigMux( Mini_Aig_t * p, int LitC, int Lit1, int Lit0 )
228 {
229  int Res0 = Mini_AigAnd( p, LitC, Lit1 );
230  int Res1 = Mini_AigAnd( p, Mini_AigLitNot(LitC), Lit0 );
231  return Mini_AigOr( p, Res0, Res1 );
232 }
233 static int Mini_AigXor( Mini_Aig_t * p, int Lit0, int Lit1 )
234 {
235  return Mini_AigMux( p, Lit0, Mini_AigLitNot(Lit1), Lit1 );
236 }
237 
238 // procedure to check the topological order during AIG construction
239 static int Mini_AigCheck( Mini_Aig_t * p )
240 {
241  int status = 1;
242  int i, iFaninLit0, iFaninLit1;
243  Mini_AigForEachAnd( p, i )
244  {
245  // get the fanin literals of this AND node
246  iFaninLit0 = Mini_AigNodeFanin0( p, i );
247  iFaninLit1 = Mini_AigNodeFanin1( p, i );
248  // compare the fanin literals with the literal of the current node (2 * i)
249  if ( iFaninLit0 >= 2 * i )
250  printf( "Fanin0 of AND node %d is not in a topological order.\n", i ), status = 0;
251  if ( iFaninLit1 >= 2 * i )
252  printf( "Fanin0 of AND node %d is not in a topological order.\n", i ), status = 0;
253  }
254  Mini_AigForEachPo( p, i )
255  {
256  // get the fanin literal of this PO node
257  iFaninLit0 = Mini_AigNodeFanin0( p, i );
258  // compare the fanin literal with the literal of the current node (2 * i)
259  if ( iFaninLit0 >= 2 * i )
260  printf( "Fanin0 of PO node %d is not in a topological order.\n", i ), status = 0;
261  }
262  return status;
263 }
264 
265 
266 
267 ////////////////////////////////////////////////////////////////////////
268 /// FUNCTION DECLARATIONS ///
269 ////////////////////////////////////////////////////////////////////////
270 
271 #endif
272 
273 ////////////////////////////////////////////////////////////////////////
274 /// END OF FILE ///
275 ////////////////////////////////////////////////////////////////////////
276 
static int Mini_AigLitIsConst(int Lit)
Definition: miniaig.h:112
static void Mini_AigSetRegNum(Mini_Aig_t *p, int n)
Definition: miniaig.h:122
static int Mini_AigLitConst1()
Definition: miniaig.h:109
#define MINI_AIG_REALLOC(type, obj, num)
Definition: miniaig.h:62
static int Mini_AigLitIsConst0(int Lit)
Definition: miniaig.h:110
static int Mini_AigVar2Lit(int Var, int fCompl)
Definition: miniaig.h:101
static int Mini_AigNodeIsConst(Mini_Aig_t *p, int Id)
Definition: miniaig.h:115
static Mini_Aig_t * Mini_AigStart()
Definition: miniaig.h:131
static int Mini_AigNodeIsPi(Mini_Aig_t *p, int Id)
Definition: miniaig.h:116
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static void Mini_AigGrow(Mini_Aig_t *p, int nCapMin)
Definition: miniaig.h:67
static int Mini_AigLitConst0()
Definition: miniaig.h:108
static int Mini_AigCreatePo(Mini_Aig_t *p, int Lit0)
Definition: miniaig.h:206
int nRegs
Definition: miniaig.h:49
static int Mini_AigCreatePi(Mini_Aig_t *p)
Definition: miniaig.h:200
#define MINI_AIG_ALLOC(type, num)
MACRO DEFINITIONS ///.
Definition: miniaig.h:58
int nSize
Definition: miniaig.h:48
static int Mini_AigRegNum(Mini_Aig_t *p)
Definition: miniaig.h:121
int * pArray
Definition: miniaig.h:50
static int Mini_AigNodeIsPo(Mini_Aig_t *p, int Id)
Definition: miniaig.h:117
#define Mini_AigForEachPo(p, i)
Definition: miniaig.h:126
static int Mini_AigNodeFanin0(Mini_Aig_t *p, int Id)
Definition: miniaig.h:89
static int Mini_AigNodeFanin1(Mini_Aig_t *p, int Id)
Definition: miniaig.h:94
static Mini_Aig_t * Mini_AigLoad(char *pFileName)
Definition: miniaig.h:176
#define Mini_AigForEachPi(p, i)
Definition: miniaig.h:125
static void Mini_AigStop(Mini_Aig_t *p)
Definition: miniaig.h:140
#define Mini_AigForEachAnd(p, i)
Definition: miniaig.h:127
static int Mini_AigAnd(Mini_Aig_t *p, int Lit0, int Lit1)
Definition: miniaig.h:215
static int Mini_AigLitIsCompl(int Lit)
Definition: miniaig.h:103
static int Mini_AigNodeIsAnd(Mini_Aig_t *p, int Id)
Definition: miniaig.h:118
#define MINI_AIG_CALLOC(type, num)
Definition: miniaig.h:59
static void Mini_AigPrintStats(Mini_Aig_t *p)
Definition: miniaig.h:145
static int Mini_AigLitNot(int Lit)
Definition: miniaig.h:104
#define MINI_AIG_FREE(obj)
Definition: miniaig.h:61
int nCap
Definition: miniaig.h:47
static int Mini_AigLitIsConst1(int Lit)
Definition: miniaig.h:111
static int Mini_AigNodeNum(Mini_Aig_t *p)
Definition: miniaig.h:114
int Var
Definition: SolverTypes.h:42
static int Mini_AigLit2Var(int Lit)
Definition: miniaig.h:102
#define assert(ex)
Definition: util_old.h:213
static void Mini_AigDump(Mini_Aig_t *p, char *pFileName)
Definition: miniaig.h:161
static int Mini_AigOr(Mini_Aig_t *p, int Lit0, int Lit1)
Definition: miniaig.h:223
static int Mini_AigCheck(Mini_Aig_t *p)
Definition: miniaig.h:239
static int Mini_AigMux(Mini_Aig_t *p, int LitC, int Lit1, int Lit0)
Definition: miniaig.h:227
static int Mini_AigLitRegular(int Lit)
Definition: miniaig.h:106
static int Mini_AigXor(Mini_Aig_t *p, int Lit0, int Lit1)
Definition: miniaig.h:233
static void Mini_AigPush(Mini_Aig_t *p, int Lit0, int Lit1)
Definition: miniaig.h:75
#define MINI_AIG_NULL
INCLUDES ///.
Definition: miniaig.h:37
static int Mini_AigLitNotCond(int Lit, int c)
Definition: miniaig.h:105
#define MINI_AIG_START_SIZE
Definition: miniaig.h:38