Adding counter generation to "symfun".

This commit is contained in:
Alan Mishchenko 2025-03-29 11:11:13 -07:00
parent 938ae9428b
commit 2442720528
2 changed files with 52 additions and 3 deletions

View File

@ -21,6 +21,11 @@
#include "abc.h"
#include "bool/kit/kit.h"
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif
ABC_NAMESPACE_IMPL_START
@ -1135,6 +1140,31 @@ Vec_Ptr_t * Abc_SopFromTruthsHex( char * pTruth )
return vRes;
}
Vec_Ptr_t * Abc_SopGenerateCounters( int nVars )
{
int m, i, o, nOuts = Abc_Base2Log( nVars + 1 );
Vec_Ptr_t * vRes = Vec_PtrAlloc( nOuts );
for ( o = 0; o < nOuts; o++ )
{
Vec_Str_t * vStr = Vec_StrAlloc( 1000 );
for ( m = 0; m < (1 << nVars); m++ ) {
int nOnes = __builtin_popcount(m);
if ( !((nOnes >> o) & 1) )
continue;
for ( i = 0; i < nVars; i++ )
Vec_StrPush( vStr, ((m >> i) & 1) ? '1' : '0' );
Vec_StrPush( vStr, ' ' );
Vec_StrPush( vStr, '1' );
Vec_StrPush( vStr, '\n' );
}
Vec_StrPush( vStr, '\0' );
//printf( "%s\n", Vec_StrArray(vStr) );
Vec_PtrPush( vRes, Vec_StrReleaseArray(vStr) );
Vec_StrFree( vStr );
}
return vRes;
}
/**Function*************************************************************
Synopsis [Creates one encoder node.]

View File

@ -25966,9 +25966,9 @@ int Abc_CommandSymFun( Abc_Frame_t * pAbc, int argc, char ** argv )
extern void Ntk_SymFunGenerate( int nVars, int fVerbose );
word * pFun = NULL;
char * pStr, * pTruth, * pCommand;
int c, k, nVars = -1, fVerbose = 0;
int c, k, nVars = -1, fCounter = 0, fVerbose = 0;
Extra_UtilGetoptReset();
while ( ( c = Extra_UtilGetopt( argc, argv, "Nvh" ) ) != EOF )
while ( ( c = Extra_UtilGetopt( argc, argv, "Ncvh" ) ) != EOF )
{
switch ( c )
{
@ -25981,6 +25981,9 @@ int Abc_CommandSymFun( Abc_Frame_t * pAbc, int argc, char ** argv )
nVars = atoi(argv[globalUtilOptind]);
globalUtilOptind++;
break;
case 'c':
fCounter ^= 1;
break;
case 'v':
fVerbose ^= 1;
break;
@ -25991,6 +25994,21 @@ int Abc_CommandSymFun( Abc_Frame_t * pAbc, int argc, char ** argv )
goto usage;
}
}
if ( fCounter )
{
if ( nVars == -1 )
{
printf( "The number of variables should be specified on the command line using \"-N <num>\".\n" );
return 1;
}
extern Vec_Ptr_t * Abc_SopGenerateCounters( int nVars );
Vec_Ptr_t * vSops = Abc_SopGenerateCounters( nVars );
Abc_Ntk_t * pNtk = Abc_NtkCreateWithNodes( vSops );
Vec_PtrFreeFree( vSops );
Abc_FrameReplaceCurrentNetwork( pAbc, pNtk );
Abc_FrameClearVerifStatus( pAbc );
return 0;
}
if ( nVars != -1 )
{
if ( nVars < 1 || nVars > 16 )
@ -26040,9 +26058,10 @@ int Abc_CommandSymFun( Abc_Frame_t * pAbc, int argc, char ** argv )
return 0;
usage:
Abc_Print( -2, "usage: symfun [-N num] [-vh] <ones>\n" );
Abc_Print( -2, "usage: symfun [-N num] [-cvh] <ones>\n" );
Abc_Print( -2, "\t generated a single-output symmetric function\n" );
Abc_Print( -2, "\t-N <num> : prints truth tables of all N-var symmetric functions [default = not used]\n" );
Abc_Print( -2, "\t-c : toggle generating a counter [default = %s]\n", fCounter? "yes": "no" );
Abc_Print( -2, "\t-v : toggle verbose output [default = %s]\n", fVerbose? "yes": "no" );
Abc_Print( -2, "\t-h : print the command usage\n");
Abc_Print( -2, "\t<ones> : the string of N+1 zeros and ones, where N is the number of variables\n" );