102static inline int Bbl_ObjIsCi(
Bbl_Obj_t * pObj ) {
return pObj->
fCi; }
103static inline int Bbl_ObjIsCo(
Bbl_Obj_t * pObj ) {
return pObj->
fCo; }
104static inline int Bbl_ObjIsNode(
Bbl_Obj_t * pObj ) {
return!pObj->
fCi && !pObj->
fCo; }
106static inline int Bbl_ObjFaninNum(
Bbl_Obj_t * pObj ) {
return pObj->
nFanins; }
116static inline char * Bbl_ManSop(
Bbl_Man_t *
p,
int h ) {
return (
char *)Bbl_VecFnc(
p->pFncs, h)->pWords; }
117static inline Bbl_Obj_t * Bbl_ManObj(
Bbl_Man_t *
p,
int Id ) {
return Bbl_VecObj(
p->pObjs,
p->vId2Obj->pArray[Id]); }
119#define Bbl_ManForEachObj_int( p, pObj, h ) \
120 for ( h = 0; (h < p->nSize) && (pObj = Bbl_VecObj(p,h)); h += Bbl_ObjSize(pObj) )
121#define Bbl_ManForEachFnc_int( p, pObj, h ) \
122 for ( h = 0; (h < p->nSize) && (pObj = Bbl_VecFnc(p,h)); h += Bbl_FncSize(pObj) )
123#define Bbl_ObjForEachFanin_int( pObj, pFanin, i ) \
124 for ( i = 0; (i < (int)pObj->nFanins) && (pFanin = Bbl_ObjFanin(pObj,i)); i++ )
126#define BBLIF_ALLOC(type, num) ((type *) malloc(sizeof(type) * (num)))
127#define BBLIF_CALLOC(type, num) ((type *) calloc((num), sizeof(type)))
128#define BBLIF_FALLOC(type, num) ((type *) memset(malloc(sizeof(type) * (num)), 0xff, sizeof(type) * (num)))
129#define BBLIF_FREE(obj) ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
130#define BBLIF_REALLOC(type, obj, num) \
131 ((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
132 ((type *) malloc(sizeof(type) * (num))))
149static inline Vec_Int_t * Vec_IntAlloc(
int nCap )
153 if ( nCap > 0 && nCap < 16 )
172static inline Vec_Int_t * Vec_IntStart(
int nSize )
175 p = Vec_IntAlloc( nSize );
177 memset(
p->pArray, 0,
sizeof(
int) * nSize );
192static inline Vec_Int_t * Vec_IntStartNatural(
int nSize )
196 p = Vec_IntAlloc( nSize );
198 for ( i = 0; i < nSize; i++ )
214static inline Vec_Int_t * Vec_IntAllocArray(
int * pArray,
int nSize )
235static inline void Vec_IntFree(
Vec_Int_t *
p )
268static inline int Vec_IntEntry(
Vec_Int_t *
p,
int i )
270 assert( i >= 0 && i < p->nSize );
285static inline void Vec_IntWriteEntry(
Vec_Int_t *
p,
int i,
int Entry )
287 assert( i >= 0 && i < p->nSize );
288 p->pArray[i] = Entry;
302static inline void Vec_IntAddToEntry(
Vec_Int_t *
p,
int i,
int Addition )
304 assert( i >= 0 && i < p->nSize );
305 p->pArray[i] += Addition;
319static inline int Vec_IntEntryLast(
Vec_Int_t *
p )
322 return p->pArray[
p->nSize-1];
336static inline void Vec_IntGrow(
Vec_Int_t *
p,
int nCapMin )
338 if (
p->nCap >= nCapMin )
356static inline void Vec_IntFill(
Vec_Int_t *
p,
int nSize,
int Fill )
359 Vec_IntGrow(
p, nSize );
360 for ( i = 0; i < nSize; i++ )
376static inline void Vec_IntFillExtra(
Vec_Int_t *
p,
int nSize,
int Fill )
379 if (
p->nSize >= nSize )
381 if ( nSize > 2 *
p->nCap )
382 Vec_IntGrow(
p, nSize );
383 else if ( nSize >
p->nCap )
384 Vec_IntGrow(
p, 2 *
p->nCap );
385 for ( i =
p->nSize; i < nSize; i++ )
401static inline int Vec_IntGetEntry(
Vec_Int_t *
p,
int i )
403 Vec_IntFillExtra(
p, i + 1, 0 );
404 return Vec_IntEntry(
p, i );
418static inline void Vec_IntSetEntry(
Vec_Int_t *
p,
int i,
int Entry )
420 Vec_IntFillExtra(
p, i + 1, 0 );
421 Vec_IntWriteEntry(
p, i, Entry );
435static inline void Vec_IntShrink(
Vec_Int_t *
p,
int nSizeNew )
437 assert(
p->nSize >= nSizeNew );
452static inline void Vec_IntClear(
Vec_Int_t *
p )
468static inline void Vec_IntPush(
Vec_Int_t *
p,
int Entry )
470 if (
p->nSize ==
p->nCap )
473 Vec_IntGrow(
p, 16 );
475 Vec_IntGrow(
p, 2 *
p->nCap );
477 p->pArray[
p->nSize++] = Entry;
495static inline Vec_Str_t * Vec_StrAlloc(
int nCap )
499 if ( nCap > 0 && nCap < 16 )
518static inline Vec_Str_t * Vec_StrAllocArray(
char * pArray,
int nSize )
541 while (
p->nSize + nBytes >
p->nCap )
547 return p->pArray +
p->nSize - nBytes;
563 fwrite( &
p->nSize,
sizeof(
int), 1, pFile );
564 fwrite(
p->pArray,
sizeof(
char),
p->nSize, pFile );
581 char * pStr = *ppStr;
582 p = Vec_StrAlloc( 0 );
583 p->nSize = *(
int *)pStr;
584 p->pArray = pStr +
sizeof(int);
585 *ppStr = pStr +
sizeof(int) +
p->nSize *
sizeof(
char);
616static inline void Vec_StrFree(
Vec_Str_t *
p )
641 pFile = fopen( pFileName,
"r" );
644 printf(
"Bbl_ManFileSize(): The file is unavailable (absent or open).\n" );
648 nFileSize = ftell( pFile );
671 pFile = fopen( pFileName,
"rb" );
673 RetValue = fread( pContents, nFileSize, 1, pFile );
694 pFile = fopen( pFileName,
"wb" );
723 pBuffer =
p->pFileData;
727 assert( pBuffer -
p->pFileData ==
p->nFileSize );
729 p->vObj2Id = Vec_IntAlloc( 1000 );
732 Vec_IntPush(
p->vObj2Id, pObj->
Id );
733 pObj->
Id = Vec_IntSize(
p->vObj2Id) - 1;
753 int h, nFuncs = 0, nNodes = 0, nObjs = 0;
755 nObjs++, nNodes += Bbl_ObjIsNode(pObj);
758 printf(
"Total objects = %7d. Total nodes = %7d. Unique functions = %7d.\n", nObjs, nNodes, nFuncs );
759 printf(
"Name manager = %5.2f MB\n", 1.0*Vec_StrSize(
p->pName)/(1 << 20) );
760 printf(
"Objs manager = %5.2f MB\n", 1.0*Vec_StrSize(
p->pObjs)/(1 << 20) );
761 printf(
"Fncs manager = %5.2f MB\n", 1.0*Vec_StrSize(
p->pFncs)/(1 << 20) );
777 if (
p->vId2Obj ) Vec_IntFree(
p->vId2Obj );
778 if (
p->vObj2Id ) Vec_IntFree(
p->vObj2Id );
779 if (
p->vFaninNums ) Vec_IntFree(
p->vFaninNums );
783 p->pName->pArray = NULL;
784 p->pObjs->pArray = NULL;
785 p->pFncs->pArray = NULL;
788 Vec_StrFree(
p->pEnts );
789 Vec_StrFree(
p->pName );
790 Vec_StrFree(
p->pObjs );
791 Vec_StrFree(
p->pFncs );
812 nLength = pName? 4 * ((
strlen(pName) + 1) / 4 + 1) : 0;
813 p->pName = Vec_StrAlloc( nLength );
814 p->pName->nSize =
p->pName->nCap;
816 strcpy(
p->pName->pArray, pName );
817 p->pObjs = Vec_StrAlloc( 1 << 16 );
818 p->pFncs = Vec_StrAlloc( 1 << 16 );
819 p->pEnts = Vec_StrAlloc( 1 << 16 );
p->pEnts->nSize = 1;
820 p->vId2Obj = Vec_IntStart( 1 << 10 );
821 p->vFaninNums = Vec_IntStart( 1 << 10 );
843 for ( i = 0; i < nCubes-1; i++ )
846 for (j = i+1; j < nCubes; j++)
847 if (
memcmp( pCubes[j], pCubes[best_i], (
size_t)nVars ) < 0 )
849 pTemp = pCubes[i]; pCubes[i] = pCubes[best_i]; pCubes[best_i] = pTemp;
866 char ** pCubes, * pSopNew;
867 int c, Length, nCubes;
869 assert( Length % (nVars + 3) == 0 );
870 nCubes = Length / (nVars + 3);
874 memcpy( pSopNew, pSop, (
size_t)(Length + 1) );
878 for ( c = 0; c < nCubes; c++ )
879 pCubes[c] = pSop + c * (nVars + 3);
883 for ( c = 0; c < nCubes; c++ )
884 memcpy( pSopNew + c * (nVars + 3), pCubes[c], (
size_t)(nVars + 3) );
886 pSopNew[nCubes * (nVars + 3)] = 0;
907 return (
char *)pEnt -
p->pEnts->pArray;
925 int h, Length =
strlen(pSop) + 1;
926 int nWords = (Length / 4 + (Length % 4 > 0));
927 if ( nVars > 16 ) nVars = 16;
928 if ( nCubes > 16 ) nCubes = 16;
931 for ( h =
p->SopMap[nVars][nCubes]; h; h = pEnt->
iNext )
933 pEnt = Bbl_VecEnt(
p->pEnts, h );
934 pFnc = Bbl_VecFnc(
p->pFncs, pEnt->
iFunc );
958 int iFunc, Length =
strlen(pSop) + 1;
959 int nWords = Length / 4 + (Length % 4 > 0);
965 if ( iFunc == Vec_StrSize(
p->pFncs) )
971 assert( iFunc == (
char *)pFnc -
p->pFncs->pArray );
993 printf(
"Attempting to create a combinational input with %d fanins (should be 0).\n", nFanins );
998 printf(
"Attempting to create a combinational output with %d fanins (should be 1).\n", nFanins );
1003 Vec_IntSetEntry(
p->vId2Obj, ObjId, (
char *)pObj -
p->pObjs->pArray );
1004 Vec_IntSetEntry(
p->vFaninNums, ObjId, 0 );
1027 pObj = Bbl_ManObj(
p, ObjId );
1028 if ( Bbl_ObjIsCi(pObj) )
1030 printf(
"Bbl_ManAddFanin(): Cannot add fanin of the combinational input (Id = %d).\n", ObjId );
1033 pFanin = Bbl_ManObj(
p, FaninId );
1034 if ( Bbl_ObjIsCo(pFanin) )
1036 printf(
"Bbl_ManAddFanin(): Cannot add fanout of the combinational output (Id = %d).\n", FaninId );
1039 iFanin = Vec_IntEntry(
p->vFaninNums, ObjId );
1040 if ( iFanin >= (
int)pObj->
nFanins )
1042 printf(
"Bbl_ManAddFanin(): Trying to add more fanins to object (Id = %d) than declared (%d).\n", ObjId, pObj->
nFanins );
1046 Vec_IntWriteEntry(
p->vFaninNums, ObjId, iFanin+1 );
1047 pObj->
pFanins[iFanin] = (
char *)pObj - (
char *)pFanin;
1065 int h, RetValue = 1;
1068 if ( Bbl_ObjIsNode(pObj) && pObj->
Fnc == -1 )
1069 RetValue = 0, printf(
"Bbl_ManCheck(): Node %d does not have function specified.\n", pObj->
Id );
1070 if ( Bbl_ObjIsCi(pObj) && pObj->
Fnc != -1 )
1071 RetValue = 0, printf(
"Bbl_ManCheck(): CI with %d has function specified.\n", pObj->
Id );
1072 if ( Bbl_ObjIsCo(pObj) && pObj->
Fnc != -1 )
1073 RetValue = 0, printf(
"Bbl_ManCheck(): CO with %d has function specified.\n", pObj->
Id );
1074 if ( Vec_IntEntry(
p->vFaninNums, pObj->
Id) != (
int)pObj->
nFanins )
1075 RetValue = 0, printf(
"Bbl_ManCheck(): Object %d has less fanins (%d) than declared (%d).\n",
1076 pObj->
Id, Vec_IntEntry(
p->vFaninNums, pObj->
Id), pObj->
nFanins );
1117 return p->pName->pArray;
1133 return p->pFncs->nSize;
1149 return Bbl_VecObj(
p->pObjs, 0 );
1165 char * pNext = (
char *)pObj + Bbl_ObjSize(pObj);
1166 char * pEdge =
p->pObjs->pArray +
p->pObjs->nSize;
1167 return (
Bbl_Obj_t *)(pNext < pEdge ? pNext : NULL);
1183 return Bbl_ObjFaninNum(
p) ? Bbl_ObjFanin(
p, 0 ) : NULL;
1202 if ( pFanin == pPrev )
1204 return i < Bbl_ObjFaninNum(
p) - 1 ? Bbl_ObjFanin(
p, i+1 ) : NULL;
1222 pFile = fopen( pFileName,
"w" );
1223 fprintf( pFile,
"# Test file written by Bbl_ManDumpBlif() in ABC.\n" );
1229 fprintf( pFile,
".inputs %d\n",
Bbl_ObjId(pObj) );
1231 fprintf( pFile,
".outputs %d\n",
Bbl_ObjId(pObj) );
1234 fprintf( pFile,
".names" );
1236 fprintf( pFile,
" %d",
Bbl_ObjId(pFanin) );
1237 fprintf( pFile,
" %d\n",
Bbl_ObjId(pObj) );
1247 fprintf( pFile,
".names" );
1249 fprintf( pFile,
" %d",
Bbl_ObjId(pFanin) );
1250 fprintf( pFile,
" %d\n",
Bbl_ObjId(pObj) );
1251 fprintf( pFile,
"1 1\n" );
1253 fprintf( pFile,
".end\n" );
1275 char * pResult, * pTemp;
1276 int nMints, nOnes, b, v;
1277 assert( nVars >= 0 && nVars <= 16 );
1278 nMints = (1 << nVars);
1281 for ( b = 0; b < nMints; b++ )
1282 nOnes += ((pTruth[b>>5] >> (b&31)) & 1);
1284 if ( nOnes == 0 || nOnes == nMints )
1287 for ( v = 0; v < nVars; v++ )
1290 *pTemp++ = nOnes?
'1' :
'0';
1293 assert( pTemp - pResult == nVars + 4 );
1296 pResult = pTemp =
BBLIF_ALLOC(
char, nOnes * (nVars + 3) + 1 );
1297 for ( b = 0; b < nMints; b++ )
1299 if ( ((pTruth[b>>5] >> (b&31)) & 1) == 0 )
1301 for ( v = 0; v < nVars; v++ )
1302 *pTemp++ = ((b >> v) & 1)?
'1' :
'0';
1308 assert( pTemp - pResult == nOnes * (nVars + 3) + 1 );
1323static inline void Bbl_ManSopToTruthElem(
int nVars,
unsigned ** pVars )
1325 unsigned Masks[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
1327 nWords = (nVars <= 5 ? 1 : (1 << (nVars - 5)));
1328 for ( i = 0; i < nVars; i++ )
1332 for ( k = 0; k <
nWords; k++ )
1333 pVars[i][k] = Masks[i];
1337 for ( k = 0; k <
nWords; k++ )
1338 if ( k & (1 << (i-5)) )
1339 pVars[i][k] = ~(unsigned)0;
1364 unsigned * pTruth, * pCube, * pVars[16];
1365 int nWords = nVars <= 5 ? 1 : (1 << (nVars - 5));
1366 int v, c, w, nCubes, fCompl = 0;
1369 if (
strlen(pSop) % (nVars + 3) != 0 )
1371 printf(
"Bbl_ManSopToTruth(): SOP is represented incorrectly.\n" );
1377 for ( v = 1; v < nVars; v++ )
1378 pVars[v] = pVars[v-1] +
nWords;
1379 pCube = pVars[v-1] +
nWords;
1380 Bbl_ManSopToTruthElem( nVars, pVars );
1383 nCubes =
strlen(pSop) / (nVars + 3);
1384 for ( c = 0; c < nCubes; c++ )
1386 fCompl = (pSop[nVars+1] ==
'0');
1389 for ( v = 0; v < nVars; v++ )
1390 if ( pSop[v] ==
'1' )
1391 for ( w = 0; w <
nWords; w++ )
1392 pCube[w] &= pVars[v][w];
1393 else if ( pSop[v] ==
'0' )
1394 for ( w = 0; w <
nWords; w++ )
1395 pCube[w] &= ~pVars[v][w];
1397 for ( w = 0; w <
nWords; w++ )
1398 pTruth[w] |= pCube[w];
1400 pSop += (nVars + 3);
1404 for ( w = 0; w <
nWords; w++ )
1405 pTruth[w] = ~pTruth[w];
1428 printf(
"Old SOP:\n%s\n", pSop );
1429 printf(
"New SOP:\n%s\n", pSopNew );
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
int Bbl_ManFileSize(char *pFileName)
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
int Bbl_ManFncSize(Bbl_Man_t *p)
int Bbl_ObjFaninNumber(Bbl_Obj_t *p)
struct Bbl_Fnc_t_ Bbl_Fnc_t
struct Vec_Str_t_ Vec_Str_t
char * Bbl_ManFileRead(char *pFileName)
void Bbl_ManAddFanin(Bbl_Man_t *p, int ObjId, int FaninId)
void Bbl_ManDumpBlif(Bbl_Man_t *p, char *pFileName)
#define BBLIF_ALLOC(type, num)
unsigned * Bbl_ManSopToTruth(char *pSop, int nVars)
int Bbl_ManSopCheckUnique(Bbl_Man_t *p, char *pSop, int nVars, int nCubes, int iFunc)
void Bbl_ManTestTruth(char *pSop, int nVars)
char * Bbl_ManSortSop(char *pSop, int nVars)
struct Bbl_Ent_t_ Bbl_Ent_t
void Bbl_ManSortCubes(char **pCubes, int nCubes, int nVars)
void Vec_StrWrite(FILE *pFile, Vec_Str_t *p)
char * Bbl_ManTruthToSop(unsigned *pTruth, int nVars)
int Bbl_ManSaveSop(Bbl_Man_t *p, char *pSop, int nVars)
#define Bbl_ManForEachObj_int(p, pObj, h)
int Bbl_ObjIsInput(Bbl_Obj_t *p)
char * Bbl_ManName(Bbl_Man_t *p)
Bbl_Obj_t * Bbl_ObjFaninFirst(Bbl_Obj_t *p)
#define Bbl_ObjForEachFanin_int(pObj, pFanin, i)
int Bbl_ObjIsLut(Bbl_Obj_t *p)
int Bbl_ObjIsMarked(Bbl_Obj_t *p)
int Bbl_ManCreateEntry(Bbl_Man_t *p, int iFunc, int iNext)
int Bbl_ObjIdOriginal(Bbl_Man_t *pMan, Bbl_Obj_t *p)
int Bbl_ObjIsOutput(Bbl_Obj_t *p)
int Bbl_ManCheck(Bbl_Man_t *p)
void Bbl_ManPrintStats(Bbl_Man_t *p)
int Bbl_ObjFncHandle(Bbl_Obj_t *p)
Bbl_Man_t * Bbl_ManReadBinaryBlif(char *pFileName)
#define Bbl_ManForEachFnc_int(p, pObj, h)
void Bbl_ManDumpBinaryBlif(Bbl_Man_t *p, char *pFileName)
Bbl_Man_t * Bbl_ManStart(char *pName)
MACRO DEFINITIONS ///.
#define BBLIF_REALLOC(type, obj, num)
char * Bbl_ObjSop(Bbl_Man_t *pMan, Bbl_Obj_t *p)
int Bbl_ObjId(Bbl_Obj_t *p)
Bbl_Obj_t * Bbl_ObjFaninNext(Bbl_Obj_t *p, Bbl_Obj_t *pPrev)
Vec_Str_t * Vec_StrRead(char **ppStr)
void Bbl_ObjMark(Bbl_Obj_t *p)
void Bbl_ManCreateObject(Bbl_Man_t *p, Bbl_Type_t Type, int ObjId, int nFanins, char *pSop)
Bbl_Obj_t * Bbl_ManObjFirst(Bbl_Man_t *p)
Bbl_Obj_t * Bbl_ManObjNext(Bbl_Man_t *p, Bbl_Obj_t *pObj)
char * Vec_StrFetch(Vec_Str_t *p, int nBytes)
void Bbl_ManStop(Bbl_Man_t *p)
struct Bbl_Obj_t_ Bbl_Obj_t
#define Bbl_ObjForEachFanin(pObj, pFanin)
struct Bbl_Man_t_ Bbl_Man_t
#define Bbl_ManForEachObj(p, pObj)