mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-09-02 22:22:41 +02:00
v108.9
* multi-monitor support * improved pause/frame advance support * added option to disable video dimming when idle
This commit is contained in:
124
nall/primitives/bit-field.hpp
Normal file
124
nall/primitives/bit-field.hpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#pragma once
|
||||
|
||||
namespace nall {
|
||||
|
||||
template<int...> struct BitField;
|
||||
|
||||
/* static BitField */
|
||||
|
||||
template<int Precision, int Index> struct BitField<Precision, Index> {
|
||||
static_assert(Precision >= 1 && Precision <= 64);
|
||||
enum : uint { bits = Precision };
|
||||
using type =
|
||||
conditional_t<bits <= 8, uint8_t,
|
||||
conditional_t<bits <= 16, uint16_t,
|
||||
conditional_t<bits <= 32, uint32_t,
|
||||
conditional_t<bits <= 64, uint64_t,
|
||||
void>>>>;
|
||||
enum : uint { shift = Index < 0 ? Precision + Index : Index };
|
||||
enum : type { mask = 1ull << shift };
|
||||
|
||||
BitField(const BitField&) = delete;
|
||||
|
||||
inline auto& operator=(const BitField& source) {
|
||||
target = target & ~mask | (bool)source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline BitField(T* source) : target((type&)*source) {
|
||||
static_assert(sizeof(T) == sizeof(type));
|
||||
}
|
||||
|
||||
inline auto bit() const {
|
||||
return shift;
|
||||
}
|
||||
|
||||
inline operator bool() const {
|
||||
return target & mask;
|
||||
}
|
||||
|
||||
inline auto& operator=(bool source) {
|
||||
target = target & ~mask | source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator&=(bool source) {
|
||||
target = target & (~mask | source << shift);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator^=(bool source) {
|
||||
target = target ^ source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator|=(bool source) {
|
||||
target = target | source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
type& target;
|
||||
};
|
||||
|
||||
/* dynamic BitField */
|
||||
|
||||
template<int Precision> struct BitField<Precision> {
|
||||
static_assert(Precision >= 1 && Precision <= 64);
|
||||
enum : uint { bits = Precision };
|
||||
using type =
|
||||
conditional_t<bits <= 8, uint8_t,
|
||||
conditional_t<bits <= 16, uint16_t,
|
||||
conditional_t<bits <= 32, uint32_t,
|
||||
conditional_t<bits <= 64, uint64_t,
|
||||
void>>>>;
|
||||
|
||||
BitField(const BitField&) = delete;
|
||||
|
||||
inline auto& operator=(const BitField& source) {
|
||||
target = target & ~mask | (bool)source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline BitField(T* source, int index) : target((type&)*source) {
|
||||
static_assert(sizeof(T) == sizeof(type));
|
||||
if(index < 0) index = Precision + index;
|
||||
mask = 1ull << index;
|
||||
shift = index;
|
||||
}
|
||||
|
||||
inline auto bit() const {
|
||||
return shift;
|
||||
}
|
||||
|
||||
inline operator bool() const {
|
||||
return target & mask;
|
||||
}
|
||||
|
||||
inline auto& operator=(bool source) {
|
||||
target = target & ~mask | source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator&=(bool source) {
|
||||
target = target & (~mask | source << shift);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator^=(bool source) {
|
||||
target = target ^ source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator|=(bool source) {
|
||||
target = target | source << shift;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
type& target;
|
||||
type mask;
|
||||
uint shift;
|
||||
};
|
||||
|
||||
}
|
@@ -2,66 +2,254 @@
|
||||
|
||||
namespace nall {
|
||||
|
||||
//warning: so that BitRange can modify the underlying number directly, it must bind a reference.
|
||||
//as a result, auto value = number.bits() will capture by-reference, rather than by-value.
|
||||
template<int...> struct BitRange;
|
||||
|
||||
template<int Precision> struct BitRange {
|
||||
/* static BitRange */
|
||||
|
||||
template<int Precision, int Lo, int Hi> struct BitRange<Precision, Lo, Hi> {
|
||||
static_assert(Precision >= 1 && Precision <= 64);
|
||||
static inline constexpr auto bits() -> uint { return Precision; }
|
||||
using utype =
|
||||
conditional_t<bits() <= 8, uint8_t,
|
||||
conditional_t<bits() <= 16, uint16_t,
|
||||
conditional_t<bits() <= 32, uint32_t,
|
||||
conditional_t<bits() <= 64, uint64_t,
|
||||
enum : uint { bits = Precision };
|
||||
using type =
|
||||
conditional_t<bits <= 8, uint8_t,
|
||||
conditional_t<bits <= 16, uint16_t,
|
||||
conditional_t<bits <= 32, uint32_t,
|
||||
conditional_t<bits <= 64, uint64_t,
|
||||
void>>>>;
|
||||
static inline constexpr auto mask() -> utype { return ~0ull >> 64 - bits(); }
|
||||
enum : uint { lo = Lo < 0 ? Precision + Lo : Lo };
|
||||
enum : uint { hi = Hi < 0 ? Precision + Hi : Hi };
|
||||
enum : type { mask = ~0ull >> 64 - (hi - lo + 1) << lo };
|
||||
enum : uint { shift = lo };
|
||||
|
||||
inline BitRange(utype& source, int lo, int hi) : source(source) {
|
||||
if(lo < 0) lo = Precision + lo;
|
||||
if(hi < 0) hi = Precision + hi;
|
||||
if(lo > hi) swap(lo, hi);
|
||||
this->lo = lo;
|
||||
this->hi = hi;
|
||||
}
|
||||
inline auto& operator=(BitRange& source) { return set(source.get()); }
|
||||
BitRange(const BitRange& source) = delete;
|
||||
|
||||
inline operator utype() const { return get(); }
|
||||
|
||||
inline auto operator++(int) { auto value = get(); set(value + 1); return value; }
|
||||
inline auto operator--(int) { auto value = get(); set(value - 1); return value; }
|
||||
|
||||
inline auto& operator++() { return set(get() + 1); }
|
||||
inline auto& operator--() { return set(get() - 1); }
|
||||
|
||||
template<typename T> inline auto& operator =(const T& value) { return set( value); }
|
||||
template<typename T> inline auto& operator *=(const T& value) { return set(get() * value); }
|
||||
template<typename T> inline auto& operator /=(const T& value) { return set(get() / value); }
|
||||
template<typename T> inline auto& operator %=(const T& value) { return set(get() % value); }
|
||||
template<typename T> inline auto& operator +=(const T& value) { return set(get() + value); }
|
||||
template<typename T> inline auto& operator -=(const T& value) { return set(get() - value); }
|
||||
template<typename T> inline auto& operator<<=(const T& value) { return set(get() << value); }
|
||||
template<typename T> inline auto& operator>>=(const T& value) { return set(get() >> value); }
|
||||
template<typename T> inline auto& operator &=(const T& value) { return set(get() & value); }
|
||||
template<typename T> inline auto& operator ^=(const T& value) { return set(get() ^ value); }
|
||||
template<typename T> inline auto& operator |=(const T& value) { return set(get() | value); }
|
||||
|
||||
private:
|
||||
inline auto get() const -> utype {
|
||||
const utype rangeBits = hi - lo + 1;
|
||||
const utype rangeMask = (1ull << rangeBits) - 1 << lo & mask();
|
||||
return (source & rangeMask) >> lo;
|
||||
}
|
||||
|
||||
inline auto& set(const utype& value) {
|
||||
const utype rangeBits = hi - lo + 1;
|
||||
const utype rangeMask = (1ull << rangeBits) - 1 << lo & mask();
|
||||
source = source & ~rangeMask | value << lo & rangeMask;
|
||||
inline auto& operator=(const BitRange& source) {
|
||||
target = target & ~mask | ((source.target & source.mask) >> source.shift) << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
utype& source;
|
||||
uint lo;
|
||||
uint hi;
|
||||
template<typename T> inline BitRange(T* source) : target((type&)*source) {
|
||||
static_assert(sizeof(T) == sizeof(type));
|
||||
}
|
||||
|
||||
inline operator type() const {
|
||||
return (target & mask) >> shift;
|
||||
}
|
||||
|
||||
inline auto operator++(int) {
|
||||
auto value = (target & mask) >> shift;
|
||||
target = target & ~mask | target + (1 << shift) & mask;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline auto operator--(int) {
|
||||
auto value = (target & mask) >> shift;
|
||||
target = target & ~mask | target - (1 << shift) & mask;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline auto& operator++() {
|
||||
target = target & ~mask | target + (1 << shift) & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator--() {
|
||||
target = target & ~mask | target - (1 << shift) & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator=(const T& source) {
|
||||
target = target & ~mask | source << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator*=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) * source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator/=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) / source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator%=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) % source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator+=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) + source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator-=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) - source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator<<=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) << source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator>>=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) >> source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator&=(const T& source) {
|
||||
target = target & (~mask | source << shift & mask);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator^=(const T& source) {
|
||||
target = target ^ source << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator|=(const T& source) {
|
||||
target = target | source << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
type& target;
|
||||
};
|
||||
|
||||
/* dynamic BitRange */
|
||||
|
||||
template<int Precision> struct BitRange<Precision> {
|
||||
static_assert(Precision >= 1 && Precision <= 64);
|
||||
enum : uint { bits = Precision };
|
||||
using type =
|
||||
conditional_t<bits <= 8, uint8_t,
|
||||
conditional_t<bits <= 16, uint16_t,
|
||||
conditional_t<bits <= 32, uint32_t,
|
||||
conditional_t<bits <= 64, uint64_t,
|
||||
void>>>>;
|
||||
|
||||
BitRange(const BitRange& source) = delete;
|
||||
|
||||
inline auto& operator=(const BitRange& source) {
|
||||
target = target & ~mask | ((source.target & source.mask) >> source.shift) << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline BitRange(T* source, int index) : target((type&)*source) {
|
||||
static_assert(sizeof(T) == sizeof(type));
|
||||
if(index < 0) index = Precision + index;
|
||||
mask = 1ull << index;
|
||||
shift = index;
|
||||
}
|
||||
|
||||
template<typename T> inline BitRange(T* source, int lo, int hi) : target((type&)*source) {
|
||||
static_assert(sizeof(T) == sizeof(type));
|
||||
if(lo < 0) lo = Precision + lo;
|
||||
if(hi < 0) hi = Precision + hi;
|
||||
if(lo > hi) swap(lo, hi);
|
||||
mask = ~0ull >> 64 - (hi - lo + 1) << lo;
|
||||
shift = lo;
|
||||
}
|
||||
|
||||
inline operator type() const {
|
||||
return (target & mask) >> shift;
|
||||
}
|
||||
|
||||
inline auto operator++(int) {
|
||||
auto value = (target & mask) >> shift;
|
||||
target = target & ~mask | target + (1 << shift) & mask;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline auto operator--(int) {
|
||||
auto value = (target & mask) >> shift;
|
||||
target = target & ~mask | target - (1 << shift) & mask;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline auto& operator++() {
|
||||
target = target & ~mask | target + (1 << shift) & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline auto& operator--() {
|
||||
target = target & ~mask | target - (1 << shift) & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator=(const T& source) {
|
||||
target = target & ~mask | source << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator*=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) * source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator/=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) / source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator%=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) % source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator+=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) + source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator-=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) - source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator<<=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) << source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator>>=(const T& source) {
|
||||
auto value = ((target & mask) >> shift) >> source;
|
||||
target = target & ~mask | value << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator&=(const T& source) {
|
||||
target = target & (~mask | source << shift & mask);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator^=(const T& source) {
|
||||
target = target ^ source << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T> inline auto& operator|=(const T& source) {
|
||||
target = target | source << shift & mask;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
type& target;
|
||||
type mask;
|
||||
uint shift;
|
||||
};
|
||||
|
||||
}
|
||||
|
@@ -12,59 +12,62 @@ template<uint Precision> struct Integer {
|
||||
conditional_t<bits() <= 64, int64_t,
|
||||
void>>>>;
|
||||
using utype = typename Natural<Precision>::utype;
|
||||
static inline constexpr auto mask() -> utype { return ~0ull >> 64 - bits(); }
|
||||
static inline constexpr auto mask() -> utype { return ~0ull >> 64 - Precision; }
|
||||
static inline constexpr auto sign() -> utype { return 1ull << Precision - 1; }
|
||||
|
||||
inline Integer() : data(0) {}
|
||||
template<uint Bits> inline Integer(Integer<Bits> value) { data = mask(value); }
|
||||
template<typename T> inline Integer(const T& value) { data = mask(value); }
|
||||
explicit inline Integer(const char* value) { data = mask(toInteger(value)); }
|
||||
template<uint Bits> inline Integer(Integer<Bits> value) { data = cast(value); }
|
||||
template<typename T> inline Integer(const T& value) { data = cast(value); }
|
||||
explicit inline Integer(const char* value) { data = cast(toInteger(value)); }
|
||||
|
||||
inline operator stype() const { return data; }
|
||||
|
||||
inline auto operator++(int) { auto value = *this; data = mask(data + 1); return value; }
|
||||
inline auto operator--(int) { auto value = *this; data = mask(data - 1); return value; }
|
||||
inline auto operator++(int) { auto value = *this; data = cast(data + 1); return value; }
|
||||
inline auto operator--(int) { auto value = *this; data = cast(data - 1); return value; }
|
||||
|
||||
inline auto& operator++() { data = mask(data + 1); return *this; }
|
||||
inline auto& operator--() { data = mask(data - 1); return *this; }
|
||||
inline auto& operator++() { data = cast(data + 1); return *this; }
|
||||
inline auto& operator--() { data = cast(data - 1); return *this; }
|
||||
|
||||
template<typename T> inline auto& operator =(const T& value) { data = mask( value); return *this; }
|
||||
template<typename T> inline auto& operator *=(const T& value) { data = mask(data * value); return *this; }
|
||||
template<typename T> inline auto& operator /=(const T& value) { data = mask(data / value); return *this; }
|
||||
template<typename T> inline auto& operator %=(const T& value) { data = mask(data % value); return *this; }
|
||||
template<typename T> inline auto& operator +=(const T& value) { data = mask(data + value); return *this; }
|
||||
template<typename T> inline auto& operator -=(const T& value) { data = mask(data - value); return *this; }
|
||||
template<typename T> inline auto& operator<<=(const T& value) { data = mask(data << value); return *this; }
|
||||
template<typename T> inline auto& operator>>=(const T& value) { data = mask(data >> value); return *this; }
|
||||
template<typename T> inline auto& operator &=(const T& value) { data = mask(data & value); return *this; }
|
||||
template<typename T> inline auto& operator ^=(const T& value) { data = mask(data ^ value); return *this; }
|
||||
template<typename T> inline auto& operator |=(const T& value) { data = mask(data | value); return *this; }
|
||||
template<typename T> inline auto& operator =(const T& value) { data = cast( value); return *this; }
|
||||
template<typename T> inline auto& operator *=(const T& value) { data = cast(data * value); return *this; }
|
||||
template<typename T> inline auto& operator /=(const T& value) { data = cast(data / value); return *this; }
|
||||
template<typename T> inline auto& operator %=(const T& value) { data = cast(data % value); return *this; }
|
||||
template<typename T> inline auto& operator +=(const T& value) { data = cast(data + value); return *this; }
|
||||
template<typename T> inline auto& operator -=(const T& value) { data = cast(data - value); return *this; }
|
||||
template<typename T> inline auto& operator<<=(const T& value) { data = cast(data << value); return *this; }
|
||||
template<typename T> inline auto& operator>>=(const T& value) { data = cast(data >> value); return *this; }
|
||||
template<typename T> inline auto& operator &=(const T& value) { data = cast(data & value); return *this; }
|
||||
template<typename T> inline auto& operator ^=(const T& value) { data = cast(data ^ value); return *this; }
|
||||
template<typename T> inline auto& operator |=(const T& value) { data = cast(data | value); return *this; }
|
||||
|
||||
inline auto operator()(int index) -> BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto operator()(int lo, int hi) -> BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int index) -> BitRange<Precision> { return {&data, index}; }
|
||||
inline auto bit(int index) const -> const BitRange<Precision> { return {&data, index}; }
|
||||
|
||||
inline auto operator()(int index) const -> const BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto operator()(int lo, int hi) const -> const BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int lo, int hi) -> BitRange<Precision> { return {&data, lo, hi}; }
|
||||
inline auto bit(int lo, int hi) const -> const BitRange<Precision> { return {&data, lo, hi}; }
|
||||
|
||||
inline auto bits(int lo, int hi) -> BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int index) -> BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto byte(int index) -> BitRange<Precision> { return {(utype&)data, index * 8 + 0, index * 8 + 7}; }
|
||||
inline auto byte(int index) -> BitRange<Precision> { return {&data, index * 8 + 0, index * 8 + 7}; }
|
||||
inline auto byte(int index) const -> const BitRange<Precision> { return {&data, index * 8 + 0, index * 8 + 7}; }
|
||||
|
||||
inline auto bits(int lo, int hi) const -> const BitRange<Precision> { return {(utype&)*this, lo, hi}; }
|
||||
inline auto bit(int index) const -> const BitRange<Precision> { return {(utype&)*this, index, index}; }
|
||||
inline auto byte(int index) const -> const BitRange<Precision> { return {(utype&)*this, index * 8 + 0, index * 8 + 7}; }
|
||||
inline auto mask(int index) const -> utype {
|
||||
return data & 1 << index;
|
||||
}
|
||||
|
||||
inline auto mask(int lo, int hi) const -> utype {
|
||||
return data & (~0ull >> 64 - (hi - lo + 1) << lo);
|
||||
}
|
||||
|
||||
inline auto slice(int index) const { return Natural<>{bit(index)}; }
|
||||
inline auto slice(int lo, int hi) const { return Natural<>{bit(lo, hi)}; }
|
||||
|
||||
inline auto clamp(uint bits) -> stype {
|
||||
const int64_t b = 1ull << (bits - 1);
|
||||
const int64_t b = 1ull << bits - 1;
|
||||
const int64_t m = b - 1;
|
||||
return data > m ? m : data < -b ? -b : data;
|
||||
}
|
||||
|
||||
inline auto clip(uint bits) -> stype {
|
||||
const uint64_t b = 1ull << (bits - 1);
|
||||
const uint64_t b = 1ull << bits - 1;
|
||||
const uint64_t m = b * 2 - 1;
|
||||
return (data & m ^ b) - b;
|
||||
}
|
||||
@@ -73,7 +76,7 @@ template<uint Precision> struct Integer {
|
||||
inline auto natural() const -> Natural<Precision>;
|
||||
|
||||
private:
|
||||
inline auto mask(stype value) const -> stype {
|
||||
inline auto cast(stype value) const -> stype {
|
||||
return (value & mask() ^ sign()) - sign();
|
||||
}
|
||||
|
||||
|
@@ -11,58 +11,61 @@ template<uint Precision> struct Natural {
|
||||
conditional_t<bits() <= 32, uint32_t,
|
||||
conditional_t<bits() <= 64, uint64_t,
|
||||
void>>>>;
|
||||
static inline constexpr auto mask() -> utype { return ~0ull >> 64 - bits(); }
|
||||
static inline constexpr auto mask() -> utype { return ~0ull >> 64 - Precision; }
|
||||
|
||||
inline Natural() : data(0) {}
|
||||
template<uint Bits> inline Natural(Natural<Bits> value) { data = mask(value); }
|
||||
template<typename T> inline Natural(const T& value) { data = mask(value); }
|
||||
explicit inline Natural(const char* value) { data = mask(toNatural(value)); }
|
||||
template<uint Bits> inline Natural(Natural<Bits> value) { data = cast(value); }
|
||||
template<typename T> inline Natural(const T& value) { data = cast(value); }
|
||||
explicit inline Natural(const char* value) { data = cast(toNatural(value)); }
|
||||
|
||||
inline operator utype() const { return data; }
|
||||
|
||||
inline auto operator++(int) { auto value = *this; data = mask(data + 1); return value; }
|
||||
inline auto operator--(int) { auto value = *this; data = mask(data - 1); return value; }
|
||||
inline auto operator++(int) { auto value = *this; data = cast(data + 1); return value; }
|
||||
inline auto operator--(int) { auto value = *this; data = cast(data - 1); return value; }
|
||||
|
||||
inline auto& operator++() { data = mask(data + 1); return *this; }
|
||||
inline auto& operator--() { data = mask(data - 1); return *this; }
|
||||
inline auto& operator++() { data = cast(data + 1); return *this; }
|
||||
inline auto& operator--() { data = cast(data - 1); return *this; }
|
||||
|
||||
template<typename T> inline auto& operator =(const T& value) { data = mask( value); return *this; }
|
||||
template<typename T> inline auto& operator *=(const T& value) { data = mask(data * value); return *this; }
|
||||
template<typename T> inline auto& operator /=(const T& value) { data = mask(data / value); return *this; }
|
||||
template<typename T> inline auto& operator %=(const T& value) { data = mask(data % value); return *this; }
|
||||
template<typename T> inline auto& operator +=(const T& value) { data = mask(data + value); return *this; }
|
||||
template<typename T> inline auto& operator -=(const T& value) { data = mask(data - value); return *this; }
|
||||
template<typename T> inline auto& operator<<=(const T& value) { data = mask(data << value); return *this; }
|
||||
template<typename T> inline auto& operator>>=(const T& value) { data = mask(data >> value); return *this; }
|
||||
template<typename T> inline auto& operator &=(const T& value) { data = mask(data & value); return *this; }
|
||||
template<typename T> inline auto& operator ^=(const T& value) { data = mask(data ^ value); return *this; }
|
||||
template<typename T> inline auto& operator |=(const T& value) { data = mask(data | value); return *this; }
|
||||
template<typename T> inline auto& operator =(const T& value) { data = cast( value); return *this; }
|
||||
template<typename T> inline auto& operator *=(const T& value) { data = cast(data * value); return *this; }
|
||||
template<typename T> inline auto& operator /=(const T& value) { data = cast(data / value); return *this; }
|
||||
template<typename T> inline auto& operator %=(const T& value) { data = cast(data % value); return *this; }
|
||||
template<typename T> inline auto& operator +=(const T& value) { data = cast(data + value); return *this; }
|
||||
template<typename T> inline auto& operator -=(const T& value) { data = cast(data - value); return *this; }
|
||||
template<typename T> inline auto& operator<<=(const T& value) { data = cast(data << value); return *this; }
|
||||
template<typename T> inline auto& operator>>=(const T& value) { data = cast(data >> value); return *this; }
|
||||
template<typename T> inline auto& operator &=(const T& value) { data = cast(data & value); return *this; }
|
||||
template<typename T> inline auto& operator ^=(const T& value) { data = cast(data ^ value); return *this; }
|
||||
template<typename T> inline auto& operator |=(const T& value) { data = cast(data | value); return *this; }
|
||||
|
||||
inline auto operator()(int index) -> BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto operator()(int lo, int hi) -> BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int index) -> BitRange<Precision> { return {&data, index}; }
|
||||
inline auto bit(int index) const -> const BitRange<Precision> { return {&data, index}; }
|
||||
|
||||
inline auto operator()(int index) const -> const BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto operator()(int lo, int hi) const -> const BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int lo, int hi) -> BitRange<Precision> { return {&data, lo, hi}; }
|
||||
inline auto bit(int lo, int hi) const -> const BitRange<Precision> { return {&data, lo, hi}; }
|
||||
|
||||
inline auto bits(int lo, int hi) -> BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int index) -> BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto byte(int index) -> BitRange<Precision> { return {(utype&)data, index * 8 + 0, index * 8 + 7}; }
|
||||
inline auto byte(int index) -> BitRange<Precision> { return {&data, index * 8 + 0, index * 8 + 7}; }
|
||||
inline auto byte(int index) const -> const BitRange<Precision> { return {&data, index * 8 + 0, index * 8 + 7}; }
|
||||
|
||||
inline auto bits(int lo, int hi) const -> const BitRange<Precision> { return {(utype&)data, lo, hi}; }
|
||||
inline auto bit(int index) const -> const BitRange<Precision> { return {(utype&)data, index, index}; }
|
||||
inline auto byte(int index) const -> const BitRange<Precision> { return {(utype&)data, index * 8 + 0, index * 8 + 7}; }
|
||||
inline auto mask(int index) const -> utype {
|
||||
return data & 1 << index;
|
||||
}
|
||||
|
||||
inline auto mask(int lo, int hi) const -> utype {
|
||||
return data & (~0ull >> 64 - (hi - lo + 1) << lo);
|
||||
}
|
||||
|
||||
inline auto slice(int index) const { return Natural<>{bit(index)}; }
|
||||
inline auto slice(int lo, int hi) const { return Natural<>{bits(lo, hi)}; }
|
||||
inline auto slice(int lo, int hi) const { return Natural<>{bit(lo, hi)}; }
|
||||
|
||||
inline auto clamp(uint bits) -> utype {
|
||||
const uint64_t b = 1ull << (bits - 1);
|
||||
const uint64_t b = 1ull << bits - 1;
|
||||
const uint64_t m = b * 2 - 1;
|
||||
return data < m ? data : m;
|
||||
}
|
||||
|
||||
inline auto clip(uint bits) -> utype {
|
||||
const uint64_t b = 1ull << (bits - 1);
|
||||
const uint64_t b = 1ull << bits - 1;
|
||||
const uint64_t m = b * 2 - 1;
|
||||
return data & m;
|
||||
}
|
||||
@@ -71,7 +74,7 @@ template<uint Precision> struct Natural {
|
||||
inline auto integer() const -> Integer<Precision>;
|
||||
|
||||
private:
|
||||
inline auto mask(utype value) const -> utype {
|
||||
inline auto cast(utype value) const -> utype {
|
||||
return value & mask();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user