addsub.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. use int::Int;
  2. use int::LargeInt;
  3. trait UAddSub: LargeInt {
  4. fn uadd(self, other: Self) -> Self {
  5. let (low, carry) = self.low().overflowing_add(other.low());
  6. let high = self.high().wrapping_add(other.high());
  7. let carry = if carry {
  8. Self::HighHalf::ONE
  9. } else {
  10. Self::HighHalf::ZERO
  11. };
  12. Self::from_parts(low, high.wrapping_add(carry))
  13. }
  14. fn uadd_one(self) -> Self {
  15. let (low, carry) = self.low().overflowing_add(Self::LowHalf::ONE);
  16. let carry = if carry {
  17. Self::HighHalf::ONE
  18. } else {
  19. Self::HighHalf::ZERO
  20. };
  21. Self::from_parts(low, self.high().wrapping_add(carry))
  22. }
  23. fn usub(self, other: Self) -> Self {
  24. let uneg = (!other).uadd_one();
  25. self.uadd(uneg)
  26. }
  27. }
  28. impl UAddSub for u128 {}
  29. trait AddSub: Int
  30. where
  31. <Self as Int>::UnsignedInt: UAddSub,
  32. {
  33. fn add(self, other: Self) -> Self {
  34. Self::from_unsigned(self.unsigned().uadd(other.unsigned()))
  35. }
  36. fn sub(self, other: Self) -> Self {
  37. Self::from_unsigned(self.unsigned().usub(other.unsigned()))
  38. }
  39. }
  40. impl AddSub for u128 {}
  41. impl AddSub for i128 {}
  42. trait Addo: AddSub
  43. where
  44. <Self as Int>::UnsignedInt: UAddSub,
  45. {
  46. fn addo(self, other: Self, overflow: &mut i32) -> Self {
  47. *overflow = 0;
  48. let result = AddSub::add(self, other);
  49. if other >= Self::ZERO {
  50. if result < self {
  51. *overflow = 1;
  52. }
  53. } else {
  54. if result >= self {
  55. *overflow = 1;
  56. }
  57. }
  58. result
  59. }
  60. }
  61. impl Addo for i128 {}
  62. impl Addo for u128 {}
  63. trait Subo: AddSub
  64. where
  65. <Self as Int>::UnsignedInt: UAddSub,
  66. {
  67. fn subo(self, other: Self, overflow: &mut i32) -> Self {
  68. *overflow = 0;
  69. let result = AddSub::sub(self, other);
  70. if other >= Self::ZERO {
  71. if result > self {
  72. *overflow = 1;
  73. }
  74. } else {
  75. if result <= self {
  76. *overflow = 1;
  77. }
  78. }
  79. result
  80. }
  81. }
  82. impl Subo for i128 {}
  83. impl Subo for u128 {}
  84. u128_lang_items! {
  85. #[lang = "i128_add"]
  86. pub fn rust_i128_add(a: i128, b: i128) -> i128 {
  87. rust_u128_add(a as _, b as _) as _
  88. }
  89. #[lang = "i128_addo"]
  90. pub fn rust_i128_addo(a: i128, b: i128) -> (i128, bool) {
  91. let mut oflow = 0;
  92. let r = a.addo(b, &mut oflow);
  93. (r, oflow != 0)
  94. }
  95. #[lang = "u128_add"]
  96. pub fn rust_u128_add(a: u128, b: u128) -> u128 {
  97. a.add(b)
  98. }
  99. #[lang = "u128_addo"]
  100. pub fn rust_u128_addo(a: u128, b: u128) -> (u128, bool) {
  101. let mut oflow = 0;
  102. let r = a.addo(b, &mut oflow);
  103. (r, oflow != 0)
  104. }
  105. #[lang = "i128_sub"]
  106. pub fn rust_i128_sub(a: i128, b: i128) -> i128 {
  107. rust_u128_sub(a as _, b as _) as _
  108. }
  109. #[lang = "i128_subo"]
  110. pub fn rust_i128_subo(a: i128, b: i128) -> (i128, bool) {
  111. let mut oflow = 0;
  112. let r = a.subo(b, &mut oflow);
  113. (r, oflow != 0)
  114. }
  115. #[lang = "u128_sub"]
  116. pub fn rust_u128_sub(a: u128, b: u128) -> u128 {
  117. a.sub(b)
  118. }
  119. #[lang = "u128_subo"]
  120. pub fn rust_u128_subo(a: u128, b: u128) -> (u128, bool) {
  121. let mut oflow = 0;
  122. let r = a.subo(b, &mut oflow);
  123. (r, oflow != 0)
  124. }
  125. }