integer.hpp 3.6 KB

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