bitvector population inline and using gcc builtin hw instruction
This commit is contained in:
1 parent
f2aeb6ff9c
commit
c44dcf132a
3 files changed
+38
-51
No files matched your search
+2
-49
@@ -182,31 +182,7 @@ return *this;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*number of ones in a binary number, from "Hacker's delight" book*/
|
//word_popul is inline in bitvector.h
|
||||||
#ifdef LONG_IS_32
|
|
||||||
static unsigned int word_popul(unsigned long x)
|
|
||||||
{
|
|
||||||
x -= ((x>>1)&0x55555555);
|
|
||||||
x = (x&0x33333333) + ((x>>2)&0x33333333);
|
|
||||||
x=(x + (x>>4))&0x0f0f0f0f;
|
|
||||||
x+= (x>>8);
|
|
||||||
x+= (x>>16);
|
|
||||||
return x&0x3f;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
//@@@@ use an efficient trick too
|
|
||||||
static unsigned int word_popul(unsigned long x)
|
|
||||||
{
|
|
||||||
unsigned int s=0;
|
|
||||||
for(int i=0; i<64; ++i)
|
|
||||||
{
|
|
||||||
if(x&1) ++s;
|
|
||||||
x>>=1;
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
bitvector& bitvector::operator>>=(unsigned int i)
|
bitvector& bitvector::operator>>=(unsigned int i)
|
||||||
@@ -260,30 +236,7 @@ zero_padding();
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//with before>0 only the ones at positions 0..before-1 are counted
|
//population is inline in bitvector.h
|
||||||
unsigned int bitvector::population(const unsigned int before) const
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
unsigned int s=0;
|
|
||||||
if(before)
|
|
||||||
{
|
|
||||||
if(before>size()) laerror("before parameter beyond the size in population()");
|
|
||||||
int full=before/blockbits;
|
|
||||||
for(i=0; i<full; ++i) s+=word_popul(v[i]);
|
|
||||||
int rest=before%blockbits;
|
|
||||||
if(rest) s+=word_popul(v[full] & ((((bitvector_block)1)<<rest)-1));
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
for(i=0; i<nn-1; ++i) s+=word_popul(v[i]);
|
|
||||||
bitvector_block a=v[nn-1];
|
|
||||||
if(modulo)
|
|
||||||
{
|
|
||||||
bitvector_block mask= ~((bitvector_block)0);
|
|
||||||
mask <<=modulo;
|
|
||||||
a &= ~mask;
|
|
||||||
}
|
|
||||||
return s+word_popul(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int bitvector::bitdiff(const bitvector &y) const
|
unsigned int bitvector::bitdiff(const bitvector &y) const
|
||||||
{
|
{
|
||||||
|
|||||||
+33
-1
@@ -43,6 +43,20 @@ inline unsigned int bitvector_rounded(unsigned int n)
|
|||||||
return ((n+blockbits-1)/blockbits)*blockbits;
|
return ((n+blockbits-1)/blockbits)*blockbits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//number of ones in a block (Hamming weight)
|
||||||
|
inline unsigned int word_popul(bitvector_block x)
|
||||||
|
{
|
||||||
|
#ifdef __GNUC__
|
||||||
|
return __builtin_popcountll(x);
|
||||||
|
#else
|
||||||
|
//from the "Hacker's delight" book
|
||||||
|
x -= (x>>1) & 0x5555555555555555ULL;
|
||||||
|
x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
|
||||||
|
x = (x + (x>>4)) & 0x0f0f0f0f0f0f0f0fULL;
|
||||||
|
return (x*0x0101010101010101ULL)>>56;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
class bitvector : public NRVec<bitvector_block>
|
class bitvector : public NRVec<bitvector_block>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -176,7 +190,25 @@ if(i>=size())
|
|||||||
bitvector lcm(const bitvector &rhs) const {return (*this)*rhs/this->gcd(rhs);};
|
bitvector lcm(const bitvector &rhs) const {return (*this)*rhs/this->gcd(rhs);};
|
||||||
bitvector composition(const bitvector &rhs) const;
|
bitvector composition(const bitvector &rhs) const;
|
||||||
unsigned int bitdiff(const bitvector &y) const; //number of differing bits (Hamming distance)
|
unsigned int bitdiff(const bitvector &y) const; //number of differing bits (Hamming distance)
|
||||||
unsigned int population(const unsigned int before=0) const; //number of 1's (Hamming weight)
|
unsigned int population(const unsigned int before=0) const //number of 1's (Hamming weight), with before>0 only at positions 0..before-1
|
||||||
|
{
|
||||||
|
unsigned int s=0;
|
||||||
|
if(before)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
if(before>size()) laerror("before parameter beyond the size in population()");
|
||||||
|
#endif
|
||||||
|
int full=before/blockbits;
|
||||||
|
for(int i=0; i<full; ++i) s+=word_popul(v[i]);
|
||||||
|
int rest=before%blockbits;
|
||||||
|
if(rest) s+=word_popul(v[full] & ((((bitvector_block)1)<<rest)-1));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
for(int i=0; i<nn-1; ++i) s+=word_popul(v[i]);
|
||||||
|
bitvector_block a=v[nn-1];
|
||||||
|
if(modulo) a &= ~((~((bitvector_block)0))<<modulo);
|
||||||
|
return s+word_popul(a);
|
||||||
|
};
|
||||||
unsigned int nlz() const; //number of leading zeroes
|
unsigned int nlz() const; //number of leading zeroes
|
||||||
unsigned int degree() const {if(iszero()) return 0; else return size()-nlz()-1;}; //interprested as a polynomial over GF(2)
|
unsigned int degree() const {if(iszero()) return 0; else return size()-nlz()-1;}; //interprested as a polynomial over GF(2)
|
||||||
void truncate(int t=0) {int s=degree()+1; if(t>s) s=t; resize(s,true);};
|
void truncate(int t=0) {int s=degree()+1; if(t>s) s=t; resize(s,true);};
|
||||||
|
|||||||
+3
-1
@@ -61,7 +61,9 @@ when the data are shared (count check as in \code{NRVec::operator[]}).
|
|||||||
\lafunc{bitvector &operator&=(const bitvector_block rhs); operator|=, operator^=}{Bitwise operation with the \emph{first block} only.}
|
\lafunc{bitvector &operator&=(const bitvector_block rhs); operator|=, operator^=}{Bitwise operation with the \emph{first block} only.}
|
||||||
\lafunc{bitvector &operator>>=(unsigned int i); bitvector operator>>(unsigned int i) const}{Logical right shift (towards bit 0), length kept.}
|
\lafunc{bitvector &operator>>=(unsigned int i); bitvector operator>>(unsigned int i) const}{Logical right shift (towards bit 0), length kept.}
|
||||||
\lafunc{bitvector &leftshift(unsigned int i, bool autoresize=false); bitvector &operator<<=(unsigned int i); bitvector operator<<(unsigned int i) const}{Logical left shift; \code{<<} and \code{<<=} enlarge the vector by $i$ bits (autoresize), \code{leftshift} by default keeps the length and drops the top bits.}
|
\lafunc{bitvector &leftshift(unsigned int i, bool autoresize=false); bitvector &operator<<=(unsigned int i); bitvector operator<<(unsigned int i) const}{Logical left shift; \code{<<} and \code{<<=} enlarge the vector by $i$ bits (autoresize), \code{leftshift} by default keeps the length and drops the top bits.}
|
||||||
\lafunc{unsigned int population(const unsigned int before=0) const}{Hamming weight (number of ones); \code{before}$\neq0$ is not implemented (laerror).}
|
\lafunc{unsigned int population(const unsigned int before=0) const}{Hamming weight (number of ones); with \code{before}$\neq0$ only the ones at the positions $0,\ldots,$\code{before}$-1$ are
|
||||||
|
counted (\code{laerror} if \code{before} exceeds the size, checked only with \code{DEBUG}). Inline, using the hardware population
|
||||||
|
count (\code{__builtin_popcountll}) with gcc.}
|
||||||
\lafunc{unsigned int bitdiff(const bitvector &y) const}{Hamming distance (same number of blocks required).}
|
\lafunc{unsigned int bitdiff(const bitvector &y) const}{Hamming distance (same number of blocks required).}
|
||||||
\lafunc{unsigned int nlz() const; unsigned int ntz() const}{Number of leading (from the top bit) and trailing zeros.}
|
\lafunc{unsigned int nlz() const; unsigned int ntz() const}{Number of leading (from the top bit) and trailing zeros.}
|
||||||
\end{lareference}
|
\end{lareference}
|
||||||
|
|||||||
Reference in new issue
Block a user