sdiv.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 F: Fn(Self, Self) -> Self,
  40. {
  41. let r = div(self, other);
  42. // NOTE won't overflow because it's using the result from the
  43. // previous division
  44. *rem = self - r.wrapping_mul(other);
  45. r
  46. }
  47. }
  48. impl Divmod for i32 {}
  49. impl Divmod for i64 {}
  50. intrinsics! {
  51. #[arm_aeabi_alias = __aeabi_idiv]
  52. pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
  53. a.div(b)
  54. }
  55. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  56. pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 {
  57. a.div(b)
  58. }
  59. #[win64_128bit_abi_hack]
  60. pub extern "C" fn __divti3(a: i128, b: i128) -> i128 {
  61. a.div(b)
  62. }
  63. #[use_c_shim_if(all(target_arch = "arm",
  64. not(target_os = "ios"),
  65. not(target_env = "msvc"),
  66. not(thumb_1)))]
  67. pub extern "C" fn __modsi3(a: i32, b: i32) -> i32 {
  68. a.mod_(b)
  69. }
  70. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  71. pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 {
  72. a.mod_(b)
  73. }
  74. #[win64_128bit_abi_hack]
  75. pub extern "C" fn __modti3(a: i128, b: i128) -> i128 {
  76. a.mod_(b)
  77. }
  78. #[use_c_shim_if(all(target_arch = "arm", not(target_env = "msvc"),
  79. not(target_os = "ios"), not(thumb_1)))]
  80. pub extern "C" fn __divmodsi4(a: i32, b: i32, rem: &mut i32) -> i32 {
  81. a.divmod(b, rem, |a, b| __divsi3(a, b))
  82. }
  83. #[aapcs_on_arm]
  84. pub extern "C" fn __divmoddi4(a: i64, b: i64, rem: &mut i64) -> i64 {
  85. a.divmod(b, rem, |a, b| __divdi3(a, b))
  86. }
  87. }
  88. u128_lang_items! {
  89. #[lang = "i128_div"]
  90. pub fn rust_i128_div(a: i128, b: i128) -> i128 {
  91. __divti3(a, b)
  92. }
  93. #[lang = "i128_rem"]
  94. pub fn rust_i128_rem(a: i128, b: i128) -> i128 {
  95. __modti3(a, b)
  96. }
  97. }