From fe3b5bf5fee78411a1ed89053dae97c2c2a8b3a0 Mon Sep 17 00:00:00 2001 From: coastalwhite Date: Wed, 24 Jul 2024 21:46:03 +0200 Subject: [PATCH 1/2] chore: `__builtin_popcount` to replace BitCount8 This replaces all occurrences of the `BitCount8` static array with the `__builtin_popcount`. It is a rather simple and small PR. Fixes #308. --- src/aig/gia/giaNf.c | 2 +- src/base/exor/exorBits.c | 14 +------------- src/bdd/cas/casCore.c | 12 ------------ src/map/if/ifDec07.c | 19 ++++--------------- src/map/if/ifDec08.c | 13 +------------ src/map/if/ifDec10.c | 13 +------------ src/misc/util/utilTruth.h | 13 +------------ src/opt/dau/dauCanon.c | 4 ++-- 8 files changed, 11 insertions(+), 79 deletions(-) diff --git a/src/aig/gia/giaNf.c b/src/aig/gia/giaNf.c index bfc677b726..10ce25c5b1 100644 --- a/src/aig/gia/giaNf.c +++ b/src/aig/gia/giaNf.c @@ -225,7 +225,7 @@ void Nf_StoCreateGateAdd( Vec_Mem_t * vTtMem, Vec_Wec_t * vTt2Match, Mio_Cell2_t if ( fPinQuick ) // reduce the number of matches agressively { Vec_IntForEachEntryDouble( vArray, GateId, Entry, i ) - if ( GateId == (int)pCell->Id && Abc_TtBitCount8[Nf_Int2Cfg(Entry).Phase] == Abc_TtBitCount8[Mat.Phase] ) + if ( GateId == (int)pCell->Id && __builtin_popcount( Nf_Int2Cfg(Entry).Phase & 0xff ) == __builtin_popcount( Mat.Phase & 0xff ) ) return; } else // reduce the number of matches less agressively diff --git a/src/base/exor/exorBits.c b/src/base/exor/exorBits.c index 41560e6504..7731a8c3b4 100644 --- a/src/base/exor/exorBits.c +++ b/src/base/exor/exorBits.c @@ -80,18 +80,6 @@ void ExorVar( Cube * pC, int Var, varvalue Val ); /// STATIC VARIABLES /// //////////////////////////////////////////////////////////////////////// -// the bit count for the first 256 integer numbers -static unsigned char BitCount8[256] = { - 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 -}; - static int SparseNumbers[163] = { 0,1,4,5,16,17,20,21,64,65,68,69,80,81,84,85,256,257,260, 261,272,273,276,277,320,321,324,325,336,337,340,1024,1025, @@ -158,7 +146,7 @@ void PrepareBitSetModule() nLimit = FULL16BITS; for ( i = 0; i < nLimit; i++ ) { - BitCount[i] = BitCount8[ i & 0xff ] + BitCount8[ i>>8 ]; + BitCount[i] = __builtin_popcount( i & 0xffff ); BitGroupNumbers[i] = MARKNUMBER; } // prepare bit groups diff --git a/src/bdd/cas/casCore.c b/src/bdd/cas/casCore.c index 21c21d2004..23d7be521d 100644 --- a/src/bdd/cas/casCore.c +++ b/src/bdd/cas/casCore.c @@ -460,18 +460,6 @@ void Experiment2( BFunc * pFunc ) /// SINGLE OUTPUT FUNCTION /// //////////////////////////////////////////////////////////////////////// -// the bit count for the first 256 integer numbers -//static unsigned char BitCount8[256] = { -// 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, -// 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, -// 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, -// 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, -// 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, -// 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, -// 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, -// 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 -//}; - ///////////////////////////////////////////////////////////// static int s_SuppSize[MAXOUTPUTS]; int CompareSupports( int *ptrX, int *ptrY ) diff --git a/src/map/if/ifDec07.c b/src/map/if/ifDec07.c index b8851e5007..6311df6d1e 100644 --- a/src/map/if/ifDec07.c +++ b/src/map/if/ifDec07.c @@ -29,17 +29,6 @@ ABC_NAMESPACE_IMPL_START /// DECLARATIONS /// //////////////////////////////////////////////////////////////////////// -// the bit count for the first 256 integer numbers -static int BitCount8[256] = { - 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 -}; // variable swapping code static word PMasks[5][3] = { { ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) }, @@ -208,7 +197,7 @@ static inline int If_Dec6CofCount2( word t ) int i, Mask = 0; for ( i = 0; i < 16; i++ ) Mask |= (1 << ((t >> (i<<2)) & 15)); - return BitCount8[((unsigned char*)&Mask)[0]] + BitCount8[((unsigned char*)&Mask)[1]]; + return __builtin_popcount( Mask & 0xffff ); } // count the number of unique cofactors (up to 3) static inline int If_Dec7CofCount3( word t[2] ) @@ -696,7 +685,7 @@ static inline word If_Dec5CofCount2( word t, int x, int y, int * Pla2Var, word t for ( Mask = i = 0; i < 16; i++ ) if ( ((i >> x) & 1) == ((m >> 0) & 1) && ((i >> y) & 1) == ((m >> 1) & 1) ) Mask |= (1 << ((t >> (i<<1)) & 3)); - if ( BitCount8[Mask & 0xF] > 2 ) + if ( __builtin_popcount( Mask & 0xF ) > 2 ) return 0; } // Kit_DsdPrintFromTruth( (unsigned *)&t, 5 ); printf( "\n" ); @@ -726,12 +715,12 @@ static inline word If_Dec5CofCount2( word t, int x, int y, int * Pla2Var, word t if ( ((i >> x) & 1) == ((m >> 0) & 1) && ((i >> y) & 1) == ((m >> 1) & 1) ) Mask |= (1 << ((t >> (i<<1)) & 3)); // find the values - if ( BitCount8[Mask & 0xF] == 1 ) + if ( __builtin_popcount( Mask & 0xF ) == 1 ) { C2[m] = F[Abc_Tt6FirstBit( Mask )]; D2[m] = ~(word)0; } - else if ( BitCount8[Mask & 0xF] == 2 ) + else if ( __builtin_popcount( Mask & 0xF ) == 2 ) { int Bit0 = Abc_Tt6FirstBit( Mask ); int Bit1 = Abc_Tt6FirstBit( Mask ^ (((word)1)<> 8) & 255]; + return __builtin_popcount( Num16 & 0xffff ); } static inline void If_DecVerifyPerm( int Pla2Var[10], int Var2Pla[10], int nVars ) { diff --git a/src/map/if/ifDec10.c b/src/map/if/ifDec10.c index 00904a182f..0c3cf5e545 100644 --- a/src/map/if/ifDec10.c +++ b/src/map/if/ifDec10.c @@ -29,17 +29,6 @@ ABC_NAMESPACE_IMPL_START /// DECLARATIONS /// //////////////////////////////////////////////////////////////////////// -// the bit count for the first 256 integer numbers -static int BitCount8[256] = { - 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 -}; // variable swapping code static word PMasks[5][3] = { { ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) }, @@ -371,7 +360,7 @@ void If_Dec10Cofactors( word * pF, int nVars, int iVar, word * pCof0, word * pCo static inline int If_Dec10Count16( int Num16 ) { assert( Num16 < (1<<16)-1 ); - return BitCount8[Num16 & 255] + BitCount8[(Num16 >> 8) & 255]; + return __builtin_popcount( Num16 & 0xffff ); } static inline void If_DecVerifyPerm( int Pla2Var[10], int Var2Pla[10], int nVars ) { diff --git a/src/misc/util/utilTruth.h b/src/misc/util/utilTruth.h index 2624f94639..86fe29a743 100644 --- a/src/misc/util/utilTruth.h +++ b/src/misc/util/utilTruth.h @@ -129,18 +129,7 @@ static word s_PPMasks[5][6][3] = { } }; -// the bit count for the first 256 integer numbers -static int Abc_TtBitCount8[256] = { - 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, - 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 -}; -static inline int Abc_TtBitCount16( int i ) { return Abc_TtBitCount8[i & 0xFF] + Abc_TtBitCount8[i >> 8]; } +static inline int Abc_TtBitCount16( int i ) { return __builtin_popcount( i & 0xffff ); } //////////////////////////////////////////////////////////////////////// /// MACRO DEFINITIONS /// diff --git a/src/opt/dau/dauCanon.c b/src/opt/dau/dauCanon.c index dd954d9a7b..0636024674 100644 --- a/src/opt/dau/dauCanon.c +++ b/src/opt/dau/dauCanon.c @@ -380,7 +380,7 @@ static int Abc_TtScc6(word wTruth, int ck) if (!wTruth) return 0; for (i = 0; i < 64; i++) if (wTruth & (word)1 << i) { - int ci = Abc_TtBitCount8[i] + ck; + int ci = __builtin_popcount( i & 0xff ) + ck; sum += shiftFunc(ci); } return sum; @@ -406,7 +406,7 @@ static inline void Abc_TtSccInCofs6(word wTruth, int nVars, int ck, int * pStore { if (wTruth & (word)1 << j) { - int ci = Abc_TtBitCount8[i] + ck; + int ci = __builtin_popcount( i & 0xff ) + ck; sum += shiftFunc(ci); } i++; From 849adb2fcbe91872eeb2d31c534a39d9908ab144 Mon Sep 17 00:00:00 2001 From: coastalwhite Date: Fri, 26 Jul 2024 21:48:46 +0200 Subject: [PATCH 2/2] chore: add `__builtin_popcount` for MSVC --- src/aig/gia/giaNf.c | 5 +++++ src/base/exor/exorBits.c | 5 +++++ src/map/if/acd/ac_decomposition.hpp | 5 +++++ src/map/if/acd/acd66.hpp | 5 +++++ src/map/if/acd/acdXX.hpp | 5 +++++ src/map/if/ifDec08.c | 5 +++++ src/map/if/ifDec10.c | 5 +++++ src/misc/util/utilTruth.h | 5 +++++ src/opt/dau/dauCanon.c | 5 +++++ 9 files changed, 45 insertions(+) diff --git a/src/aig/gia/giaNf.c b/src/aig/gia/giaNf.c index 10ce25c5b1..f41d8dd493 100644 --- a/src/aig/gia/giaNf.c +++ b/src/aig/gia/giaNf.c @@ -32,6 +32,11 @@ #include "map/scl/sclCon.h" #include "misc/tim/tim.h" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_IMPL_START //////////////////////////////////////////////////////////////////////// diff --git a/src/base/exor/exorBits.c b/src/base/exor/exorBits.c index 7731a8c3b4..e1d8e556eb 100644 --- a/src/base/exor/exorBits.c +++ b/src/base/exor/exorBits.c @@ -40,6 +40,11 @@ #include "exor.h" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_IMPL_START //////////////////////////////////////////////////////////////////////// diff --git a/src/map/if/acd/ac_decomposition.hpp b/src/map/if/acd/ac_decomposition.hpp index 803f9ce59d..7628ab39ec 100644 --- a/src/map/if/acd/ac_decomposition.hpp +++ b/src/map/if/acd/ac_decomposition.hpp @@ -40,6 +40,11 @@ #include "kitty_operators.hpp" #include "kitty_static_tt.hpp" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_CXX_HEADER_START namespace acd diff --git a/src/map/if/acd/acd66.hpp b/src/map/if/acd/acd66.hpp index 1095129867..607eee197c 100644 --- a/src/map/if/acd/acd66.hpp +++ b/src/map/if/acd/acd66.hpp @@ -39,6 +39,11 @@ #include "kitty_operators.hpp" #include "kitty_static_tt.hpp" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_CXX_HEADER_START namespace acd diff --git a/src/map/if/acd/acdXX.hpp b/src/map/if/acd/acdXX.hpp index e57199ce75..13d1bdddf6 100644 --- a/src/map/if/acd/acdXX.hpp +++ b/src/map/if/acd/acdXX.hpp @@ -39,6 +39,11 @@ #include "kitty_operators.hpp" #include "kitty_static_tt.hpp" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_CXX_HEADER_START namespace acd diff --git a/src/map/if/ifDec08.c b/src/map/if/ifDec08.c index ceaa833dae..30a6cd6673 100644 --- a/src/map/if/ifDec08.c +++ b/src/map/if/ifDec08.c @@ -22,6 +22,11 @@ #include "misc/extra/extra.h" #include "bool/kit/kit.h" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_IMPL_START diff --git a/src/map/if/ifDec10.c b/src/map/if/ifDec10.c index 0c3cf5e545..73717180b4 100644 --- a/src/map/if/ifDec10.c +++ b/src/map/if/ifDec10.c @@ -22,6 +22,11 @@ #include "misc/extra/extra.h" #include "bool/kit/kit.h" +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_IMPL_START diff --git a/src/misc/util/utilTruth.h b/src/misc/util/utilTruth.h index 86fe29a743..35f0f83e45 100644 --- a/src/misc/util/utilTruth.h +++ b/src/misc/util/utilTruth.h @@ -25,6 +25,11 @@ /// INCLUDES /// //////////////////////////////////////////////////////////////////////// +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + //////////////////////////////////////////////////////////////////////// /// PARAMETERS /// //////////////////////////////////////////////////////////////////////// diff --git a/src/opt/dau/dauCanon.c b/src/opt/dau/dauCanon.c index 0636024674..bd97c280c0 100644 --- a/src/opt/dau/dauCanon.c +++ b/src/opt/dau/dauCanon.c @@ -24,6 +24,11 @@ #include "bool/lucky/lucky.h" #include +#ifdef _MSC_VER +# include +# define __builtin_popcount __popcnt +#endif + ABC_NAMESPACE_IMPL_START ////////////////////////////////////////////////////////////////////////