Skip to content

Commit

Permalink
Merge pull request #309 from coastalwhite/chore-popcount-intrinsics
Browse files Browse the repository at this point in the history
chore: `__builtin_popcount` to replace BitCount8
  • Loading branch information
alanminko authored Aug 1, 2024
2 parents 279a909 + 849adb2 commit a06dde4
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 79 deletions.
7 changes: 6 additions & 1 deletion src/aig/gia/giaNf.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include "map/scl/sclCon.h"
#include "misc/tim/tim.h"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -225,7 +230,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
Expand Down
19 changes: 6 additions & 13 deletions src/base/exor/exorBits.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@

#include "exor.h"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -80,18 +85,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,
Expand Down Expand Up @@ -158,7 +151,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
Expand Down
12 changes: 0 additions & 12 deletions src/bdd/cas/casCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
5 changes: 5 additions & 0 deletions src/map/if/acd/ac_decomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
#include "kitty_operators.hpp"
#include "kitty_static_tt.hpp"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

ABC_NAMESPACE_CXX_HEADER_START

namespace acd
Expand Down
5 changes: 5 additions & 0 deletions src/map/if/acd/acd66.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#include "kitty_operators.hpp"
#include "kitty_static_tt.hpp"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

ABC_NAMESPACE_CXX_HEADER_START

namespace acd
Expand Down
5 changes: 5 additions & 0 deletions src/map/if/acd/acdXX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#include "kitty_operators.hpp"
#include "kitty_static_tt.hpp"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

ABC_NAMESPACE_CXX_HEADER_START

namespace acd
Expand Down
19 changes: 4 additions & 15 deletions src/map/if/ifDec07.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down Expand Up @@ -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] )
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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)<<Bit0) );
Expand Down
18 changes: 6 additions & 12 deletions src/map/if/ifDec08.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,18 @@
#include "misc/extra/extra.h"
#include "bool/kit/kit.h"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

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) },
Expand Down Expand Up @@ -374,7 +368,7 @@ void If_Dec08Cofactors( word * pF, int nVars, int iVar, word * pCof0, word * pCo
static inline int If_Dec08Count16( 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 )
{
Expand Down
18 changes: 6 additions & 12 deletions src/map/if/ifDec10.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,18 @@
#include "misc/extra/extra.h"
#include "bool/kit/kit.h"

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

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) },
Expand Down Expand Up @@ -371,7 +365,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 )
{
Expand Down
18 changes: 6 additions & 12 deletions src/misc/util/utilTruth.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
/// INCLUDES ///
////////////////////////////////////////////////////////////////////////

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

////////////////////////////////////////////////////////////////////////
/// PARAMETERS ///
////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -129,18 +134,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 ///
Expand Down
9 changes: 7 additions & 2 deletions src/opt/dau/dauCanon.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#include "bool/lucky/lucky.h"
#include <math.h>

#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -380,7 +385,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;
Expand All @@ -406,7 +411,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++;
Expand Down

0 comments on commit a06dde4

Please sign in to comment.