ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
abcRpo.c
Go to the documentation of this file.
1
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
42static inline int Abc_TruthGetBit(word * p, int i) {
43 return (int) (p[i >> 6] >> (i & 63)) & 1;
44}
45
46static inline void Abc_TruthSetBit(word * p, int i) {
47 p[i >> 6] |= (((word) 1) << (i & 63));
48}
49
50static 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
56static inline int Abc_TruthGetHex(word * p, int k) {
57 return (int) (p[k >> 4] >> ((k << 2) & 63)) & 15;
58}
59
60static inline void Abc_TruthSetHex(word * p, int k, int d) {
61 p[k >> 4] |= (((word) d) << ((k << 2) & 63));
62}
63
64static inline void Abc_TruthXorHex(word * p, int k, int d) {
65 p[k >> 4] ^= (((word) d) << ((k << 2) & 63));
66}
67
71
72// read one hex character
73
74static 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
87static 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
97static void Abc_TruthReadHex(word * pTruth, char * pString, int nVars) {
98 int nWords = (nVars < 7) ? 1 : (1 << (nVars - 6));
99 int k, Digit, nDigits = (nVars < 7) ? (1 << (nVars-2)) : (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
121static 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
144static Rpo_TtStore_t * Abc_TruthStoreAlloc(int nVars, int nFuncs) {
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
162static Rpo_TtStore_t * Abc_TruthStoreAlloc2(int nVars, int nFuncs, word * pBuffer) {
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
179static 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
197extern int Abc_FileSize(char * pFileName);
198
210extern char * Abc_FileRead(char * pFileName);
211
223extern void Abc_TruthGetParams(char * pFileName, int * pnVars, int * pnTruths);
224
225
237static 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
273static 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
301static Rpo_TtStore_t * Abc_TtStoreLoad(char * pFileName, int nVarNum) {
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
345void 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// }
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
394void Abc_TruthRpoTest(char * pFileName, int nVarNum, int nThreshold, int fVerbose) {
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
425int 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
438
439
441
442
void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition abcDec.c:117
void Abc_TruthStoreRead(char *pFileName, Abc_TtStore_t *p)
Definition abcDec.c:321
void Abc_TruthReadHex(word *pTruth, char *pString, int nVars)
Definition abcDec.c:92
Abc_TtStore_t * Abc_TruthStoreAlloc2(int nVars, int nFuncs, word *pBuffer)
Definition abcDec.c:159
void Abc_TtStoreWrite(char *pFileName, Abc_TtStore_t *p, int fBinary)
Definition abcDec.c:359
void Abc_TtStoreFree(Abc_TtStore_t *p, int nVarNum)
Definition abcDec.c:176
Abc_TtStore_t * Abc_TtStoreLoad(char *pFileName, int nVarNum)
Definition abcDec.c:396
Abc_TtStore_t * Abc_TruthStoreAlloc(int nVars, int nFuncs)
Definition abcDec.c:141
int nWords
Definition abcNpn.c:127
void Abc_TruthRpoTest(char *pFileName, int nVarNum, int nThreshold, int fVerbose)
Definition abcRpo.c:394
int Abc_FileSize(char *pFileName)
Definition abcDec.c:195
void Abc_TruthGetParams(char *pFileName, int *pnVars, int *pnTruths)
Definition abcDec.c:263
typedefABC_NAMESPACE_IMPL_START struct Rpo_TtStore_t_ Rpo_TtStore_t
Definition abcRpo.c:30
char * Abc_FileRead(char *pFileName)
Definition abcDec.c:223
int Abc_RpoTest(char *pFileName, int nVarNum, int nThreshold, int fVerbose)
Definition abcRpo.c:425
void Abc_TruthRpoPerform(Rpo_TtStore_t *p, int nThreshold, int fVerbose)
Definition abcRpo.c:345
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
struct Literal_t_ Literal_t
Definition literal.h:50
Cube * p
Definition exorList.c:222
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
Literal_t * Rpo_Factorize(unsigned *target, int nVars, int nThreshold, int verbose)
Definition rpo.c:174
int lit
Definition satVec.h:130
for(p=first;p->value< newval;p=p->next)
word ** pFuncs
Definition abcRpo.c:36
#define assert(ex)
Definition util_old.h:213
char * memset()
char * malloc()