sdiv.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios"), not(thumb_1)))]
  53. #[arm_aeabi_alias = __aeabi_idiv]
  54. pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
  55. a.div(b)
  56. }
  57. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  58. pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 {
  59. a.div(b)
  60. }
  61. #[win64_128bit_abi_hack]
  62. pub extern "C" fn __divti3(a: i128, b: i128) -> i128 {
  63. a.div(b)
  64. }
  65. #[use_c_shim_if(all(target_arch = "arm",
  66. not(target_os = "ios"),
  67. not(target_env = "msvc"),
  68. not(thumb_1)))]
  69. pub extern "C" fn __modsi3(a: i32, b: i32) -> i32 {
  70. a.mod_(b)
  71. }
  72. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  73. pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 {
  74. a.mod_(b)
  75. }
  76. #[win64_128bit_abi_hack]
  77. pub extern "C" fn __modti3(a: i128, b: i128) -> i128 {
  78. a.mod_(b)
  79. }
  80. #[use_c_shim_if(all(target_arch = "arm", not(target_env = "msvc"),
  81. not(target_os = "ios"), not(thumb_1)))]
  82. pub extern "C" fn __divmodsi4(a: i32, b: i32, rem: &mut i32) -> i32 {
  83. a.divmod(b, rem, |a, b| __divsi3(a, b))
  84. }
  85. #[aapcs_on_arm]
  86. pub extern "C" fn __divmoddi4(a: i64, b: i64, rem: &mut i64) -> i64 {
  87. a.divmod(b, rem, |a, b| __divdi3(a, b))
  88. }
  89. }
  90. u128_lang_items! {
  91. #[lang = "i128_div"]
  92. pub fn rust_i128_div(a: i128, b: i128) -> i128 {
  93. __divti3(a, b)
  94. }
  95. #[lang = "i128_rem"]
  96. pub fn rust_i128_rem(a: i128, b: i128) -> i128 {
  97. __modti3(a, b)
  98. }
  99. }