arithmetic.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. //multi-precision arithmetic
  3. //warning: each size is quadratically more expensive than the size before it!
  4. #include <nall/stdint.hpp>
  5. #include <nall/string.hpp>
  6. #include <nall/range.hpp>
  7. #include <nall/traits.hpp>
  8. #include <nall/arithmetic/unsigned.hpp>
  9. namespace nall {
  10. template<uint Bits> struct ArithmeticNatural;
  11. template<> struct ArithmeticNatural< 8> { using type = uint8_t; };
  12. template<> struct ArithmeticNatural< 16> { using type = uint16_t; };
  13. template<> struct ArithmeticNatural< 32> { using type = uint32_t; };
  14. template<> struct ArithmeticNatural< 64> { using type = uint64_t; };
  15. #if INTMAX_BITS >= 128
  16. template<> struct ArithmeticNatural<128> { using type = uint128_t; };
  17. #endif
  18. }
  19. #if INTMAX_BITS < 128
  20. #define PairBits 128
  21. #define TypeBits 64
  22. #define HalfBits 32
  23. #include <nall/arithmetic/natural.hpp>
  24. #undef PairBits
  25. #undef TypeBits
  26. #undef HalfBits
  27. #endif
  28. #define PairBits 256
  29. #define TypeBits 128
  30. #define HalfBits 64
  31. #include <nall/arithmetic/natural.hpp>
  32. #undef PairBits
  33. #undef TypeBits
  34. #undef HalfBits
  35. #define PairBits 512
  36. #define TypeBits 256
  37. #define HalfBits 128
  38. #include <nall/arithmetic/natural.hpp>
  39. #undef PairBits
  40. #undef TypeBits
  41. #undef HalfBits
  42. #define PairBits 1024
  43. #define TypeBits 512
  44. #define HalfBits 256
  45. #include <nall/arithmetic/natural.hpp>
  46. #undef PairBits
  47. #undef TypeBits
  48. #undef HalfBits
  49. #define PairBits 2048
  50. #define TypeBits 1024
  51. #define HalfBits 512
  52. #include <nall/arithmetic/natural.hpp>
  53. #undef PairBits
  54. #undef TypeBits
  55. #undef HalfBits
  56. #define PairBits 4096
  57. #define TypeBits 2048
  58. #define HalfBits 1024
  59. #include <nall/arithmetic/natural.hpp>
  60. #undef PairBits
  61. #undef TypeBits
  62. #undef HalfBits
  63. #define PairBits 8192
  64. #define TypeBits 4096
  65. #define HalfBits 2048
  66. #include <nall/arithmetic/natural.hpp>
  67. #undef PairBits
  68. #undef TypeBits
  69. #undef HalfBits
  70. namespace nall {
  71. //TODO: these types are for expressing smaller bit ranges in class interfaces
  72. //for instance, XChaCha20 taking a 192-bit nonce
  73. //however, they still allow more bits than expressed ...
  74. //some sort of wrapper needs to be devised to ensure these sizes are masked and wrap appropriately
  75. using uint192_t = uint256_t;
  76. }