Skip to content

Commit

Permalink
Adding command for generating sorters.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanminko committed Nov 12, 2024
1 parent b5a76d8 commit 3aff0af
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/aig/gia/giaGen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,65 @@ Gia_Man_t * Gia_ManGenMux( int nIns, char * pNums )
return p;
}


/**Function*************************************************************
Synopsis [Generates N-bit sorter using pair-wise sorting algorithm.]
Description [https://en.wikipedia.org/wiki/Pairwise_sorting_network]
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Gia_ManGenSorterOne( Gia_Man_t * p, int * pLits, int i, int k )
{
int Lit1 = Gia_ManAppendAnd( p, pLits[i], pLits[k] );
int Lit2 = Gia_ManAppendOr ( p, pLits[i], pLits[k] );
pLits[i] = Lit1;
pLits[k] = Lit2;
}
static inline void Gia_ManGenSorterConstrMerge( Gia_Man_t * p, int * pLits, int lo, int hi, int r )
{
int i, step = r * 2;
if ( step < hi - lo )
{
Gia_ManGenSorterConstrMerge( p, pLits, lo, hi-r, step );
Gia_ManGenSorterConstrMerge( p, pLits, lo+r, hi, step );
for ( i = lo+r; i < hi-r; i += step )
Gia_ManGenSorterOne( p, pLits, i, i+r );
}
}
static inline void Gia_ManGenSorterConstrRange( Gia_Man_t * p, int * pLits, int lo, int hi )
{
if ( hi - lo >= 1 )
{
int i, mid = lo + (hi - lo) / 2;
for ( i = lo; i <= mid; i++ )
Gia_ManGenSorterOne( p, pLits, i, i + (hi - lo + 1) / 2 );
Gia_ManGenSorterConstrRange( p, pLits, lo, mid );
Gia_ManGenSorterConstrRange( p, pLits, mid+1, hi );
Gia_ManGenSorterConstrMerge( p, pLits, lo, hi, 1 );
}
}
Gia_Man_t * Gia_ManGenSorter( int LogN )
{
int i, nVars = 1 << LogN;
int nVarsAlloc = nVars + 2 * (nVars * LogN * (LogN-1) / 4 + nVars - 1);
Vec_Int_t * vLits = Vec_IntAlloc( nVars );
Gia_Man_t * p = Gia_ManStart( 1 + 2*nVars + nVarsAlloc );
p->pName = Abc_UtilStrsav( "sorter" );
for ( i = 0; i < nVars; i++ )
Vec_IntPush( vLits, Gia_ManAppendCi(p) );
Gia_ManGenSorterConstrRange( p, Vec_IntArray(vLits), 0, nVars - 1 );
for ( i = 0; i < nVars; i++ )
Gia_ManAppendCo( p, Vec_IntEntry(vLits, i) );
Vec_IntFree( vLits );
return p;
}


////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
Expand Down
64 changes: 63 additions & 1 deletion src/base/abci/abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ static int Abc_CommandAbc9Odc ( Abc_Frame_t * pAbc, int argc, cha
static int Abc_CommandAbc9GenRel ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandAbc9GenMux ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandAbc9GenComp ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandAbc9GenSorter ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandAbc9GenNeuron ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandAbc9Window ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandAbc9FunAbs ( Abc_Frame_t * pAbc, int argc, char ** argv );
Expand Down Expand Up @@ -1420,6 +1421,7 @@ void Abc_Init( Abc_Frame_t * pAbc )
Cmd_CommandAdd( pAbc, "ABC9", "&genrel", Abc_CommandAbc9GenRel, 0 );
Cmd_CommandAdd( pAbc, "ABC9", "&genmux", Abc_CommandAbc9GenMux, 0 );
Cmd_CommandAdd( pAbc, "ABC9", "&gencomp", Abc_CommandAbc9GenComp, 0 );
Cmd_CommandAdd( pAbc, "ABC9", "&gensorter", Abc_CommandAbc9GenSorter, 0 );
Cmd_CommandAdd( pAbc, "ABC9", "&genneuron", Abc_CommandAbc9GenNeuron, 0 );
Cmd_CommandAdd( pAbc, "ABC9", "&window", Abc_CommandAbc9Window, 0 );
Cmd_CommandAdd( pAbc, "ABC9", "&funabs", Abc_CommandAbc9FunAbs, 0 );
Expand Down Expand Up @@ -54083,7 +54085,67 @@ int Abc_CommandAbc9GenComp( Abc_Frame_t * pAbc, int argc, char ** argv )
Abc_Print( -2, "\t-i : toggles using interleaved variable ordering [default = %s]\n", fInter ? "yes": "no" );
Abc_Print( -2, "\t-v : toggles printing verbose information [default = %s]\n", fVerbose ? "yes": "no" );
Abc_Print( -2, "\t-h : print the command usage\n");
Abc_Print( -2, "\tstring : the sizes of control input groups\n");
return 1;
}

/**Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

***********************************************************************/
int Abc_CommandAbc9GenSorter( Abc_Frame_t * pAbc, int argc, char ** argv )
{
extern Gia_Man_t * Gia_ManGenSorter( int LogN );
Gia_Man_t * pTemp = NULL;
int c, LogN = 0, fVerbose = 0;
Extra_UtilGetoptReset();
while ( ( c = Extra_UtilGetopt( argc, argv, "Kvh" ) ) != EOF )
{
switch ( c )
{
case 'K':
if ( globalUtilOptind >= argc )
{
Abc_Print( -1, "Command line switch \"-K\" should be followed by an integer.\n" );
goto usage;
}
LogN = atoi(argv[globalUtilOptind]);
globalUtilOptind++;
if ( LogN < 0 )
goto usage;
break;
case 'v':
fVerbose ^= 1;
break;
case 'h':
goto usage;
default:
goto usage;
}
}
if ( LogN == 0 )
{
Abc_Print( -1, "Abc_CommandAbc9GenComp(): The number of inputs should be defined on the command line \"-K num\".\n" );
return 0;
}
pTemp = Gia_ManGenSorter( LogN );
Abc_FrameUpdateGia( pAbc, pTemp );
if ( fVerbose )
Abc_Print( 1, "Generated %d-input sorter composed of %d elementary 2-bit sorters.\n", 1 << LogN, (1 << LogN) * LogN * (LogN-1) / 4 + (1 << LogN) - 1 );
return 0;

usage:
Abc_Print( -2, "usage: &gensorter [-K <num>] [-vh]\n" );
Abc_Print( -2, "\t generates the sorter using pair-wise sorting algorithm\n" );
Abc_Print( -2, "\t-K num : the base-2 log of the number of inputs [default = undefined]\n" );
Abc_Print( -2, "\t-v : toggles printing verbose information [default = %s]\n", fVerbose ? "yes": "no" );
Abc_Print( -2, "\t-h : print the command usage\n");
return 1;
}

Expand Down

0 comments on commit 3aff0af

Please sign in to comment.