From 3aff0af0c544ac8fda316a4095a6e201652097da Mon Sep 17 00:00:00 2001 From: Alan Mishchenko Date: Mon, 11 Nov 2024 21:02:59 -0800 Subject: [PATCH] Adding command for generating sorters. --- src/aig/gia/giaGen.c | 59 ++++++++++++++++++++++++++++++++++++++++ src/base/abci/abc.c | 64 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/src/aig/gia/giaGen.c b/src/aig/gia/giaGen.c index 03b194e7c..a807fe60f 100644 --- a/src/aig/gia/giaGen.c +++ b/src/aig/gia/giaGen.c @@ -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 /// //////////////////////////////////////////////////////////////////////// diff --git a/src/base/abci/abc.c b/src/base/abci/abc.c index 8e87106b2..0140edf51 100644 --- a/src/base/abci/abc.c +++ b/src/base/abci/abc.c @@ -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 ); @@ -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 ); @@ -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 ] [-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; }