cleanup bitops

This commit is contained in:
Jonathan Campbell
2024-03-22 15:47:55 -07:00
parent 681aff263c
commit d9bfb74b87

View File

@@ -97,7 +97,9 @@ template <typename T=unsigned int> static inline constexpr unsigned int _bitseql
}
/* private common function */
template <typename T=unsigned int> static inline void _bitseqlengthlsb_1(unsigned int &c,T &v) {
template <typename T=unsigned int> static inline unsigned int _bitseqlengthlsb_1(T &v) {
unsigned int c = 0;
while ((v & 0xFFUL) == 0xFFUL) {
v >>= (T)8UL;
c += (T)8;
@@ -106,6 +108,8 @@ template <typename T=unsigned int> static inline void _bitseqlengthlsb_1(unsigne
v >>= (T)1UL;
c++;
}
return c;
}
/* Return number of sequential 1 bits counting from LSB in value 'v' of type 'T'
@@ -126,9 +130,7 @@ template <const unsigned int v> static inline constexpr unsigned int bitseqlengt
}
template <typename T=unsigned int> static inline unsigned int bitseqlengthlsb(T v) {
unsigned int c = 0;
_bitseqlengthlsb_1(/*&*/c,/*&*/v);
return c;
return _bitseqlengthlsb_1(/*&*/v);
}
@@ -145,7 +147,7 @@ template <const unsigned int a,typename T=unsigned int> static inline constexpr
return (T)1U << (T)a;
}
template <typename T=unsigned int> static inline constexpr T bit2mask(const unsigned int a) {
template <typename T=unsigned int> static inline constexpr T bit2mask(const T a) {
return (T)1U << (T)a;
}
@@ -187,7 +189,7 @@ template <const unsigned int a,typename T=unsigned int> static inline constexpr
return bitcount2masklsb<a,0u,T>();
}
template <typename T=unsigned int> static inline constexpr T bitcount2masklsb(const unsigned int a,const unsigned int offset=0) {
template <typename T=unsigned int> static inline constexpr T bitcount2masklsb(const T a,const unsigned int offset=0) {
/* NTS: special case for a == type_bits because shifting the size of a register OR LARGER is undefined.
* On Intel x86 processors, with 32-bit integers, x >> 32 == x >> 0 because only the low 5 bits are used */
return ((a < type_bits<T>()) ? (bit2mask<T>(a) - (T)1u) : allones<T>()) << (T)offset;
@@ -220,7 +222,7 @@ template <const unsigned int a,typename T=unsigned int> static inline constexpr
return bitcount2maskmsb<a,0u,T>();
}
template <typename T=unsigned int> static inline constexpr T bitcount2maskmsb(const unsigned int a,const unsigned int offset=0) {
template <typename T=unsigned int> static inline constexpr T bitcount2maskmsb(const T a,const unsigned int offset=0) {
/* NTS: special case for a == type_bits because shifting the size of a register OR LARGER is undefined.
* On Intel x86 processors, with 32-bit integers, x >> 32 == x >> 0 because only the low 5 bits are used */
return ((a != (T)0) ? ((T)(allones<T>() << (T)(type_bits<T>() - a)) >> (T)offset) : allzero<T>());
@@ -283,7 +285,7 @@ template <typename T=unsigned int> static inline constexpr T bitcount2maskmsb(co
* For integer values, apply the AND operator to the same bit position from both integers for each bit across the width of the integer.
*
* @return Boolean true if 'a' is a power of 2 */
template <typename T=unsigned int> static inline constexpr bool ispowerof2(const unsigned int a) {
template <typename T=unsigned int> static inline constexpr bool ispowerof2(const T a) {
return (a & (a-(T)1u)) == 0;
}
@@ -342,7 +344,7 @@ template <typename T=unsigned int> static inline unsigned int log2(T v) {
/* return type, pair */
class bitseqlengthandpos_ret_t {
public:
bitseqlengthandpos_ret_t(const unsigned int _start,const unsigned int _length) : start(_start), length(_length) { }
constexpr bitseqlengthandpos_ret_t(const unsigned int _start,const unsigned int _length) : start(_start), length(_length) { }
public:
bool operator==(const bitseqlengthandpos_ret_t &n) const {
return (n.start == start) &&