abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
luckyRead.c File Reference
#include "luckyInt.h"

Go to the source code of this file.

Functions

static ABC_NAMESPACE_IMPL_START int Abc_TruthGetBit (word *p, int i)
 
static void Abc_TruthSetBit (word *p, int i)
 
static void Abc_TruthXorBit (word *p, int i)
 
static int Abc_TruthGetHex (word *p, int k)
 
static void Abc_TruthSetHex (word *p, int k, int d)
 
static void Abc_TruthXorHex (word *p, int k, int d)
 
static int Abc_TruthReadHexDigit (char HexChar)
 
static void Abc_TruthWriteHexDigit (FILE *pFile, int HexDigit)
 
static void Abc_TruthReadHex (word *pTruth, char *pString, int nVars)
 
static void Abc_TruthWriteHex (FILE *pFile, word *pTruth, int nVars)
 
static Abc_TtStore_tAbc_TruthStoreAlloc (int nVars, int nFuncs)
 
void Abc_TruthStoreFree (Abc_TtStore_t *p)
 
static char * Abc_FileRead (char *pFileName)
 
static void Abc_TruthGetParams (char *pFileName, int *pnVars, int *pnTruths)
 
static Abc_TtStore_tAbc_Create_TtSpore (char *pFileInput)
 
static void Abc_TruthStoreRead (char *pFileName, Abc_TtStore_t *p)
 
static void Abc_TruthStoreWrite (char *pFileName, Abc_TtStore_t *p)
 
static void WriteToFile (char *pFileName, Abc_TtStore_t *p, word *a)
 
static void WriteToFile1 (char *pFileName, Abc_TtStore_t *p, word **a)
 
static void WriteToFile2 (char *pFileName, Abc_TtStore_t *p, word *a)
 
Abc_TtStore_tsetTtStore (char *pFileInput)
 

Function Documentation

static Abc_TtStore_t* Abc_Create_TtSpore ( char *  pFileInput)
static

Definition at line 200 of file luckyRead.c.

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 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
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
static char* Abc_FileRead ( char *  pFileName)
static

Definition at line 123 of file luckyRead.c.

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 }
char * malloc()
#define SEEK_END
Definition: zconf.h:392
VOID_HACK rewind()
static ABC_NAMESPACE_IMPL_START int Abc_TruthGetBit ( word p,
int  i 
)
inlinestatic

CFile****************************************************************

FileName [luckyRead.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [Semi-canonical form computation package.]

Synopsis [Reading truth tables from file.]

Author [Jake]

Date [Started - August 2012]

Definition at line 23 of file luckyRead.c.

23 { return (int)(p[i>>6] >> (i & 63)) & 1; }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static int Abc_TruthGetHex ( word p,
int  k 
)
inlinestatic

Definition at line 28 of file luckyRead.c.

28 { return (int)(p[k>>4] >> ((k<<2) & 63)) & 15; }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static void Abc_TruthGetParams ( char *  pFileName,
int *  pnVars,
int *  pnTruths 
)
static

Definition at line 155 of file luckyRead.c.

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 }
static char * Abc_FileRead(char *pFileName)
Definition: luckyRead.c:123
static void Abc_TruthReadHex ( word pTruth,
char *  pString,
int  nVars 
)
inlinestatic

Definition at line 56 of file luckyRead.c.

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 }
int nWords
Definition: abcNpn.c:127
static int Abc_TruthReadHexDigit(char HexChar)
Definition: luckyRead.c:33
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthSetHex(word *p, int k, int d)
Definition: luckyRead.c:29
static int Abc_TruthReadHexDigit ( char  HexChar)
inlinestatic

Definition at line 33 of file luckyRead.c.

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 }
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthSetBit ( word p,
int  i 
)
inlinestatic

Definition at line 24 of file luckyRead.c.

24 { p[i>>6] |= (((word)1)<<(i & 63)); }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static void Abc_TruthSetHex ( word p,
int  k,
int  d 
)
inlinestatic

Definition at line 29 of file luckyRead.c.

29 { p[k>>4] |= (((word)d)<<((k<<2) & 63)); }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static Abc_TtStore_t* Abc_TruthStoreAlloc ( int  nVars,
int  nFuncs 
)
inlinestatic

Definition at line 96 of file luckyRead.c.

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 }
char * malloc()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
word ** pFuncs
Definition: luckyInt.h:70
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
char * calloc()
void Abc_TruthStoreFree ( Abc_TtStore_t p)

Definition at line 115 of file luckyRead.c.

116 {
117  free( p->pFuncs[0] );
118  free( p->pFuncs );
119  free( p );
120 }
VOID_HACK free()
word ** pFuncs
Definition: luckyInt.h:70
static void Abc_TruthStoreRead ( char *  pFileName,
Abc_TtStore_t p 
)
static

Definition at line 210 of file luckyRead.c.

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 }
static char * Abc_FileRead(char *pFileName)
Definition: luckyRead.c:123
word ** pFuncs
Definition: luckyInt.h:70
static void Abc_TruthReadHex(word *pTruth, char *pString, int nVars)
Definition: luckyRead.c:56
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthStoreWrite ( char *  pFileName,
Abc_TtStore_t p 
)
static

Definition at line 239 of file luckyRead.c.

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 }
word ** pFuncs
Definition: luckyInt.h:70
static void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: luckyRead.c:81
static void Abc_TruthWriteHex ( FILE *  pFile,
word pTruth,
int  nVars 
)
inlinestatic

Definition at line 81 of file luckyRead.c.

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 }
static void Abc_TruthWriteHexDigit(FILE *pFile, int HexDigit)
Definition: luckyRead.c:46
static int Abc_TruthGetHex(word *p, int k)
Definition: luckyRead.c:28
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthWriteHexDigit ( FILE *  pFile,
int  HexDigit 
)
inlinestatic

Definition at line 46 of file luckyRead.c.

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 }
#define assert(ex)
Definition: util_old.h:213
static void Abc_TruthXorBit ( word p,
int  i 
)
inlinestatic

Definition at line 25 of file luckyRead.c.

25 { p[i>>6] ^= (((word)1)<<(i & 63)); }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static void Abc_TruthXorHex ( word p,
int  k,
int  d 
)
inlinestatic

Definition at line 30 of file luckyRead.c.

30 { p[k>>4] ^= (((word)d)<<((k<<2) & 63)); }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
Abc_TtStore_t* setTtStore ( char *  pFileInput)

Definition at line 319 of file luckyRead.c.

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 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
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
static void Abc_TruthStoreRead(char *pFileName, Abc_TtStore_t *p)
Definition: luckyRead.c:210
static void WriteToFile ( char *  pFileName,
Abc_TtStore_t p,
word a 
)
static

Definition at line 257 of file luckyRead.c.

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 }
static void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: luckyRead.c:81
static void WriteToFile1 ( char *  pFileName,
Abc_TtStore_t p,
word **  a 
)
static

Definition at line 275 of file luckyRead.c.

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 }
static void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: luckyRead.c:81
static void WriteToFile2 ( char *  pFileName,
Abc_TtStore_t p,
word a 
)
static

Definition at line 296 of file luckyRead.c.

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 }
static void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: luckyRead.c:81