26# define __builtin_popcount __popcnt
65 if ( pName == NULL )
return NULL;
84 char * pSopCover, * pCube;
87 Length = nCubes * (nVars + 3);
89 memset( pSopCover,
'-', (
size_t)Length );
90 pSopCover[Length] = 0;
92 for ( i = 0; i < nCubes; i++ )
94 pCube = pSopCover + i * (nVars + 3);
95 pCube[nVars + 0] =
' ';
96 pCube[nVars + 1] =
'1';
97 pCube[nVars + 2] =
'\n';
148 Buffer[0] =
'1' - fCompl0;
149 Buffer[1] =
'1' - fCompl1;
173 for ( i = 0; i < nVars; i++ )
174 pSop[i] =
'1' - (pfCompl? pfCompl[i] : 0);
175 pSop[nVars + 1] =
'1';
195 for ( i = 0; i < nVars; i++ )
197 pSop[nVars + 1] =
'0';
217 for ( i = 0; i < nVars; i++ )
218 pSop[i] =
'0' + (pfCompl? pfCompl[i] : 0);
219 pSop[nVars + 1] =
'0';
236 char * pSop, * pCube;
242 pCube[i] =
'1' - (pfCompl? pfCompl[i] : 0);
264 for ( i = 0; i < nVars; i++ )
302 assert( pSop[nVars+2] ==
'\n' );
385 char * pSop, * pCube;
386 int nMints, Counter, i, k;
391 nMints = (1 << nVars);
392 for ( i = 0; i < nMints; i++ )
393 Counter += ((pTruth[i>>5] & (1 << (i&31))) > 0);
402 for ( i = 0; i < nMints; i++ )
403 if ( (pTruth[i>>5] & (1 << (i&31))) > 0 )
405 pCube = pSop + Counter * (nVars + 3);
406 for ( k = 0; k < nVars; k++ )
407 pCube[k] =
'0' + ((i & (1 << k)) > 0);
426 char * pSop, * pCube;
427 int i, k, Entry, Literal;
428 assert( Vec_IntSize(vCover) > 0 );
429 if ( Vec_IntSize(vCover) == 0 )
432 pSop =
Abc_SopStart( pMan, Vec_IntSize(vCover), nVars );
436 pCube = pSop + i * (nVars + 3);
437 for ( k = 0; k < nVars; k++ )
439 Literal = 3 & (Entry >> (k << 1));
442 else if ( Literal == 2 )
444 else if ( Literal != 0 )
465 int w,
nWords = Abc_Truth6WordNum( nVars );
468 for ( w = 0; w <
nWords; w++ )
474 for ( w = 0; w <
nWords; w++ )
481 int RetValue =
Kit_TruthIsop( (
unsigned *)pTruth, nVars, vCover, 1 );
483 assert( RetValue == 0 || RetValue == 1 );
509 Vec_IntClear( vCover );
510 for ( pCube = pSop; *pCube; pCube += nVars + 3 )
513 for ( k = nVars - 1; k >= 0; k-- )
514 if ( pCube[k] ==
'0' )
515 Entry = (Entry << 2) | 1;
516 else if ( pCube[k] ==
'1' )
517 Entry = (Entry << 2) | 2;
518 else if ( pCube[k] ==
'-' )
519 Entry = (Entry << 2);
522 Vec_IntPush( vCover, Entry );
543 for ( pCur = pSop; *pCur; pCur++ )
544 nCubes += (*pCur ==
'\n');
565 for ( pCur = pSop; *pCur; pCur++ )
567 nLits -= (*pCur ==
'\n');
568 nLits += (*pCur ==
'0' || *pCur ==
'1');
587 for ( pCur = pSop; *pCur !=
'\n'; pCur++ )
590 return pCur - pSop - 2;
607 if ( pSop[nVars+1] ==
'0' || pSop[nVars+1] ==
'n' )
609 if ( pSop[nVars+1] ==
'1' || pSop[nVars+1] ==
'x' )
632 if ( pCube[i] !=
'-' )
633 return pCube[i] -
'0';
651 for ( pCur = pSop; *pCur; pCur++ )
654 if ( *(pCur - 1) ==
'0' )
656 else if ( *(pCur - 1) ==
'1' )
658 else if ( *(pCur - 1) ==
'x' )
660 else if ( *(pCur - 1) ==
'n' )
685 if ( pCube[iVar] ==
'0' )
687 else if ( pCube[iVar] ==
'1' )
706 for ( pCur = pSop; *pCur; pCur++ )
708 return (
int)(*(pCur - 1) ==
'0' || *(pCur - 1) ==
'n');
726 return pSop[0] ==
' ' && pSop[1] ==
'0';
742 return pSop[0] ==
' ' && pSop[1] ==
'1';
760 if ( (pSop[0] ==
'1' && pSop[2] ==
'1') || (pSop[0] ==
'0' && pSop[2] ==
'0') )
780 if ( (pSop[0] ==
'0' && pSop[2] ==
'1') || (pSop[0] ==
'1' && pSop[2] ==
'0') )
801 for ( pCur = pSop; *pCur !=
' '; pCur++ )
804 if ( pCur[1] !=
'1' )
822 char * pCube, * pCur;
831 for ( pCur = pCube; *pCur !=
' '; pCur++ )
832 nLits += ( *pCur !=
'-' );
853 for ( pCur = pSop; *pCur; pCur++ )
855 return (
int)(*(pCur - 1) ==
'x' || *(pCur - 1) ==
'n');
873 char * pCubes, * pCubesOld;
874 int fFound0 = 0, fFound1 = 0;
877 for ( pCubes = pSop; *pCubes; pCubes++ )
880 for ( pCubesOld = pCubes; *pCubes !=
' '; pCubes++ );
882 if ( pCubes - pCubesOld != nFanins )
884 fprintf( stdout,
"Abc_SopCheck: SOP has a mismatch between its cover size (%d) and its fanin number (%d).\n",
885 (
int)(ABC_PTRDIFF_T)(pCubes - pCubesOld), nFanins );
890 if ( *pCubes ==
'0' )
892 else if ( *pCubes ==
'1' )
894 else if ( *pCubes !=
'x' && *pCubes !=
'n' )
896 fprintf( stdout,
"Abc_SopCheck: SOP has a strange character (%c) in the output part of its cube.\n", *pCubes );
901 if ( *pCubes !=
'\n' )
903 fprintf( stdout,
"Abc_SopCheck: SOP has a cube without new line in the end.\n" );
907 if ( fFound0 && fFound1 )
909 fprintf( stdout,
"Abc_SopCheck: SOP has cubes in both phases.\n" );
928 char * pBase;
int nVars;
929 int Log2 = Abc_Base2Log(
strlen(pToken) );
930 if ( fHex &&
strlen(pToken) == 1 )
932 if ( (1 << Log2) != (
int)
strlen(pToken) )
934 printf(
"The truth table length (%d) is not power-of-2.\n", (
int)
strlen(pToken) );
935 Vec_PtrFreeData( vRes );
936 Vec_PtrShrink( vRes, 0 );
939 if ( Vec_PtrSize(vRes) == 0 )
941 pBase = (
char *)Vec_PtrEntry( vRes, 0 );
943 if ( nVars != Log2+2*fHex )
945 printf(
"Truth table #1 has %d vars while truth table #%d has %d vars.\n", nVars, Vec_PtrSize(vRes)+1, Log2+2*fHex );
946 Vec_PtrFreeData( vRes );
947 Vec_PtrShrink( vRes, 0 );
966 char * pSopCover, * pCube;
967 int nTruthSize, nVars, Digit, Length, Mint, i, b;
971 nTruthSize =
strlen(pTruth);
972 nVars = Abc_Base2Log( nTruthSize );
973 if ( nTruthSize != (1 << (nVars)) )
975 printf(
"String %s does not look like a truth table of a %d-variable function.\n", pTruth, nVars );
980 vMints = Vec_IntAlloc( 100 );
981 for ( i = 0; i < nTruthSize; i++ )
983 if ( pTruth[i] >=
'0' && pTruth[i] <=
'1' )
984 Digit = pTruth[i] -
'0';
987 Vec_IntFree( vMints );
988 printf(
"String %s does not look like a binary representation of the truth table.\n", pTruth );
992 Vec_IntPush( vMints, nTruthSize - 1 - i );
1002 if ( Vec_IntSize(vMints) == 0 || Vec_IntSize(vMints) == (1 << nVars) )
1006 pSopCover[1] =
'0' + (Vec_IntSize(vMints) > 0);
1007 pSopCover[2] =
'\n';
1013 Length = Vec_IntSize(vMints) * (nVars + 3);
1014 pSopCover =
ABC_ALLOC(
char, Length + 1 );
1015 pSopCover[Length] = 0;
1018 pCube = pSopCover + i * (nVars + 3);
1019 for ( b = 0; b < nVars; b++ )
1021 if ( Mint & (1 << b) )
1025 pCube[nVars + 0] =
' ';
1026 pCube[nVars + 1] =
'1';
1027 pCube[nVars + 2] =
'\n';
1030 Vec_IntFree( vMints );
1036 char * pCopy = Abc_UtilStrsav(pTruth);
1037 char * pToken =
strtok( pCopy,
" \r\n\t|" );
1043 pToken =
strtok( NULL,
" \r\n\t|" );
1062 char * pSopCover, * pCube;
1063 int nTruthSize, nVars, Digit, Length, Mint, i, b;
1067 nTruthSize =
strlen(pTruth);
1068 nVars = (nTruthSize < 2) ? 2 : Abc_Base2Log(nTruthSize) + 2;
1069 if ( nTruthSize != (1 << (nVars-2)) )
1071 printf(
"String %s does not look like a truth table of a %d-variable function.\n", pTruth, nVars );
1076 vMints = Vec_IntAlloc( 100 );
1077 for ( i = 0; i < nTruthSize; i++ )
1079 if ( pTruth[i] >=
'0' && pTruth[i] <=
'9' )
1080 Digit = pTruth[i] -
'0';
1081 else if ( pTruth[i] >=
'a' && pTruth[i] <=
'f' )
1082 Digit = 10 + pTruth[i] -
'a';
1083 else if ( pTruth[i] >=
'A' && pTruth[i] <=
'F' )
1084 Digit = 10 + pTruth[i] -
'A';
1087 printf(
"String %s does not look like a hexadecimal representation of the truth table.\n", pTruth );
1090 for ( b = 0; b < 4; b++ )
1091 if ( Digit & (1 << b) )
1092 Vec_IntPush( vMints, 4*(nTruthSize-1-i)+b );
1096 if ( Vec_IntSize(vMints) == 0 || Vec_IntSize(vMints) == (1 << nVars) )
1100 pSopCover[1] =
'0' + (Vec_IntSize(vMints) > 0);
1101 pSopCover[2] =
'\n';
1106 Length = Vec_IntSize(vMints) * (nVars + 3);
1107 pSopCover =
ABC_ALLOC(
char, Length + 1 );
1108 pSopCover[Length] = 0;
1111 pCube = pSopCover + i * (nVars + 3);
1112 for ( b = 0; b < nVars; b++ )
1114 if ( Mint & (1 << b) )
1118 pCube[nVars + 0] =
' ';
1119 pCube[nVars + 1] =
'1';
1120 pCube[nVars + 2] =
'\n';
1124 Vec_IntFree( vMints );
1130 char * pCopy = Abc_UtilStrsav(pTruth);
1131 char * pToken =
strtok( pCopy,
" \r\n\t|" );
1134 if ( pToken[0] ==
'0' && pToken[1] ==
'x' )
1139 pToken =
strtok( NULL,
" \r\n\t|" );
1147 int m, i, o, nOuts = Abc_Base2Log( nVars + 1 );
1148 Vec_Ptr_t * vRes = Vec_PtrAlloc( nOuts );
1149 for ( o = 0; o < nOuts; o++ )
1151 Vec_Str_t * vStr = Vec_StrAlloc( 1000 );
1152 for ( m = 0; m < (1 << nVars); m++ ) {
1153 int nOnes = __builtin_popcount(m);
1154 if ( !((nOnes >> o) & 1) )
1156 for ( i = 0; i < nVars; i++ )
1157 Vec_StrPush( vStr, ((m >> i) & 1) ?
'1' :
'0' );
1158 Vec_StrPush( vStr,
' ' );
1159 Vec_StrPush( vStr,
'1' );
1160 Vec_StrPush( vStr,
'\n' );
1162 Vec_StrPush( vStr,
'\0' );
1164 Vec_PtrPush( vRes, Vec_StrReleaseArray(vStr) );
1165 Vec_StrFree( vStr );
1184 assert( iValue < nValues );
1185 sprintf( Buffer,
"d0\n%d 1\n", iValue );
1204 int v, Counter, fFirst = 1, nBits = Abc_Base2Log(nValues);
1208 for ( v = 0; v < nValues; v++ )
1209 Counter += ( (v & (1 << iBit)) > 0 );
1211 vSop = Vec_StrAlloc( 100 );
1212 Vec_StrPrintStr( vSop,
"d0\n" );
1214 Vec_StrPrintStr( vSop,
"(" );
1215 for ( v = 0; v < nValues; v++ )
1216 if ( v & (1 << iBit) )
1221 Vec_StrPush( vSop,
',' );
1222 Vec_StrPrintNum( vSop, v );
1225 Vec_StrPrintStr( vSop,
")" );
1226 Vec_StrPrintStr( vSop,
" 1\n" );
1227 Vec_StrPush( vSop, 0 );
1229 Vec_StrFree( vSop );
1250 vSop = Vec_StrAlloc( 100 );
1251 for ( i = 0; i < nValues; i++ )
1253 for ( k = 0; k < nValues; k++ )
1256 Vec_StrPrintStr( vSop,
"1 " );
1258 Vec_StrPrintStr( vSop,
"- " );
1260 Vec_StrPrintNum( vSop, i );
1261 Vec_StrPush( vSop,
'\n' );
1263 Vec_StrPush( vSop, 0 );
1265 Vec_StrFree( vSop );
1284 int i, b, nBits = Abc_Base2Log(nValues);
1285 assert( nValues > 1 && nValues <= (1<<nBits) );
1286 vSop = Vec_StrAlloc( 100 );
1287 for ( i = 0; i < nValues; i++ )
1289 for ( b = 0; b < nBits; b++ )
1291 Vec_StrPrintNum( vSop, (
int)((i & (1 << b)) > 0) );
1292 Vec_StrPush( vSop,
' ' );
1294 Vec_StrPrintNum( vSop, i );
1295 Vec_StrPush( vSop,
'\n' );
1297 Vec_StrPush( vSop, 0 );
1299 Vec_StrFree( vSop );
1316 static word Truth[8] = {
1329 assert( nVars >= 0 && nVars <= 6 );
1330 assert( nVars == nInputs );
1333 for ( v = 0; v < nVars; v++,
lit++ )
1335 if ( pSop[
lit] ==
'1' )
1337 else if ( pSop[
lit] ==
'0' )
1339 else if ( pSop[
lit] !=
'-' )
1348 }
while ( pSop[
lit] );
1367 static word Truth[7][2] = {
1379 assert( nVars >= 0 && nVars <= 7 );
1380 assert( nVars == nInputs );
1384 for ( v = 0; v < nVars; v++,
lit++ )
1386 if ( pSop[
lit] ==
'1' )
1388 Cube[0] &= Truth[v][0];
1389 Cube[1] &= Truth[v][1];
1391 else if ( pSop[
lit] ==
'0' )
1393 Cube[0] &= ~Truth[v][0];
1394 Cube[1] &= ~Truth[v][1];
1396 else if ( pSop[
lit] !=
'-' )
1406 }
while ( pSop[
lit] );
1428 int nWords = nVars <= 6 ? 1 : 1 << (nVars-6);
1430 assert( nVars >= 0 && nVars <= 16 );
1431 assert( nVars == nInputs );
1432 for ( i = 0; i <
nWords; i++ )
1435 for ( i = 0; i <
nWords; i++ )
1436 pCube[i] = ~(
word)0;
1437 for ( v = 0; v < nVars; v++,
lit++ )
1439 if ( pSop[
lit] ==
'1' )
1441 for ( i = 0; i <
nWords; i++ )
1442 pCube[i] &= pVars[v][i];
1444 else if ( pSop[
lit] ==
'0' )
1446 for ( i = 0; i <
nWords; i++ )
1447 pCube[i] &= ~pVars[v][i];
1449 else if ( pSop[
lit] !=
'-' )
1452 for ( i = 0; i <
nWords; i++ )
1453 pRes[i] |= pCube[i];
1459 }
while ( pSop[
lit] );
1462 for ( i = 0; i <
nWords; i++ )
int Abc_SopIsOrType(char *pSop)
char * Abc_SopCreateFromTruth(Mem_Flex_t *pMan, int nVars, unsigned *pTruth)
char * Abc_SopCreateNand(Mem_Flex_t *pMan, int nVars)
char * Abc_SopCreateNor(Mem_Flex_t *pMan, int nVars)
int Abc_SopCheck(char *pSop, int nFanins)
int Abc_SopIsConst0(char *pSop)
int Abc_SopIsBuf(char *pSop)
int Abc_SopIsExorType(char *pSop)
Vec_Ptr_t * Abc_SopFromTruthsHex(char *pTruth)
char * Abc_SopStart(Mem_Flex_t *pMan, int nCubes, int nVars)
char * Abc_SopCreateOrMultiCube(Mem_Flex_t *pMan, int nVars, int *pfCompl)
int Abc_SopGetLitNum(char *pSop)
char * Abc_SopCreateConst1(Mem_Flex_t *pMan)
int Abc_SopGetCubeNum(char *pSop)
char * Abc_SopFromTruthHex(char *pTruth)
char * Abc_SopCreateXor(Mem_Flex_t *pMan, int nVars)
int Abc_SopGetIthCareLit(char *pSop, int i)
char * Abc_SopCreateMux(Mem_Flex_t *pMan)
char * Abc_SopCreateNxor(Mem_Flex_t *pMan, int nVars)
int Abc_SopIsAndType(char *pSop)
char * Abc_SopCreateOr(Mem_Flex_t *pMan, int nVars, int *pfCompl)
int Abc_SopIsComplement(char *pSop)
int Abc_SopIsConst1(char *pSop)
int Abc_SopIsInv(char *pSop)
Vec_Ptr_t * Abc_SopFromTruthsBin(char *pTruth)
char * Abc_SopCreateAnd2(Mem_Flex_t *pMan, int fCompl0, int fCompl1)
ABC_NAMESPACE_IMPL_START char * Abc_SopRegister(Mem_Flex_t *pMan, const char *pName)
DECLARATIONS ///.
char * Abc_SopDecoderPos(Mem_Flex_t *pMan, int nValues)
char * Abc_SopCreateAnd(Mem_Flex_t *pMan, int nVars, int *pfCompl)
int Abc_SopGetPhase(char *pSop)
char * Abc_SopEncoderPos(Mem_Flex_t *pMan, int iValue, int nValues)
word Abc_SopToTruth(char *pSop, int nInputs)
char * Abc_SopCreateXorSpecial(Mem_Flex_t *pMan, int nVars)
void Abc_SopComplement(char *pSop)
void Abc_SopToIsop(char *pSop, Vec_Int_t *vCover)
int Abc_SopCheckReadTruth(Vec_Ptr_t *vRes, char *pToken, int fHex)
char * Abc_SopDecoderLog(Mem_Flex_t *pMan, int nValues)
Vec_Ptr_t * Abc_SopGenerateCounters(int nVars)
void Abc_SopComplementVar(char *pSop, int iVar)
char * Abc_SopCreateFromTruthIsop(Mem_Flex_t *pMan, int nVars, word *pTruth, Vec_Int_t *vCover)
char * Abc_SopCreateFromIsop(Mem_Flex_t *pMan, int nVars, Vec_Int_t *vCover)
char * Abc_SopCreateConst0(Mem_Flex_t *pMan)
char * Abc_SopEncoderLog(Mem_Flex_t *pMan, int iBit, int nValues)
void Abc_SopToTruthBig(char *pSop, int nInputs, word **pVars, word *pCube, word *pRes)
char * Abc_SopCreateBuf(Mem_Flex_t *pMan)
void Abc_SopToTruth7(char *pSop, int nInputs, word r[2])
char * Abc_SopCreateInv(Mem_Flex_t *pMan)
int Abc_SopGetVarNum(char *pSop)
char * Abc_SopFromTruthBin(char *pTruth)
#define Abc_SopForEachCube(pSop, nFanins, pCube)
#define ABC_ALLOC(type, num)
#define ABC_CONST(number)
PARAMETERS ///.
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
struct Vec_Str_t_ Vec_Str_t
unsigned __int64 word
DECLARATIONS ///.
int Kit_TruthIsop(unsigned *puTruth, int nVars, Vec_Int_t *vMemory, int fTryBoth)
char * Mem_FlexEntryFetch(Mem_Flex_t *p, int nBytes)
struct Mem_Flex_t_ Mem_Flex_t
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.