mod.rs 6.0 KB

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