sdiv.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use int::Int;
  2. trait Div: Int {
  3. /// Returns `a / b`
  4. fn div(self, other: Self) -> Self {
  5. let s_a = self >> (Self::BITS - 1);
  6. let s_b = other >> (Self::BITS - 1);
  7. // NOTE it's OK to overflow here because of the `.unsigned()` below.
  8. // This whole operation is computing the absolute value of the inputs
  9. // So some overflow will happen when dealing with e.g. `i64::MIN`
  10. // where the absolute value is `(-i64::MIN) as u64`
  11. let a = (self ^ s_a).wrapping_sub(s_a);
  12. let b = (other ^ s_b).wrapping_sub(s_b);
  13. let s = s_a ^ s_b;
  14. let r = a.unsigned().aborting_div(b.unsigned());
  15. (Self::from_unsigned(r) ^ s) - s
  16. }
  17. }
  18. impl Div for i32 {}
  19. impl Div for i64 {}
  20. impl Div for i128 {}
  21. trait Mod: Int {
  22. /// Returns `a % b`
  23. fn mod_(self, other: Self) -> Self {
  24. let s = other >> (Self::BITS - 1);
  25. // NOTE(wrapping_sub) see comment in the `div`
  26. let b = (other ^ s).wrapping_sub(s);
  27. let s = self >> (Self::BITS - 1);
  28. let a = (self ^ s).wrapping_sub(s);
  29. let r = a.unsigned().aborting_rem(b.unsigned());
  30. (Self::from_unsigned(r) ^ s) - s
  31. }
  32. }
  33. impl Mod for i32 {}
  34. impl Mod for i64 {}
  35. impl Mod for i128 {}
  36. trait Divmod: Int {
  37. /// Returns `a / b` and sets `*rem = n % d`
  38. fn divmod<F>(self, other: Self, rem: &mut Self, div: F) -> Self
  39. where
  40. F: Fn(Self, Self) -> Self,
  41. {
  42. let r = div(self, other);
  43. // NOTE won't overflow because it's using the result from the
  44. // previous division
  45. *rem = self - r.wrapping_mul(other);
  46. r
  47. }
  48. }
  49. impl Divmod for i32 {}
  50. impl Divmod for i64 {}
  51. intrinsics! {
  52. #[maybe_use_optimized_c_shim]
  53. #[arm_aeabi_alias = __aeabi_idiv]
  54. /// Returns `n / d`
  55. pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
  56. a.div(b)
  57. }
  58. #[maybe_use_optimized_c_shim]
  59. /// Returns `n % d`
  60. pub extern "C" fn __modsi3(a: i32, b: i32) -> i32 {
  61. a.mod_(b)
  62. }
  63. #[maybe_use_optimized_c_shim]
  64. /// Returns `n / d` and sets `*rem = n % d`
  65. pub extern "C" fn __divmodsi4(a: i32, b: i32, rem: &mut i32) -> i32 {
  66. a.divmod(b, rem, |a, b| __divsi3(a, b))
  67. }
  68. #[maybe_use_optimized_c_shim]
  69. /// Returns `n / d`
  70. pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 {
  71. a.div(b)
  72. }
  73. #[maybe_use_optimized_c_shim]
  74. /// Returns `n % d`
  75. pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 {
  76. a.mod_(b)
  77. }
  78. #[aapcs_on_arm]
  79. /// Returns `n / d` and sets `*rem = n % d`
  80. pub extern "C" fn __divmoddi4(a: i64, b: i64, rem: &mut i64) -> i64 {
  81. a.divmod(b, rem, |a, b| __divdi3(a, b))
  82. }
  83. #[win64_128bit_abi_hack]
  84. /// Returns `n / d`
  85. pub extern "C" fn __divti3(a: i128, b: i128) -> i128 {
  86. a.div(b)
  87. }
  88. #[win64_128bit_abi_hack]
  89. /// Returns `n % d`
  90. pub extern "C" fn __modti3(a: i128, b: i128) -> i128 {
  91. a.mod_(b)
  92. }
  93. // LLVM does not currently have a `__divmodti4` function
  94. }