#pragma once namespace nall { template struct Natural { static_assert(Precision >= 1 && Precision <= 64); static inline constexpr auto bits() -> uint { return Precision; } using utype = conditional_t>>>; static inline constexpr auto mask() -> utype { return ~0ull >> 64 - bits(); } inline Natural() : data(0) {} template inline Natural(Natural value) { data = mask(value); } template inline Natural(const T& value) { data = mask(value); } explicit inline operator bool() const { return data; } inline operator uint64_t() 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++() { data = mask(data + 1); return *this; } inline auto& operator--() { data = mask(data - 1); return *this; } inline auto operator!() const { return Natural{!data}; } inline auto operator~() const { return Natural{~data}; } inline auto operator+() const { return Natural<>{+(uint64_t)data}; } inline auto operator-() const { return Natural<>{-(uint64_t)data}; } #define lhs data #define rhs value template inline auto& operator =(const T& value) { lhs = mask( rhs); return *this; } template inline auto& operator *=(const T& value) { lhs = mask(lhs * rhs); return *this; } template inline auto& operator /=(const T& value) { lhs = mask(lhs / rhs); return *this; } template inline auto& operator %=(const T& value) { lhs = mask(lhs % rhs); return *this; } template inline auto& operator +=(const T& value) { lhs = mask(lhs + rhs); return *this; } template inline auto& operator -=(const T& value) { lhs = mask(lhs - rhs); return *this; } template inline auto& operator<<=(const T& value) { lhs = mask(lhs << rhs); return *this; } template inline auto& operator>>=(const T& value) { lhs = mask(lhs >> rhs); return *this; } template inline auto& operator &=(const T& value) { lhs = mask(lhs & rhs); return *this; } template inline auto& operator ^=(const T& value) { lhs = mask(lhs ^ rhs); return *this; } template inline auto& operator |=(const T& value) { lhs = mask(lhs | rhs); return *this; } #undef lhs #undef rhs //warning: this does not and cannot short-circuit; value is always evaluated template inline auto orElse(const T& value) { return Natural<>{data ? data : value}; } inline auto bits(int lo, int hi) -> BitRange { return {(utype&)data, lo, hi}; } inline auto bit(int index) -> BitRange { return {(utype&)data, index, index}; } inline auto byte(int index) -> BitRange { return {(utype&)data, index * 8 + 0, index * 8 + 7}; } inline auto bits(int lo, int hi) const -> const BitRange { return {(utype&)data, lo, hi}; } inline auto bit(int index) const -> const BitRange { return {(utype&)data, index, index}; } inline auto byte(int index) const -> const BitRange { return {(utype&)data, index * 8 + 0, index * 8 + 7}; } 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 clamp(uint bits) { const uint64_t b = 1ull << (bits - 1); const uint64_t m = b * 2 - 1; return Natural<>{data < m ? data : m}; } inline auto clip(uint bits) { const uint64_t b = 1ull << (bits - 1); const uint64_t m = b * 2 - 1; return Natural<>{data & m}; } inline auto serialize(serializer& s) { s(data); } inline auto integer() const -> Integer; private: inline auto mask(utype value) const -> utype { return value & mask(); } utype data; }; #define lhs (uint64_t)l #define rhs r template inline auto operator *(Natural l, Natural r) { return Natural{lhs * rhs}; } template inline auto operator /(Natural l, Natural r) { return Natural{lhs / rhs}; } template inline auto operator %(Natural l, Natural r) { return Natural{lhs % rhs}; } template inline auto operator +(Natural l, Natural r) { return Natural{lhs + rhs}; } template inline auto operator -(Natural l, Natural r) { return Natural{lhs - rhs}; } template inline auto operator<<(Natural l, Natural r) { return Natural{lhs << rhs}; } template inline auto operator>>(Natural l, Natural r) { return Natural{lhs >> rhs}; } template inline auto operator &(Natural l, Natural r) { return Natural{lhs & rhs}; } template inline auto operator ^(Natural l, Natural r) { return Natural{lhs ^ rhs}; } template inline auto operator |(Natural l, Natural r) { return Natural{lhs | rhs}; } #undef lhs #undef rhs }