mod.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. use core::ops;
  2. macro_rules! hty {
  3. ($ty:ty) => {
  4. <$ty as LargeInt>::HighHalf
  5. }
  6. }
  7. macro_rules! os_ty {
  8. ($ty:ty) => {
  9. <$ty as Int>::OtherSign
  10. }
  11. }
  12. pub mod mul;
  13. pub mod sdiv;
  14. pub mod shift;
  15. pub mod udiv;
  16. /// Trait for some basic operations on integers
  17. pub trait Int:
  18. Copy +
  19. PartialEq +
  20. PartialOrd +
  21. ops::AddAssign +
  22. ops::Add<Output = Self> +
  23. ops::Sub<Output = Self> +
  24. ops::Div<Output = Self> +
  25. ops::Shl<u32, Output = Self> +
  26. ops::Shr<u32, Output = Self> +
  27. ops::BitOr<Output = Self> +
  28. ops::BitXor<Output = Self> +
  29. ops::BitAnd<Output = Self> +
  30. ops::BitAndAssign +
  31. ops::Not<Output = Self> +
  32. {
  33. /// Type with the same width but other signedness
  34. type OtherSign: Int;
  35. /// Unsigned version of Self
  36. type UnsignedInt: Int;
  37. /// Returns the bitwidth of the int type
  38. fn bits() -> u32;
  39. fn zero() -> Self;
  40. fn one() -> Self;
  41. /// Extracts the sign from self and returns a tuple.
  42. ///
  43. /// # Examples
  44. ///
  45. /// ```rust,ignore
  46. /// let i = -25_i32;
  47. /// let (sign, u) = i.extract_sign();
  48. /// assert_eq!(sign, true);
  49. /// assert_eq!(u, 25_u32);
  50. /// ```
  51. fn extract_sign(self) -> (bool, Self::UnsignedInt);
  52. fn unsigned(self) -> Self::UnsignedInt;
  53. fn from_unsigned(unsigned: Self::UnsignedInt) -> Self;
  54. // copied from primitive integers, but put in a trait
  55. fn max_value() -> Self;
  56. fn min_value() -> Self;
  57. fn wrapping_add(self, other: Self) -> Self;
  58. fn wrapping_mul(self, other: Self) -> Self;
  59. fn wrapping_sub(self, other: Self) -> Self;
  60. fn aborting_div(self, other: Self) -> Self;
  61. fn aborting_rem(self, other: Self) -> Self;
  62. }
  63. fn unwrap<T>(t: Option<T>) -> T {
  64. match t {
  65. Some(t) => t,
  66. None => ::abort(),
  67. }
  68. }
  69. macro_rules! int_impl {
  70. ($ity:ty, $uty:ty, $bits:expr) => {
  71. impl Int for $uty {
  72. type OtherSign = $ity;
  73. type UnsignedInt = $uty;
  74. fn zero() -> Self {
  75. 0
  76. }
  77. fn one() -> Self {
  78. 1
  79. }
  80. fn bits() -> u32 {
  81. $bits
  82. }
  83. fn extract_sign(self) -> (bool, $uty) {
  84. (false, self)
  85. }
  86. fn unsigned(self) -> $uty {
  87. self
  88. }
  89. fn from_unsigned(me: $uty) -> Self {
  90. me
  91. }
  92. fn max_value() -> Self {
  93. <Self>::max_value()
  94. }
  95. fn min_value() -> Self {
  96. <Self>::min_value()
  97. }
  98. fn wrapping_add(self, other: Self) -> Self {
  99. <Self>::wrapping_add(self, other)
  100. }
  101. fn wrapping_mul(self, other: Self) -> Self {
  102. <Self>::wrapping_mul(self, other)
  103. }
  104. fn wrapping_sub(self, other: Self) -> Self {
  105. <Self>::wrapping_sub(self, other)
  106. }
  107. fn aborting_div(self, other: Self) -> Self {
  108. unwrap(<Self>::checked_div(self, other))
  109. }
  110. fn aborting_rem(self, other: Self) -> Self {
  111. unwrap(<Self>::checked_rem(self, other))
  112. }
  113. }
  114. impl Int for $ity {
  115. type OtherSign = $uty;
  116. type UnsignedInt = $uty;
  117. fn bits() -> u32 {
  118. $bits
  119. }
  120. fn zero() -> Self {
  121. 0
  122. }
  123. fn one() -> Self {
  124. 1
  125. }
  126. fn extract_sign(self) -> (bool, $uty) {
  127. if self < 0 {
  128. (true, (!(self as $uty)).wrapping_add(1))
  129. } else {
  130. (false, self as $uty)
  131. }
  132. }
  133. fn unsigned(self) -> $uty {
  134. self as $uty
  135. }
  136. fn from_unsigned(me: $uty) -> Self {
  137. me as $ity
  138. }
  139. fn max_value() -> Self {
  140. <Self>::max_value()
  141. }
  142. fn min_value() -> Self {
  143. <Self>::min_value()
  144. }
  145. fn wrapping_add(self, other: Self) -> Self {
  146. <Self>::wrapping_add(self, other)
  147. }
  148. fn wrapping_mul(self, other: Self) -> Self {
  149. <Self>::wrapping_mul(self, other)
  150. }
  151. fn wrapping_sub(self, other: Self) -> Self {
  152. <Self>::wrapping_sub(self, other)
  153. }
  154. fn aborting_div(self, other: Self) -> Self {
  155. unwrap(<Self>::checked_div(self, other))
  156. }
  157. fn aborting_rem(self, other: Self) -> Self {
  158. unwrap(<Self>::checked_rem(self, other))
  159. }
  160. }
  161. }
  162. }
  163. int_impl!(i32, u32, 32);
  164. int_impl!(i64, u64, 64);
  165. int_impl!(i128, u128, 128);
  166. /// Trait to convert an integer to/from smaller parts
  167. pub trait LargeInt: Int {
  168. type LowHalf: Int;
  169. type HighHalf: Int;
  170. fn low(self) -> Self::LowHalf;
  171. fn low_as_high(low: Self::LowHalf) -> Self::HighHalf;
  172. fn high(self) -> Self::HighHalf;
  173. fn high_as_low(low: Self::HighHalf) -> Self::LowHalf;
  174. fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self;
  175. }
  176. macro_rules! large_int {
  177. ($ty:ty, $tylow:ty, $tyhigh:ty, $halfbits:expr) => {
  178. impl LargeInt for $ty {
  179. type LowHalf = $tylow;
  180. type HighHalf = $tyhigh;
  181. fn low(self) -> $tylow {
  182. self as $tylow
  183. }
  184. fn low_as_high(low: $tylow) -> $tyhigh {
  185. low as $tyhigh
  186. }
  187. fn high(self) -> $tyhigh {
  188. (self >> $halfbits) as $tyhigh
  189. }
  190. fn high_as_low(high: $tyhigh) -> $tylow {
  191. high as $tylow
  192. }
  193. fn from_parts(low: $tylow, high: $tyhigh) -> $ty {
  194. low as $ty | ((high as $ty) << $halfbits)
  195. }
  196. }
  197. }
  198. }
  199. large_int!(u64, u32, u32, 32);
  200. large_int!(i64, u32, i32, 32);
  201. large_int!(u128, u64, u64, 64);
  202. large_int!(i128, u64, i64, 64);