Skip to content

Commit 28f4ad8

Browse files
committed
New command &symfun.
1 parent 9e90e60 commit 28f4ad8

File tree

3 files changed

+214
-10
lines changed

3 files changed

+214
-10
lines changed

src/aig/gia/giaLutCas.c

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,65 @@ ABC_NAMESPACE_IMPL_START
3434
/// FUNCTION DEFINITIONS ///
3535
////////////////////////////////////////////////////////////////////////
3636

37+
/**Function*************************************************************
38+
39+
Synopsis []
40+
41+
Description []
42+
43+
SideEffects []
44+
45+
SeeAlso []
46+
47+
***********************************************************************/
48+
int Gia_ManGenSymFun_rec( Gia_Man_t * p, word Str, int nChars, Vec_Ptr_t * vStrs, Vec_Wec_t * vLits, Vec_Int_t * vIns )
49+
{
50+
if ( Str == 0 ) return 0;
51+
if ( Str == Abc_Tt6Mask(nChars) ) return 1;
52+
assert( nChars > 1 );
53+
Vec_Wrd_t * vStore = (Vec_Wrd_t *)Vec_PtrEntry(vStrs, nChars-1);
54+
Vec_Int_t * vValue = Vec_WecEntry(vLits, nChars-1);
55+
int Index;
56+
if ( (Index = Vec_WrdFind(vStore, Str)) >= 0 )
57+
return Vec_IntEntry(vValue, Index);
58+
word Str0 = Str & ~Abc_Tt6MaskI(nChars-1);
59+
word Str1 = Str >> 1;
60+
int Lit0 = Gia_ManGenSymFun_rec( p, Str0, nChars-1, vStrs, vLits, vIns );
61+
int Lit1 = Gia_ManGenSymFun_rec( p, Str1, nChars-1, vStrs, vLits, vIns );
62+
int Lit = Gia_ManAppendMux2( p, Vec_IntEntry(vIns, nChars-2), Lit1, Lit0 );
63+
Vec_WrdPush( vStore, Str );
64+
Vec_WrdPush( vStore, ~Str & Abc_Tt6Mask(nChars) );
65+
Vec_IntPush( vValue, Lit );
66+
Vec_IntPush( vValue, Abc_LitNot(Lit) );
67+
return Lit;
68+
}
69+
Gia_Man_t * Gia_ManGenSymFun( Vec_Wrd_t * vFuns, int nChars, int fVerbose )
70+
{
71+
assert( nChars <= 64 );
72+
word Str; int i;
73+
Vec_Ptr_t * vStrs = Vec_PtrAlloc(nChars);
74+
for ( i = 0; i < nChars; i++ )
75+
Vec_PtrPush( vStrs, Vec_WrdAlloc(0) );
76+
Vec_Wec_t * vLits = Vec_WecStart(nChars);
77+
Vec_Int_t * vOuts = Vec_IntAlloc(Vec_WrdSize(vFuns));
78+
Gia_Man_t * pNew = Gia_ManStart( 10000 );
79+
pNew->pName = Abc_UtilStrsav( "sym" );
80+
Vec_Int_t * vIns = Vec_IntAlloc(nChars-1);
81+
for ( i = 0; i < nChars-1; i++ )
82+
Vec_IntPush(vIns, Gia_ManAppendCi(pNew));
83+
Vec_WrdForEachEntry( vFuns, Str, i )
84+
Vec_IntPush( vOuts, Gia_ManGenSymFun_rec(pNew, Str, nChars, vStrs, vLits, vIns ) );
85+
Vec_WrdForEachEntry( vFuns, Str, i )
86+
Gia_ManAppendCo(pNew, Vec_IntEntry(vOuts,i) );
87+
for ( i = 0; i < nChars; i++ )
88+
Vec_WrdFree( (Vec_Wrd_t *)Vec_PtrEntry(vStrs, i) );
89+
Vec_PtrFree(vStrs);
90+
Vec_WecFree(vLits);
91+
Vec_IntFree(vOuts);
92+
Vec_IntFree(vIns);
93+
return pNew;
94+
}
95+
3796
/**Function*************************************************************
3897
3998
Synopsis []
@@ -101,15 +160,15 @@ char * Gia_LutCasPerm( int nVars, int nLuts, int LutSize )
101160
Gia_LutCasSort( pRes + i * LutSize, 1, LutSize-1 );
102161
return pRes;
103162
}
104-
int Gia_ManLutCasGen_rec( Gia_Man_t * pNew, Vec_Int_t * vCtrls, int iCtrl, Vec_Int_t * vDatas, int Shift )
163+
int Gia_ManGenLutCas_rec( Gia_Man_t * pNew, Vec_Int_t * vCtrls, int iCtrl, Vec_Int_t * vDatas, int Shift )
105164
{
106165
if ( iCtrl-- == 0 )
107166
return Vec_IntEntry( vDatas, Shift );
108-
int iLit0 = Gia_ManLutCasGen_rec( pNew, vCtrls, iCtrl, vDatas, Shift );
109-
int iLit1 = Gia_ManLutCasGen_rec( pNew, vCtrls, iCtrl, vDatas, Shift + (1<<iCtrl));
167+
int iLit0 = Gia_ManGenLutCas_rec( pNew, vCtrls, iCtrl, vDatas, Shift );
168+
int iLit1 = Gia_ManGenLutCas_rec( pNew, vCtrls, iCtrl, vDatas, Shift + (1<<iCtrl));
110169
return Gia_ManAppendMux( pNew, Vec_IntEntry(vCtrls, iCtrl), iLit1, iLit0 );
111170
}
112-
Gia_Man_t * Gia_ManLutCasGen( Gia_Man_t * p, char * pPermStr, int nVars, int nLuts, int LutSize, int Seed, int fVerbose )
171+
Gia_Man_t * Gia_ManGenLutCas( Gia_Man_t * p, char * pPermStr, int nVars, int nLuts, int LutSize, int Seed, int fVerbose )
113172
{
114173
if ( Seed )
115174
srand(Seed);
@@ -139,7 +198,7 @@ Gia_Man_t * Gia_ManLutCasGen( Gia_Man_t * p, char * pPermStr, int nVars, int nLu
139198
pCur++;
140199
for ( int k = 1; k < LutSize; k++ )
141200
Vec_IntWriteEntry( vLits, k, Vec_IntEntry(vCtrls, (int)(*pCur++ - 'a')) );
142-
Vec_IntWriteEntry( vLits, 0, Gia_ManLutCasGen_rec(pNew, vLits, LutSize, vDatas, i * (1 << LutSize)) );
201+
Vec_IntWriteEntry( vLits, 0, Gia_ManGenLutCas_rec(pNew, vLits, LutSize, vDatas, i * (1 << LutSize)) );
143202
}
144203
// if the AIG is given, create a miter
145204
int iLit = Vec_IntEntry(vLits, 0);
@@ -166,15 +225,15 @@ Gia_Man_t * Gia_ManLutCasGen( Gia_Man_t * p, char * pPermStr, int nVars, int nLu
166225
}
167226

168227
/*
169-
int Gia_ManLutCasGenSolve( int nVars, int nLuts, int LutSize, char * pTtStr, int fVerbose )
228+
int Gia_ManGenLutCasSolve( int nVars, int nLuts, int LutSize, char * pTtStr, int fVerbose )
170229
{
171230
extern Gia_Man_t * Gia_QbfQuantifyAll( Gia_Man_t * p, int nPars, int fAndAll, int fOrAll );
172231
assert( strlen(pTtStr) <= 1024 );
173232
word pTruth[64] = {0};
174233
int i, Id, nVars = Abc_TtReadHex( pTruth, pTtStr );
175234
assert( nVars <= 12 );
176235
int nParams = nLuts * (1 << LutSize);
177-
Gia_Man_t * pCas = Gia_ManLutCasGen( NULL, NULL, nVars, nLuts, LutSize, 0, fVerbose );
236+
Gia_Man_t * pCas = Gia_ManGenLutCas( NULL, NULL, nVars, nLuts, LutSize, 0, fVerbose );
178237
Gia_Man_t * pCofs = Gia_QbfQuantifyAll( pCas, nParams, 0, 0 );
179238
Gia_ManFree( pCas );
180239
Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( pCofs, 8, 0, 0, 0, 0 );

src/base/abci/abc.c

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ static int Abc_CommandAbc9Nf ( Abc_Frame_t * pAbc, int argc, cha
519519
static int Abc_CommandAbc9Of ( Abc_Frame_t * pAbc, int argc, char ** argv );
520520
static int Abc_CommandAbc9Simap ( Abc_Frame_t * pAbc, int argc, char ** argv );
521521
static int Abc_CommandAbc9Exmap ( Abc_Frame_t * pAbc, int argc, char ** argv );
522+
static int Abc_CommandAbc9SymFun ( Abc_Frame_t * pAbc, int argc, char ** argv );
522523
static int Abc_CommandAbc9Pack ( Abc_Frame_t * pAbc, int argc, char ** argv );
523524
static int Abc_CommandAbc9Edge ( Abc_Frame_t * pAbc, int argc, char ** argv );
524525
static int Abc_CommandAbc9SatLut ( Abc_Frame_t * pAbc, int argc, char ** argv );
@@ -1342,6 +1343,7 @@ void Abc_Init( Abc_Frame_t * pAbc )
13421343
Cmd_CommandAdd( pAbc, "ABC9", "&of", Abc_CommandAbc9Of, 0 );
13431344
Cmd_CommandAdd( pAbc, "ABC9", "&simap", Abc_CommandAbc9Simap, 0 );
13441345
Cmd_CommandAdd( pAbc, "ABC9", "&exmap", Abc_CommandAbc9Exmap, 0 );
1346+
Cmd_CommandAdd( pAbc, "ABC9", "&symfun", Abc_CommandAbc9SymFun, 0 );
13451347
Cmd_CommandAdd( pAbc, "ABC9", "&pack", Abc_CommandAbc9Pack, 0 );
13461348
Cmd_CommandAdd( pAbc, "ABC9", "&edge", Abc_CommandAbc9Edge, 0 );
13471349
Cmd_CommandAdd( pAbc, "ABC9", "&satlut", Abc_CommandAbc9SatLut, 0 );
@@ -45517,6 +45519,111 @@ int Abc_CommandAbc9Exmap( Abc_Frame_t * pAbc, int argc, char ** argv )
4551745519
return 1;
4551845520
}
4551945521

45522+
/**Function*************************************************************
45523+
45524+
Synopsis []
45525+
45526+
Description []
45527+
45528+
SideEffects []
45529+
45530+
SeeAlso []
45531+
45532+
***********************************************************************/
45533+
int Abc_CommandAbc9SymFun( Abc_Frame_t * pAbc, int argc, char ** argv )
45534+
{
45535+
extern Gia_Man_t * Gia_ManGenSymFun( Vec_Wrd_t * vFuns, int nChars, int fVerbose );
45536+
Gia_Man_t * pNew = NULL;
45537+
Vec_Wrd_t * vFuns = NULL;
45538+
int c, nChars = 0, nMaj = 0, nHot = 0, nXor = 0, fVerbose = 0;
45539+
Extra_UtilGetoptReset();
45540+
while ( ( c = Extra_UtilGetopt( argc, argv, "MHXvh" ) ) != EOF )
45541+
{
45542+
switch ( c )
45543+
{
45544+
case 'M':
45545+
if ( globalUtilOptind >= argc )
45546+
{
45547+
Abc_Print( -1, "Command line switch \"-M\" should be followed by a file name.\n" );
45548+
goto usage;
45549+
}
45550+
nMaj = atoi(argv[globalUtilOptind++]);
45551+
break;
45552+
case 'H':
45553+
if ( globalUtilOptind >= argc )
45554+
{
45555+
Abc_Print( -1, "Command line switch \"-H\" should be followed by a file name.\n" );
45556+
goto usage;
45557+
}
45558+
nHot = atoi(argv[globalUtilOptind++]);
45559+
break;
45560+
case 'X':
45561+
if ( globalUtilOptind >= argc )
45562+
{
45563+
Abc_Print( -1, "Command line switch \"-X\" should be followed by a file name.\n" );
45564+
goto usage;
45565+
}
45566+
nXor = atoi(argv[globalUtilOptind++]);
45567+
break;
45568+
case 'v':
45569+
fVerbose ^= 1;
45570+
break;
45571+
case 'h':
45572+
default:
45573+
goto usage;
45574+
}
45575+
}
45576+
if ( nMaj ) {
45577+
nChars = nMaj+1;
45578+
vFuns = Vec_WrdAlloc(1);
45579+
Vec_WrdPush( vFuns, Abc_Tt6Mask(nMaj/2+1) << (nMaj/2+1) );
45580+
}
45581+
else if ( nHot ) {
45582+
nChars = nHot+1;
45583+
vFuns = Vec_WrdAlloc(1);
45584+
Vec_WrdPush( vFuns, 2 );
45585+
}
45586+
else if ( nXor ) {
45587+
nChars = nXor+1;
45588+
vFuns = Vec_WrdAlloc(1);
45589+
Vec_WrdPush( vFuns, ABC_CONST(0xAAAAAAAAAAAAAAAA) & Abc_Tt6Mask(nXor+1) );
45590+
}
45591+
else {
45592+
if ( argc == globalUtilOptind ) {
45593+
printf( "One or more characteristic strings should be given on the command line.\n" );
45594+
return 0;
45595+
}
45596+
nChars = (int)strlen(argv[globalUtilOptind]);
45597+
for ( c = globalUtilOptind+1; c < argc; c++ )
45598+
if ( nChars != (int)strlen(argv[c]) ) {
45599+
printf( "Char strings have different lengths. Quitting.\n" );
45600+
return 0;
45601+
}
45602+
vFuns = Vec_WrdAlloc( argc );
45603+
for ( c = globalUtilOptind; c < argc; c++ )
45604+
Vec_WrdPush( vFuns, Abc_TtReadBin64(argv[c]) );
45605+
}
45606+
if ( nChars > 64 ) {
45607+
printf( "Currently can handle functions up to 63 inputs. Quitting.\n" );
45608+
return 0;
45609+
}
45610+
pNew = Gia_ManGenSymFun( vFuns, nChars, fVerbose );
45611+
Abc_FrameUpdateGia( pAbc, pNew );
45612+
Vec_WrdFree( vFuns );
45613+
return 0;
45614+
45615+
usage:
45616+
Abc_Print( -2, "usage: &symfun [-MHX num] [-vh] <str0> <str1> ... <str(N-1)>\n" );
45617+
Abc_Print( -2, "\t derives AIG of a multi-output symmetric function\n" );
45618+
Abc_Print( -2, "\t-M <num> : generate the majority gate with the given input count [default = unused]\n" );
45619+
Abc_Print( -2, "\t-H <num> : generate the 1-hot condition with the given input count [default = unused]\n" );
45620+
Abc_Print( -2, "\t-X <num> : generate the xor-gate with the given input count [default = unused]\n" );
45621+
Abc_Print( -2, "\t-v : toggles verbose output [default = %s]\n", fVerbose? "yes": "no" );
45622+
Abc_Print( -2, "\t-h : prints the command usage\n");
45623+
Abc_Print( -2, "\t<str0> <str1> ... <str(N-1)> : char strings in binary notation LSB first\n");
45624+
return 1;
45625+
}
45626+
4552045627
/**Function*************************************************************
4552145628

4552245629
Synopsis []
@@ -51726,7 +51833,7 @@ int Abc_CommandAbc9GenQbf( Abc_Frame_t * pAbc, int argc, char ** argv )
5172651833
***********************************************************************/
5172751834
int Abc_CommandAbc9GenLutCas( Abc_Frame_t * pAbc, int argc, char ** argv )
5172851835
{
51729-
extern Gia_Man_t * Gia_ManLutCasGen( Gia_Man_t * p, char * pPermStr, int nVars, int nLuts, int LutSize, int Seed, int fVerbose );
51836+
extern Gia_Man_t * Gia_ManGenLutCas( Gia_Man_t * p, char * pPermStr, int nVars, int nLuts, int LutSize, int Seed, int fVerbose );
5173051837
int nVars = 0;
5173151838
int nLuts = 2;
5173251839
int LutSize = 6;
@@ -51840,7 +51947,7 @@ int Abc_CommandAbc9GenLutCas( Abc_Frame_t * pAbc, int argc, char ** argv )
5184051947
Abc_Print( -1, "Function with %d variables is too large for a cascade composed of %d connected %d-LUTs.\n", nVars, nLuts, LutSize );
5184151948
return 1;
5184251949
}
51843-
pTemp = Gia_ManLutCasGen( pAbc->pGia, pPermStr, nVars, nLuts, LutSize, Seed, fVerbose );
51950+
pTemp = Gia_ManGenLutCas( pAbc->pGia, pPermStr, nVars, nLuts, LutSize, Seed, fVerbose );
5184451951
Abc_FrameUpdateGia( pAbc, pTemp );
5184551952
return 0;
5184651953

src/misc/util/utilTruth.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ static inline int Abc_TtHexDigitNum( int nVars ) { return nVars <= 2 ? 1 : 1 <<
230230
SeeAlso []
231231
232232
***********************************************************************/
233-
static inline word Abc_Tt6Mask( int nBits ) { assert( nBits >= 0 && nBits <= 64 ); return (~(word)0) >> (64-nBits); }
233+
static inline word Abc_Tt6MaskI( int iBit ) { assert( iBit >= 0 && iBit <= 64 ); return ((word)1) << iBit; }
234+
static inline word Abc_Tt6Mask( int nBits ) { assert( nBits >= 0 && nBits <= 64 ); return (~(word)0) >> (64-nBits); }
234235
static inline void Abc_TtMask( word * pTruth, int nWords, int nBits )
235236
{
236237
int w;
@@ -1579,6 +1580,43 @@ static inline int Abc_TtReadHexNumber( word * pTruth, char * pString )
15791580
}
15801581

15811582

1583+
/**Function*************************************************************
1584+
1585+
Synopsis [Reads the integer number as a binary string.]
1586+
1587+
Description []
1588+
1589+
SideEffects []
1590+
1591+
SeeAlso []
1592+
1593+
***********************************************************************/
1594+
static inline int Abc_TtReadBin( word * pWords, int nWords, char * pString )
1595+
{
1596+
int i, Len = (int)strlen(pString), nWords2 = (Len+63)/64;
1597+
assert( nWords2 <= nWords );
1598+
memset( pWords, 0, sizeof(word)*nWords );
1599+
for ( i = 0; i < Len; i++ )
1600+
if ( pString[i] == '1' )
1601+
Abc_TtSetBit(pWords, i);
1602+
else if ( pString[i] != '0' )
1603+
return 0;
1604+
return 1;
1605+
}
1606+
static inline word Abc_TtReadBin64( char * pString )
1607+
{
1608+
word Word = 0;
1609+
int Len = (int)strlen(pString);
1610+
assert( Len <= 64 );
1611+
int Res = Abc_TtReadBin( &Word, 1, pString );
1612+
if ( Res == 0 ) {
1613+
printf( "Reading binary string \"%s\" has failed.\n", pString );
1614+
Word = ~(word)0;
1615+
}
1616+
return Word;
1617+
}
1618+
1619+
15821620
/**Function*************************************************************
15831621
15841622
Synopsis []

0 commit comments

Comments
 (0)