abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
luckyRead.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [luckyRead.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Semi-canonical form computation package.]
8 
9  Synopsis [Reading truth tables from file.]
10 
11  Author [Jake]
12 
13  Date [Started - August 2012]
14 
15 ***********************************************************************/
16 
17 #include "luckyInt.h"
18 
20 
21 
22 // read/write/flip i-th bit of a bit string table:
23 static inline int Abc_TruthGetBit( word * p, int i ) { return (int)(p[i>>6] >> (i & 63)) & 1; }
24 static inline void Abc_TruthSetBit( word * p, int i ) { p[i>>6] |= (((word)1)<<(i & 63)); }
25 static inline void Abc_TruthXorBit( word * p, int i ) { p[i>>6] ^= (((word)1)<<(i & 63)); }
26 
27 // read/write k-th digit d of a hexadecimal number:
28 static inline int Abc_TruthGetHex( word * p, int k ) { return (int)(p[k>>4] >> ((k<<2) & 63)) & 15; }
29 static inline void Abc_TruthSetHex( word * p, int k, int d ) { p[k>>4] |= (((word)d)<<((k<<2) & 63)); }
30 static inline void Abc_TruthXorHex( word * p, int k, int d ) { p[k>>4] ^= (((word)d)<<((k<<2) & 63)); }
31 
32 // read one hex character
33 static inline int Abc_TruthReadHexDigit( char HexChar )
34 {
35  if ( HexChar >= '0' && HexChar <= '9' )
36  return HexChar - '0';
37  if ( HexChar >= 'A' && HexChar <= 'F' )
38  return HexChar - 'A' + 10;
39  if ( HexChar >= 'a' && HexChar <= 'f' )
40  return HexChar - 'a' + 10;
41  assert( 0 ); // not a hexadecimal symbol
42  return -1; // return value which makes no sense
43 }
44 
45 // write one hex character
46 static inline void Abc_TruthWriteHexDigit( FILE * pFile, int HexDigit )
47 {
48  assert( HexDigit >= 0 && HexDigit < 16 );
49  if ( HexDigit < 10 )
50  fprintf( pFile, "%d", HexDigit );
51  else
52  fprintf( pFile, "%c", 'A' + HexDigit-10 );
53 }
54 
55 // read one truth table in hexadecimal
56 static inline void Abc_TruthReadHex( word * pTruth, char * pString, int nVars )
57 {
58  int nWords = (nVars < 7)? 1 : (1 << (nVars-6));
59  int k, Digit, nDigits = (nWords << 4);
60  char EndSymbol;
61  // skip the first 2 symbols if they are "0x"
62  if ( pString[0] == '0' && pString[1] == 'x' )
63  pString += 2;
64  // get the last symbol
65  EndSymbol = pString[nDigits];
66  // the end symbol of the TT (the one immediately following hex digits)
67  // should be one of the following: space, a new-line, or a zero-terminator
68  // (note that on Windows symbols '\r' can be inserted before each '\n')
69  assert( EndSymbol == ' ' || EndSymbol == '\n' || EndSymbol == '\r' || EndSymbol == '\0' );
70  // read hexadecimal digits in the reverse order
71  // (the last symbol in the string is the least significant digit)
72  for ( k = 0; k < nDigits; k++ )
73  {
74  Digit = Abc_TruthReadHexDigit( pString[nDigits - 1 - k] );
75  assert( Digit >= 0 && Digit < 16 );
76  Abc_TruthSetHex( pTruth, k, Digit );
77  }
78 }
79 
80 // write one truth table in hexadecimal (do not add end-of-line!)
81 static inline void Abc_TruthWriteHex( FILE * pFile, word * pTruth, int nVars )
82 {
83  int nDigits, Digit, k;
84  // write hexadecimal digits in the reverse order
85  // (the last symbol in the string is the least significant digit)
86  nDigits = (1 << (nVars-2));
87  for ( k = 0; k < nDigits; k++ )
88  {
89  Digit = Abc_TruthGetHex( pTruth, nDigits - 1 - k );
90  assert( Digit >= 0 && Digit < 16 );
91  Abc_TruthWriteHexDigit( pFile, Digit );
92  }
93 }
94 
95 // allocate and clear memory to store 'nTruths' truth tables of 'nVars' variables
96 static inline Abc_TtStore_t * Abc_TruthStoreAlloc( int nVars, int nFuncs )
97 {
98  Abc_TtStore_t * p;
99  int i;
100  p = (Abc_TtStore_t *)malloc( sizeof(Abc_TtStore_t) );
101  p->nVars = nVars;
102  p->nWords = (nVars < 7) ? 1 : (1 << (nVars-6));
103  p->nFuncs = nFuncs;
104  // alloc array of 'nFuncs' pointers to truth tables
105  p->pFuncs = (word **)malloc( sizeof(word *) * p->nFuncs );
106  // alloc storage for 'nFuncs' truth tables as one chunk of memory
107  p->pFuncs[0] = (word *)calloc( sizeof(word), p->nFuncs * p->nWords );
108  // split it up into individual truth tables
109  for ( i = 1; i < p->nFuncs; i++ )
110  p->pFuncs[i] = p->pFuncs[i-1] + p->nWords;
111  return p;
112 }
113 
114 // free memory previously allocated for storing truth tables
116 {
117  free( p->pFuncs[0] );
118  free( p->pFuncs );
119  free( p );
120 }
121 
122 // read file contents
123 static char * Abc_FileRead( char * pFileName )
124 {
125  FILE * pFile;
126  char * pBuffer;
127  int nFileSize;
128  int RetValue;
129  pFile = fopen( pFileName, "r" );
130  if ( pFile == NULL )
131  {
132  printf( "Cannot open file \"%s\" for reading.\n", pFileName );
133  return NULL;
134  }
135  // get the file size, in bytes
136  fseek( pFile, 0, SEEK_END );
137  nFileSize = ftell( pFile );
138  // move the file current reading position to the beginning
139  rewind( pFile );
140  // load the contents of the file into memory
141  pBuffer = (char *)malloc( nFileSize + 3 );
142  RetValue = fread( pBuffer, nFileSize, 1, pFile );
143  // add several empty lines at the end
144  // (these will be used to signal the end of parsing)
145  pBuffer[ nFileSize + 0] = '\n';
146  pBuffer[ nFileSize + 1] = '\n';
147  // terminate the string with '\0'
148  pBuffer[ nFileSize + 2] = '\0';
149  fclose( pFile );
150  return pBuffer;
151 }
152 
153 // determine the number of variables by reading the first line
154 // determine the number of functions by counting the lines
155 static void Abc_TruthGetParams( char * pFileName, int * pnVars, int * pnTruths )
156 {
157  char * pContents;
158  int i, nVars, nLines;
159  // prepare the output
160  if ( pnVars )
161  *pnVars = 0;
162  if ( pnTruths )
163  *pnTruths = 0;
164  // read data from file
165  pContents = Abc_FileRead( pFileName );
166  if ( pContents == NULL )
167  return;
168  // count the number of symbols before the first space or new-line
169  // (note that on Windows symbols '\r' can be inserted before each '\n')
170  for ( i = 0; pContents[i]; i++ )
171  if ( pContents[i] == ' ' || pContents[i] == '\n' || pContents[i] == '\r' )
172  break;
173  if ( pContents[i] == 0 )
174  printf( "Strange, the input file does not have spaces and new-lines...\n" );
175 
176  // account for the fact that truth tables may have "0x" at the beginning of each line
177  if ( pContents[0] == '0' && pContents[1] == 'x' )
178  i = i - 2;
179 
180  // determine the number of variables
181  for ( nVars = 0; nVars < 32; nVars++ )
182  if ( 4 * i == (1 << nVars) ) // the number of bits equal to the size of truth table
183  break;
184  if ( nVars < 2 || nVars > 16 )
185  {
186  printf( "Does not look like the input file contains truth tables...\n" );
187  return;
188  }
189  if ( pnVars )
190  *pnVars = nVars;
191 
192  // determine the number of functions by counting the lines
193  nLines = 0;
194  for ( i = 0; pContents[i]; i++ )
195  nLines += (pContents[i] == '\n');
196  if ( pnTruths )
197  *pnTruths = nLines;
198 }
199 
200 static Abc_TtStore_t * Abc_Create_TtSpore (char * pFileInput)
201 {
202  int nVars, nTruths;
203  Abc_TtStore_t * p;
204  Abc_TruthGetParams( pFileInput, &nVars, &nTruths );
205  p = Abc_TruthStoreAlloc( nVars, nTruths );
206  return p;
207 
208 }
209 // read truth tables from file
210 static void Abc_TruthStoreRead( char * pFileName, Abc_TtStore_t* p )
211 {
212  char * pContents;
213  int i, nLines;
214  pContents = Abc_FileRead( pFileName );
215  if ( pContents == NULL )
216  return;
217  // here it is assumed (without checking!) that each line of the file
218  // begins with a string of hexadecimal chars followed by space
219 
220  // the file will be read till the first empty line (pContents[i] == '\n')
221  // (note that Abc_FileRead() added several empty lines at the end of the file contents)
222  for ( nLines = i = 0; pContents[i] != '\n'; )
223  {
224  // read one line
225  Abc_TruthReadHex( p->pFuncs[nLines++], &pContents[i], p->nVars );
226  // skip till after the end-of-line symbol
227  // (note that end-of-line symbol is also skipped)
228  while ( pContents[i++] != '\n' );
229  }
230  // adjust the number of functions read
231  // (we may have allocated more storage because some lines in the file were empty)
232  assert( p->nFuncs >= nLines );
233  p->nFuncs = nLines;
234 }
235 
236 // write truth tables into file
237 // (here we write one symbol at a time - it can be optimized by writing
238 // each truth table into a string and then writing the string into a file)
239 static void Abc_TruthStoreWrite( char * pFileName, Abc_TtStore_t * p )
240 {
241  FILE * pFile;
242  int i;
243  pFile = fopen( pFileName, "wb" );
244  if ( pFile == NULL )
245  {
246  printf( "Cannot open file \"%s\" for writing.\n", pFileName );
247  return;
248  }
249  for ( i = 0; i < p->nFuncs; i++ )
250  {
251  Abc_TruthWriteHex( pFile, p->pFuncs[i], p->nVars );
252  fprintf( pFile, "\n" );
253  }
254  fclose( pFile );
255 }
256 
257 static void WriteToFile(char * pFileName, Abc_TtStore_t * p, word* a)
258 {
259  FILE * pFile;
260  int i;
261  pFile = fopen( pFileName, "w" );
262  if ( pFile == NULL )
263  {
264  printf( "Cannot open file \"%s\" for writing.\n", pFileName );
265  return;
266  }
267  for ( i = 0; i < p->nFuncs; i++ )
268  {
269  Abc_TruthWriteHex( pFile, &a[i], p->nVars );
270  fprintf( pFile, "\n" );
271  }
272  fclose( pFile );
273 }
274 
275 static void WriteToFile1(char * pFileName, Abc_TtStore_t * p, word** a)
276 {
277  FILE * pFile;
278  int i,j;
279  pFile = fopen( pFileName, "w" );
280  if ( pFile == NULL )
281  {
282  printf( "Cannot open file \"%s\" for writing.\n", pFileName );
283  return;
284  }
285  for ( i = 0; i < p->nFuncs; i++ )
286  {
287  fprintf( pFile, "0" );
288  fprintf( pFile, "x" );
289  for ( j=p->nWords-1; j >= 0; j-- )
290  Abc_TruthWriteHex( pFile, &a[i][j], p->nVars );
291  fprintf( pFile, "\n" );
292  }
293  fprintf( pFile, "\n" );
294  fclose( pFile );
295 }
296 static void WriteToFile2(char * pFileName, Abc_TtStore_t * p, word* a)
297 {
298  FILE * pFile;
299  int i,j;
300  pFile = fopen( pFileName, "w" );
301  if ( pFile == NULL )
302  {
303  printf( "Cannot open file \"%s\" for writing.\n", pFileName );
304  return;
305  }
306  for ( i = 0; i < p->nFuncs; i++ )
307  {
308  fprintf( pFile, "0" );
309  fprintf( pFile, "x" );
310  for ( j=p->nWords-1; j >= 0; j-- )
311  Abc_TruthWriteHex( pFile, a+i, p->nVars );
312  fprintf( pFile, "\n" );
313  }
314  fprintf( pFile, "\n" );
315  fclose( pFile );
316 }
317 
318 
319 Abc_TtStore_t * setTtStore(char * pFileInput)
320 {
321  int nVars, nTruths;
322  Abc_TtStore_t * p;
323  // figure out how many truth table and how many variables
324  Abc_TruthGetParams( pFileInput, &nVars, &nTruths );
325  // allocate data-structure
326  p = Abc_TruthStoreAlloc( nVars, nTruths );
327 
328  Abc_TruthStoreRead( pFileInput, p );
329  return p;
330 }
331 
332 
char * malloc()
static void Abc_TruthXorHex(word *p, int k, int d)
Definition: luckyRead.c:30
VOID_HACK free()
static char * Abc_FileRead(char *pFileName)
Definition: luckyRead.c:123
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static void Abc_TruthWriteHexDigit(FILE *pFile, int HexDigit)
Definition: luckyRead.c:46
static Abc_TtStore_t * Abc_Create_TtSpore(char *pFileInput)
Definition: luckyRead.c:200
word ** pFuncs
Definition: luckyInt.h:70
#define SEEK_END
Definition: zconf.h:392
static void WriteToFile1(char *pFileName, Abc_TtStore_t *p, word **a)
Definition: luckyRead.c:275
static ABC_NAMESPACE_IMPL_START int Abc_TruthGetBit(word *p, int i)
Definition: luckyRead.c:23
int nWords
Definition: abcNpn.c:127
void Abc_TruthStoreFree(Abc_TtStore_t *p)
Definition: luckyRead.c:115
static int Abc_TruthGetHex(word *p, int k)
Definition: luckyRead.c:28
Abc_TtStore_t * setTtStore(char *pFileInput)
Definition: luckyRead.c:319
static void WriteToFile(char *pFileName, Abc_TtStore_t *p, word *a)
Definition: luckyRead.c:257
static Abc_TtStore_t * Abc_TruthStoreAlloc(int nVars, int nFuncs)
Definition: luckyRead.c:96
static void Abc_TruthGetParams(char *pFileName, int *pnVars, int *pnTruths)
Definition: luckyRead.c:155
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static void WriteToFile2(char *pFileName, Abc_TtStore_t *p, word *a)
Definition: luckyRead.c:296
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static int Abc_TruthReadHexDigit(char HexChar)
Definition: luckyRead.c:33
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void Abc_TruthStoreRead(char *pFileName, Abc_TtStore_t *p)
Definition: luckyRead.c:210
static void Abc_TruthStoreWrite(char *pFileName, Abc_TtStore_t *p)
Definition: luckyRead.c:239
char * calloc()
static void Abc_TruthReadHex(word *pTruth, char *pString, int nVars)
Definition: luckyRead.c:56
static void Abc_TruthSetBit(word *p, int i)
Definition: luckyRead.c:24
static void Abc_TruthXorBit(word *p, int i)
Definition: luckyRead.c:25
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthSetHex(word *p, int k, int d)
Definition: luckyRead.c:29
static void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: luckyRead.c:81
VOID_HACK rewind()