natural.hpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. namespace nall {
  3. template<uint Precision> struct Natural {
  4. static_assert(Precision >= 1 && Precision <= 64);
  5. static constexpr auto bits() -> uint { return Precision; }
  6. using utype =
  7. conditional_t<Precision <= 8, uint8_t,
  8. conditional_t<Precision <= 16, uint16_t,
  9. conditional_t<Precision <= 32, uint32_t,
  10. conditional_t<Precision <= 64, uint64_t,
  11. void>>>>;
  12. static constexpr auto mask() -> utype { return ~0ull >> 64 - Precision; }
  13. Natural() : data(0) {}
  14. template<uint Bits> Natural(Natural<Bits> value) { data = cast(value); }
  15. template<typename T> Natural(const T& value) { data = cast(value); }
  16. explicit Natural(const char* value) { data = cast(toNatural(value)); }
  17. operator utype() const { return data; }
  18. auto operator++(int) { auto value = *this; data = cast(data + 1); return value; }
  19. auto operator--(int) { auto value = *this; data = cast(data - 1); return value; }
  20. auto& operator++() { data = cast(data + 1); return *this; }
  21. auto& operator--() { data = cast(data - 1); return *this; }
  22. template<typename T> auto& operator =(const T& value) { data = cast( value); return *this; }
  23. template<typename T> auto& operator *=(const T& value) { data = cast(data * value); return *this; }
  24. template<typename T> auto& operator /=(const T& value) { data = cast(data / value); return *this; }
  25. template<typename T> auto& operator %=(const T& value) { data = cast(data % value); return *this; }
  26. template<typename T> auto& operator +=(const T& value) { data = cast(data + value); return *this; }
  27. template<typename T> auto& operator -=(const T& value) { data = cast(data - value); return *this; }
  28. template<typename T> auto& operator<<=(const T& value) { data = cast(data << value); return *this; }
  29. template<typename T> auto& operator>>=(const T& value) { data = cast(data >> value); return *this; }
  30. template<typename T> auto& operator &=(const T& value) { data = cast(data & value); return *this; }
  31. template<typename T> auto& operator ^=(const T& value) { data = cast(data ^ value); return *this; }
  32. template<typename T> auto& operator |=(const T& value) { data = cast(data | value); return *this; }
  33. auto bit(int index) -> DynamicBitRange<Natural> { return {*this, index}; }
  34. auto bit(int index) const -> const DynamicBitRange<Natural> { return {(Natural&)*this, index}; }
  35. auto bit(int lo, int hi) -> DynamicBitRange<Natural> { return {*this, lo, hi}; }
  36. auto bit(int lo, int hi) const -> const DynamicBitRange<Natural> { return {(Natural&)*this, lo, hi}; }
  37. auto byte(int index) -> DynamicBitRange<Natural> { return {*this, index * 8 + 0, index * 8 + 7}; }
  38. auto byte(int index) const -> const DynamicBitRange<Natural> { return {(Natural&)*this, index * 8 + 0, index * 8 + 7}; }
  39. auto mask(int index) const -> utype {
  40. return data & 1 << index;
  41. }
  42. auto mask(int lo, int hi) const -> utype {
  43. return data & (~0ull >> 64 - (hi - lo + 1) << lo);
  44. }
  45. auto slice(int index) const { return Natural<>{bit(index)}; }
  46. auto slice(int lo, int hi) const { return Natural<>{bit(lo, hi)}; }
  47. auto clamp(uint bits) -> utype {
  48. const uint64_t b = 1ull << bits - 1;
  49. const uint64_t m = b * 2 - 1;
  50. return data < m ? data : m;
  51. }
  52. auto clip(uint bits) -> utype {
  53. const uint64_t b = 1ull << bits - 1;
  54. const uint64_t m = b * 2 - 1;
  55. return data & m;
  56. }
  57. auto serialize(serializer& s) { s(data); }
  58. auto integer() const -> Integer<Precision>;
  59. private:
  60. auto cast(utype value) const -> utype {
  61. return value & mask();
  62. }
  63. utype data;
  64. };
  65. }