More bitops.

This commit is contained in:
Maxim Poliakovski 2023-04-01 10:55:39 +02:00
parent f181e721b0
commit e01fea87a9

View File

@ -24,6 +24,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef BIT_OPS_H
#define BIT_OPS_H
#include <cinttypes>
#if defined(__GNUG__) && !defined(__clang__) // GCC, mybe ICC but not Clang
# include <x86intrin.h>
@ -56,4 +58,24 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#endif
template <class T>
inline T extract_bits(T val, int pos, int len) {
return (val >> pos) & (((T)1 << len) - 1);
}
template <class T>
inline void insert_bits(T &old_val, T new_val, int pos, int len) {
T mask = (((T)1 << len) - 1) << pos;
old_val = (old_val & ~mask) | ((new_val << pos) & mask);
}
/* Return true if the specified bit is different in the given numbers. */
static inline bool bit_changed(uint64_t old_val, uint64_t new_val, int bit_num) {
return (old_val ^ new_val) & (1ULL << bit_num);
}
static inline bool bit_set(const uint64_t val, const int bit_num) {
return !!(val & (1ULL << bit_num));
}
#endif // BIT_OPS_H