mod.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. pub mod mul;
  2. pub mod sdiv;
  3. pub mod shift;
  4. pub mod udiv;
  5. /// Trait for some basic operations on integers
  6. pub trait Int {
  7. /// Returns the bitwidth of the int type
  8. fn bits() -> u32;
  9. }
  10. // TODO: Once i128/u128 support lands, we'll want to add impls for those as well
  11. impl Int for u32 {
  12. fn bits() -> u32 {
  13. 32
  14. }
  15. }
  16. impl Int for i32 {
  17. fn bits() -> u32 {
  18. 32
  19. }
  20. }
  21. impl Int for u64 {
  22. fn bits() -> u32 {
  23. 64
  24. }
  25. }
  26. impl Int for i64 {
  27. fn bits() -> u32 {
  28. 64
  29. }
  30. }
  31. /// Trait to convert an integer to/from smaller parts
  32. pub trait LargeInt {
  33. type LowHalf;
  34. type HighHalf;
  35. fn low(self) -> Self::LowHalf;
  36. fn high(self) -> Self::HighHalf;
  37. fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self;
  38. }
  39. // TODO: Once i128/u128 support lands, we'll want to add impls for those as well
  40. impl LargeInt for u64 {
  41. type LowHalf = u32;
  42. type HighHalf = u32;
  43. fn low(self) -> u32 {
  44. self as u32
  45. }
  46. fn high(self) -> u32 {
  47. (self >> 32) as u32
  48. }
  49. fn from_parts(low: u32, high: u32) -> u64 {
  50. low as u64 | ((high as u64) << 32)
  51. }
  52. }
  53. impl LargeInt for i64 {
  54. type LowHalf = u32;
  55. type HighHalf = i32;
  56. fn low(self) -> u32 {
  57. self as u32
  58. }
  59. fn high(self) -> i32 {
  60. (self >> 32) as i32
  61. }
  62. fn from_parts(low: u32, high: i32) -> i64 {
  63. low as i64 | ((high as i64) << 32)
  64. }
  65. }