abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abcRpo.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [abcRpo.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Rpo package.]
8 
9  Synopsis [Procedures for executing RPO.]
10 
11  Author [Mayler G. A. Martins / Vinicius Callegaro]
12 
13  Affiliation [UFRGS]
14 
15  Date [Ver. 1.0. Started - May 08, 2013.]
16 
17  Revision [$Id: abcRpo.c,v 1.00 2013/05/08 00:00:00 mgamartins Exp $]
18 
19  ***********************************************************************/
20 
21 #include "misc/extra/extra.h"
22 
23 #include "bool/rpo/rpo.h"
24 #include "bool/rpo/literal.h"
25 
27 
28 
29 // data-structure to store a bunch of truth tables
31 
33  int nVars;
34  int nWords;
35  int nFuncs;
37 };
38 
39 
40 // read/write/flip i-th bit of a bit string table:
41 
42 static inline int Abc_TruthGetBit(word * p, int i) {
43  return (int) (p[i >> 6] >> (i & 63)) & 1;
44 }
45 
46 static inline void Abc_TruthSetBit(word * p, int i) {
47  p[i >> 6] |= (((word) 1) << (i & 63));
48 }
49 
50 static inline void Abc_TruthXorBit(word * p, int i) {
51  p[i >> 6] ^= (((word) 1) << (i & 63));
52 }
53 
54 // read/write k-th digit d of a hexadecimal number:
55 
56 static inline int Abc_TruthGetHex(word * p, int k) {
57  return (int) (p[k >> 4] >> ((k << 2) & 63)) & 15;
58 }
59 
60 static inline void Abc_TruthSetHex(word * p, int k, int d) {
61  p[k >> 4] |= (((word) d) << ((k << 2) & 63));
62 }
63 
64 static inline void Abc_TruthXorHex(word * p, int k, int d) {
65  p[k >> 4] ^= (((word) d) << ((k << 2) & 63));
66 }
67 
68 ////////////////////////////////////////////////////////////////////////
69 /// FUNCTION DEFINITIONS ///
70 ////////////////////////////////////////////////////////////////////////
71 
72 // read one hex character
73 
74 static inline int Abc_TruthReadHexDigit(char HexChar) {
75  if (HexChar >= '0' && HexChar <= '9')
76  return HexChar - '0';
77  if (HexChar >= 'A' && HexChar <= 'F')
78  return HexChar - 'A' + 10;
79  if (HexChar >= 'a' && HexChar <= 'f')
80  return HexChar - 'a' + 10;
81  assert(0); // not a hexadecimal symbol
82  return -1; // return value which makes no sense
83 }
84 
85 // write one hex character
86 
87 static inline void Abc_TruthWriteHexDigit(FILE * pFile, int HexDigit) {
88  assert(HexDigit >= 0 && HexDigit < 16);
89  if (HexDigit < 10)
90  fprintf(pFile, "%d", HexDigit);
91  else
92  fprintf(pFile, "%c", 'A' + HexDigit - 10);
93 }
94 
95 // read one truth table in hexadecimal
96 
97 static void Abc_TruthReadHex(word * pTruth, char * pString, int nVars) {
98  int nWords = (nVars < 7) ? 1 : (1 << (nVars - 6));
99  int k, Digit, nDigits = (nWords << 4);
100  char EndSymbol;
101  // skip the first 2 symbols if they are "0x"
102  if (pString[0] == '0' && pString[1] == 'x')
103  pString += 2;
104  // get the last symbol
105  EndSymbol = pString[nDigits];
106  // the end symbol of the TT (the one immediately following hex digits)
107  // should be one of the following: space, a new-line, or a zero-terminator
108  // (note that on Windows symbols '\r' can be inserted before each '\n')
109  assert(EndSymbol == ' ' || EndSymbol == '\n' || EndSymbol == '\r' || EndSymbol == '\0');
110  // read hexadecimal digits in the reverse order
111  // (the last symbol in the string is the least significant digit)
112  for (k = 0; k < nDigits; k++) {
113  Digit = Abc_TruthReadHexDigit(pString[nDigits - 1 - k]);
114  assert(Digit >= 0 && Digit < 16);
115  Abc_TruthSetHex(pTruth, k, Digit);
116  }
117 }
118 
119 // write one truth table in hexadecimal (do not add end-of-line!)
120 
121 static void Abc_TruthWriteHex(FILE * pFile, word * pTruth, int nVars) {
122  int nDigits, Digit, k;
123  nDigits = (1 << (nVars - 2));
124  for (k = 0; k < nDigits; k++) {
125  Digit = Abc_TruthGetHex(pTruth, k);
126  assert(Digit >= 0 && Digit < 16);
127  Abc_TruthWriteHexDigit(pFile, Digit);
128  }
129 }
130 
131 
132 
133 /**Function*************************************************************
134 
135  Synopsis [Allocate/Deallocate storage for truth tables..]
136 
137  Description []
138 
139  SideEffects []
140 
141  SeeAlso []
142 
143  ***********************************************************************/
144 static Rpo_TtStore_t * Abc_TruthStoreAlloc(int nVars, int nFuncs) {
145  Rpo_TtStore_t * p;
146  int i;
147  p = (Rpo_TtStore_t *) malloc(sizeof (Rpo_TtStore_t));
148  p->nVars = nVars;
149  p->nWords = (nVars < 7) ? 1 : (1 << (nVars - 6));
150  p->nFuncs = nFuncs;
151  // alloc storage for 'nFuncs' truth tables as one chunk of memory
152  p->pFuncs = (word **) malloc((sizeof (word *) + sizeof (word) * p->nWords) * p->nFuncs);
153  // assign and clean the truth table storage
154  p->pFuncs[0] = (word *) (p->pFuncs + p->nFuncs);
155  memset(p->pFuncs[0], 0, sizeof (word) * p->nWords * p->nFuncs);
156  // split it up into individual truth tables
157  for (i = 1; i < p->nFuncs; i++)
158  p->pFuncs[i] = p->pFuncs[i - 1] + p->nWords;
159  return p;
160 }
161 
162 static Rpo_TtStore_t * Abc_TruthStoreAlloc2(int nVars, int nFuncs, word * pBuffer) {
163  Rpo_TtStore_t * p;
164  int i;
165  p = (Rpo_TtStore_t *) malloc(sizeof (Rpo_TtStore_t));
166  p->nVars = nVars;
167  p->nWords = (nVars < 7) ? 1 : (1 << (nVars - 6));
168  p->nFuncs = nFuncs;
169  // alloc storage for 'nFuncs' truth tables as one chunk of memory
170  p->pFuncs = (word **) malloc(sizeof (word *) * p->nFuncs);
171  // assign and clean the truth table storage
172  p->pFuncs[0] = pBuffer;
173  // split it up into individual truth tables
174  for (i = 1; i < p->nFuncs; i++)
175  p->pFuncs[i] = p->pFuncs[i - 1] + p->nWords;
176  return p;
177 }
178 
179 static void Abc_TtStoreFree(Rpo_TtStore_t * p, int nVarNum) {
180  if (nVarNum >= 0)
181  ABC_FREE(p->pFuncs[0]);
182  ABC_FREE(p->pFuncs);
183  ABC_FREE(p);
184 }
185 
186 /**Function*************************************************************
187 
188  Synopsis [Read file contents.]
189 
190  Description []
191 
192  SideEffects []
193 
194  SeeAlso []
195 
196  ***********************************************************************/
197 extern int Abc_FileSize(char * pFileName);
198 
199 /**Function*************************************************************
200 
201  Synopsis [Read file contents.]
202 
203  Description []
204 
205  SideEffects []
206 
207  SeeAlso []
208 
209  ***********************************************************************/
210 extern char * Abc_FileRead(char * pFileName);
211 
212 /**Function*************************************************************
213 
214  Synopsis [Determine the number of variables by reading the first line.]
215 
216  Description [Determine the number of functions by counting the lines.]
217 
218  SideEffects []
219 
220  SeeAlso []
221 
222  ***********************************************************************/
223 extern void Abc_TruthGetParams(char * pFileName, int * pnVars, int * pnTruths);
224 
225 
226 /**Function*************************************************************
227 
228  Synopsis [Read truth tables from file.]
229 
230  Description []
231 
232  SideEffects []
233 
234  SeeAlso []
235 
236  ***********************************************************************/
237 static void Abc_TruthStoreRead(char * pFileName, Rpo_TtStore_t * p) {
238  char * pContents;
239  int i, nLines;
240  pContents = Abc_FileRead(pFileName);
241  if (pContents == NULL)
242  return;
243  // here it is assumed (without checking!) that each line of the file
244  // begins with a string of hexadecimal chars followed by space
245 
246  // the file will be read till the first empty line (pContents[i] == '\n')
247  // (note that Abc_FileRead() added several empty lines at the end of the file contents)
248  for (nLines = i = 0; pContents[i] != '\n';) {
249  // read one line
250  Abc_TruthReadHex(p->pFuncs[nLines++], &pContents[i], p->nVars);
251  // skip till after the end-of-line symbol
252  // (note that end-of-line symbol is also skipped)
253  while (pContents[i++] != '\n');
254  }
255  // adjust the number of functions read
256  // (we may have allocated more storage because some lines in the file were empty)
257  assert(p->nFuncs >= nLines);
258  p->nFuncs = nLines;
259  ABC_FREE(pContents);
260 }
261 
262 /**Function*************************************************************
263 
264  Synopsis [Write truth tables into file.]
265 
266  Description []
267 
268  SideEffects []
269 
270  SeeAlso []
271 
272  ***********************************************************************/
273 static void Abc_TtStoreWrite(char * pFileName, Rpo_TtStore_t * p, int fBinary) {
274  FILE * pFile;
275  int i, nBytes = 8 * Abc_Truth6WordNum(p->nVars);
276  pFile = fopen(pFileName, "wb");
277  if (pFile == NULL) {
278  printf("Cannot open file \"%s\" for writing.\n", pFileName);
279  return;
280  }
281  for (i = 0; i < p->nFuncs; i++) {
282  if (fBinary)
283  fwrite(p->pFuncs[i], nBytes, 1, pFile);
284  else
285  Abc_TruthWriteHex(pFile, p->pFuncs[i], p->nVars), fprintf(pFile, "\n");
286  }
287  fclose(pFile);
288 }
289 
290 /**Function*************************************************************
291 
292  Synopsis [Read truth tables from input file and write them into output file.]
293 
294  Description []
295 
296  SideEffects []
297 
298  SeeAlso []
299 
300  ***********************************************************************/
301 static Rpo_TtStore_t * Abc_TtStoreLoad(char * pFileName, int nVarNum) {
302  Rpo_TtStore_t * p;
303  if (nVarNum < 0) {
304  int nVars, nTruths;
305  // figure out how many truth table and how many variables
306  Abc_TruthGetParams(pFileName, &nVars, &nTruths);
307  if (nVars < 2 || nVars > 16 || nTruths == 0)
308  return NULL;
309  // allocate data-structure
310  p = Abc_TruthStoreAlloc(nVars, nTruths);
311  // read info from file
312  Abc_TruthStoreRead(pFileName, p);
313  } else {
314  char * pBuffer;
315  int nFileSize = Abc_FileSize(pFileName);
316  int nBytes = (1 << (nVarNum - 3)); // why mishchencko put -3? ###
317  int nTruths = nFileSize / nBytes;
318  //Abc_Print(-2,"nFileSize=%d,nTruths=%d\n",nFileSize, nTruths);
319  if (nFileSize == -1)
320  return NULL;
321  assert(nVarNum >= 6);
322  if (nFileSize % nBytes != 0)
323  Abc_Print(0, "The file size (%d) is divided by the truth table size (%d) with remainder (%d).\n",
324  nFileSize, nBytes, nFileSize % nBytes);
325  // read file contents
326  pBuffer = Abc_FileRead(pFileName);
327  // allocate data-structure
328  p = Abc_TruthStoreAlloc2(nVarNum, nTruths, (word *) pBuffer);
329  }
330  return p;
331 }
332 
333 
334 /**Function*************************************************************
335 
336  Synopsis [Apply decomposition to the truth table.]
337 
338  Description [Returns the number of AIG nodes.]
339 
340  SideEffects []
341 
342  SeeAlso []
343 
344  ***********************************************************************/
345 void Abc_TruthRpoPerform(Rpo_TtStore_t * p, int nThreshold, int fVerbose) {
346  clock_t clk = clock();
347  int i;
348  int rpoCount = 0;
349  Literal_t* lit;
350  float percent;
351  for (i = 0; i < p->nFuncs; i++) {
352 // if(i>1000) {
353 // continue;
354 // }
355 ////
356 // if(i!= 2196 ) { //5886
357 // continue;
358 // }
359  if(fVerbose) {
360  Abc_Print(-2,"%d: ", i+1);
361  }
362 
363  lit = Rpo_Factorize((unsigned *) p->pFuncs[i], p->nVars, nThreshold, fVerbose);
364  if (lit != NULL) {
365  if(fVerbose) {
366  Abc_Print(-2, "Solution : %s\n", lit->expression->pArray);
367  Abc_Print(-2, "\n\n");
368  }
369  Lit_Free(lit);
370  rpoCount++;
371  } else {
372  if(fVerbose) {
373  Abc_Print(-2, "null\n");
374  Abc_Print(-2, "\n\n");
375  }
376  }
377  }
378  percent = (rpoCount * 100.0) / p->nFuncs;
379  Abc_Print(-2,"%d of %d (%.2f %%) functions are RPO.\n", rpoCount,p->nFuncs,percent);
380  Abc_PrintTime(1, "Time", clock() - clk);
381 }
382 
383 /**Function*************************************************************
384 
385  Synopsis [Apply decomposition to truth tables.]
386 
387  Description []
388 
389  SideEffects []
390 
391  SeeAlso []
392 
393  ***********************************************************************/
394 void Abc_TruthRpoTest(char * pFileName, int nVarNum, int nThreshold, int fVerbose) {
395  Rpo_TtStore_t * p;
396 
397  // allocate data-structure
398 // if (fVerbose) {
399 // Abc_Print(-2, "Number of variables = %d\n", nVarNum);
400 // }
401  p = Abc_TtStoreLoad(pFileName, nVarNum);
402 
403  if (fVerbose) {
404  Abc_Print(-2, "Number of variables = %d\n", p->nVars);
405  }
406  // consider functions from the file
407  Abc_TruthRpoPerform(p, nThreshold, fVerbose);
408 
409  // delete data-structure
410  Abc_TtStoreFree(p, nVarNum);
411  // printf( "Finished decomposing truth tables from file \"%s\".\n", pFileName );
412 }
413 
414 /**Function*************************************************************
415 
416  Synopsis [Testbench for decomposition algorithms.]
417 
418  Description []
419 
420  SideEffects []
421 
422  SeeAlso []
423 
424  ***********************************************************************/
425 int Abc_RpoTest(char * pFileName, int nVarNum,int nThreshold, int fVerbose) {
426  if (fVerbose) {
427  printf("Using truth tables from file \"%s\"...\n", pFileName);
428  }
429  Abc_TruthRpoTest(pFileName, nVarNum, nThreshold, fVerbose);
430  fflush(stdout);
431  return 0;
432 }
433 
434 
435 /////////////////////ert truth table to ///////////////////////////////////////////////////
436 /// END OF FILE ///
437 ////////////////////////////////////////////////////////////////////////
438 
439 
441 
442 
char * memset()
static int Abc_TruthGetHex(word *p, int k)
Definition: abcRpo.c:56
int Abc_RpoTest(char *pFileName, int nVarNum, int nThreshold, int fVerbose)
Definition: abcRpo.c:425
char * malloc()
char * Abc_FileRead(char *pFileName)
Definition: abcDec.c:222
static void Abc_TtStoreFree(Rpo_TtStore_t *p, int nVarNum)
Definition: abcRpo.c:179
void Abc_TruthRpoPerform(Rpo_TtStore_t *p, int nThreshold, int fVerbose)
Definition: abcRpo.c:345
static void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: abcRpo.c:121
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static int Abc_Truth6WordNum(int nVars)
Definition: abc_global.h:257
static int Abc_TruthReadHexDigit(char HexChar)
FUNCTION DEFINITIONS ///.
Definition: abcRpo.c:74
static void Abc_TruthStoreRead(char *pFileName, Rpo_TtStore_t *p)
Definition: abcRpo.c:237
int Abc_FileSize(char *pFileName)
Definition: abcDec.c:194
Vec_Str_t * expression
Definition: literal.h:55
int nWords
Definition: abcNpn.c:127
static void Lit_Free(Literal_t *lit)
Definition: literal.h:283
int lit
Definition: satVec.h:130
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
void Abc_TruthGetParams(char *pFileName, int *pnVars, int *pnTruths)
Definition: abcDec.c:262
typedefABC_NAMESPACE_IMPL_START struct Rpo_TtStore_t_ Rpo_TtStore_t
Definition: abcRpo.c:30
Literal_t * Rpo_Factorize(unsigned *target, int nVars, int nThreshold, int verbose)
Definition: rpo.c:174
char * pArray
Definition: bblif.c:51
static Rpo_TtStore_t * Abc_TruthStoreAlloc(int nVars, int nFuncs)
Definition: abcRpo.c:144
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static void Abc_TruthXorBit(word *p, int i)
Definition: abcRpo.c:50
static int Abc_TruthGetBit(word *p, int i)
Definition: abcRpo.c:42
word ** pFuncs
Definition: abcRpo.c:36
static void Abc_TruthWriteHexDigit(FILE *pFile, int HexDigit)
Definition: abcRpo.c:87
static void Abc_TtStoreWrite(char *pFileName, Rpo_TtStore_t *p, int fBinary)
Definition: abcRpo.c:273
static void Abc_Print(int level, const char *format,...)
Definition: abc_global.h:313
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
int nFuncs
Definition: abcRpo.c:35
static void Abc_TruthReadHex(word *pTruth, char *pString, int nVars)
Definition: abcRpo.c:97
#define ABC_FREE(obj)
Definition: abc_global.h:232
static void Abc_TruthSetBit(word *p, int i)
Definition: abcRpo.c:46
#define assert(ex)
Definition: util_old.h:213
void Abc_TruthRpoTest(char *pFileName, int nVarNum, int nThreshold, int fVerbose)
Definition: abcRpo.c:394
static void Abc_TruthXorHex(word *p, int k, int d)
Definition: abcRpo.c:64
static Rpo_TtStore_t * Abc_TruthStoreAlloc2(int nVars, int nFuncs, word *pBuffer)
Definition: abcRpo.c:162
static Rpo_TtStore_t * Abc_TtStoreLoad(char *pFileName, int nVarNum)
Definition: abcRpo.c:301
int nWords
Definition: abcRpo.c:34
static void Abc_TruthSetHex(word *p, int k, int d)
Definition: abcRpo.c:60