natural.hpp 3.7 KB

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