addsub.rs 3.2 KB

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