#ifndef NALL_BIT_HPP
#define NALL_BIT_HPP
namespace nall {
template<unsigned bits>
constexpr inline uintmax_t uclamp(const uintmax_t x) {
enum : uintmax_t { b = 1ull << (bits - 1), y = b * 2 - 1 };
return y + ((x - y) & -(x < y));
}
template<unsigned bits>
constexpr inline uintmax_t uclip(const uintmax_t x) {
return (x & ((uintmax_t)(((uintmax_t)(1ull << (bits - 1))) * 2 - 1)));
}
template<unsigned bits>
constexpr inline intmax_t sclamp(const intmax_t x) {
return (x > (((intmax_t)(1ull << (bits - 1))) - 1)) ? (((intmax_t)(1ull << (bits - 1))) - 1) : (x < -((intmax_t)(1ull << (bits - 1)))) ? -((intmax_t)(1ull << (bits - 1))) : x;
}
template<unsigned bits>
constexpr inline intmax_t sclip(const intmax_t x) {
return ((x & ((uintmax_t)(((uintmax_t)(1ull << (bits - 1))) * 2 - 1))) ^ ((uintmax_t)(1ull << (bits - 1)))) - ((uintmax_t)(1ull << (bits - 1)));
}
namespace bit {
constexpr inline uintmax_t lowest(const uintmax_t x) {
return x & -x;
}
constexpr inline uintmax_t clear_lowest(const uintmax_t x) {
return x & (x - 1);
}
constexpr inline uintmax_t set_lowest(const uintmax_t x) {
return x | (x + 1);
}
inline unsigned count(uintmax_t x) {
unsigned count = 0;
do count += x & 1; while(x >>= 1);
return count;
}
inline uintmax_t round(uintmax_t x) {
if((x & (x - 1)) == 0) return x;
while(x & (x - 1)) x &= x - 1;
return x << 1;
}
}
}
#endif