mod.rs 7.0 KB

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