17#ifndef LIB_BIG_INT_UTIL_H_
18#define LIB_BIG_INT_UTIL_H_
20#include <boost/multiprecision/cpp_int.hpp>
23typedef boost::multiprecision::cpp_int big_int;
30big_int ripBits(big_int &value,
int bits);
39BitRange findOnes(
const big_int &value);
41big_int cvtInt(
const char *s,
unsigned base);
42big_int shift_left(
const big_int &v,
unsigned bits);
43big_int shift_right(
const big_int &v,
unsigned bits);
45big_int maskFromSlice(
unsigned m,
unsigned l);
46big_int mask(
unsigned bits);
48inline unsigned scan0_positive(
const boost::multiprecision::cpp_int &val,
unsigned pos) {
49 while (boost::multiprecision::bit_test(val, pos)) ++pos;
52inline unsigned scan1_positive(
const boost::multiprecision::cpp_int &val,
unsigned pos) {
53 if (val == 0 || pos > boost::multiprecision::msb(val))
return ~0U;
54 unsigned lsb = boost::multiprecision::lsb(val);
55 if (lsb >= pos)
return lsb;
56 while (!boost::multiprecision::bit_test(val, pos)) ++pos;
59inline unsigned scan0(
const boost::multiprecision::cpp_int &val,
unsigned pos) {
60 if (val < 0)
return scan1_positive(-val - 1, pos);
61 return scan0_positive(val, pos);
63inline unsigned scan1(
const boost::multiprecision::cpp_int &val,
unsigned pos) {
64 if (val < 0)
return scan0_positive(-val - 1, pos);
65 return scan1_positive(val, pos);
70static inline unsigned bitcount(big_int v) {
71 if (v < 0)
return ~0U;
80static inline int ffs(big_int v) {
81 if (v <= 0)
return -1;
82 return boost::multiprecision::lsb(v);
85static inline int floor_log2(big_int v) {
94static inline int ceil_log2(big_int v) {
return v ? floor_log2(v - 1) + 1 : -1; }
Definition big_int_util.h:32