sdiv.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. use int::Int;
  2. macro_rules! div {
  3. ($intrinsic:ident: $ty:ty, $uty:ty) => {
  4. div!($intrinsic: $ty, $uty, $ty, |i| {i});
  5. };
  6. ($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => {
  7. /// Returns `a / b`
  8. #[cfg_attr(not(test), no_mangle)]
  9. pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
  10. let s_a = a >> (<$ty>::bits() - 1);
  11. let s_b = b >> (<$ty>::bits() - 1);
  12. // NOTE it's OK to overflow here because of the `as $uty` cast below
  13. // This whole operation is computing the absolute value of the inputs
  14. // So some overflow will happen when dealing with e.g. `i64::MIN`
  15. // where the absolute value is `(-i64::MIN) as u64`
  16. let a = (a ^ s_a).wrapping_sub(s_a);
  17. let b = (b ^ s_b).wrapping_sub(s_b);
  18. let s = s_a ^ s_b;
  19. let r = udiv!(a as $uty, b as $uty);
  20. ($conv)((r as $ty ^ s) - s)
  21. }
  22. }
  23. }
  24. macro_rules! mod_ {
  25. ($intrinsic:ident: $ty:ty, $uty:ty) => {
  26. mod_!($intrinsic: $ty, $uty, $ty, |i| {i});
  27. };
  28. ($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => {
  29. /// Returns `a % b`
  30. #[cfg_attr(not(test), no_mangle)]
  31. pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
  32. let s = b >> (<$ty>::bits() - 1);
  33. let b = (b ^ s) - s;
  34. let s = a >> (<$ty>::bits() - 1);
  35. let a = (a ^ s) - s;
  36. let r = urem!(a as $uty, b as $uty);
  37. ($conv)((r as $ty ^ s) - s)
  38. }
  39. }
  40. }
  41. macro_rules! divmod {
  42. ($abi:tt, $intrinsic:ident, $div:ident: $ty:ty) => {
  43. /// Returns `a / b` and sets `*rem = n % d`
  44. #[cfg_attr(not(test), no_mangle)]
  45. pub extern $abi fn $intrinsic(a: $ty, b: $ty, rem: &mut $ty) -> $ty {
  46. #[cfg(all(feature = "c", any(target_arch = "x86")))]
  47. extern {
  48. fn $div(a: $ty, b: $ty) -> $ty;
  49. }
  50. let r = match () {
  51. #[cfg(not(all(feature = "c", any(target_arch = "x86"))))]
  52. () => $div(a, b),
  53. #[cfg(all(feature = "c", any(target_arch = "x86")))]
  54. () => unsafe { $div(a, b) },
  55. };
  56. // NOTE won't overflow because it's using the result from the
  57. // previous division
  58. *rem = a - r.wrapping_mul(b);
  59. r
  60. }
  61. }
  62. }
  63. #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
  64. div!(__divsi3: i32, u32);
  65. #[cfg(not(all(feature = "c", target_arch = "x86")))]
  66. div!(__divdi3: i64, u64);
  67. #[cfg(not(all(windows, target_pointer_width="64")))]
  68. div!(__divti3: i128, u128);
  69. #[cfg(all(windows, target_pointer_width="64"))]
  70. div!(__divti3: i128, u128, ::U64x2, ::sconv);
  71. #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
  72. mod_!(__modsi3: i32, u32);
  73. #[cfg(not(all(feature = "c", target_arch = "x86")))]
  74. mod_!(__moddi3: i64, u64);
  75. #[cfg(not(all(windows, target_pointer_width="64")))]
  76. mod_!(__modti3: i128, u128);
  77. #[cfg(all(windows, target_pointer_width="64"))]
  78. mod_!(__modti3: i128, u128, ::U64x2, ::sconv);
  79. #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
  80. divmod!("C", __divmodsi4, __divsi3: i32);
  81. #[cfg(target_arch = "arm")]
  82. divmod!("aapcs", __divmoddi4, __divdi3: i64);
  83. #[cfg(not(target_arch = "arm"))]
  84. divmod!("C", __divmoddi4, __divdi3: i64);
  85. #[cfg(test)]
  86. mod tests {
  87. use qc::{U32, U64};
  88. check! {
  89. fn __divdi3(f: extern "C" fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
  90. let (n, d) = (n.0 as i64, d.0 as i64);
  91. if d == 0 {
  92. None
  93. } else {
  94. Some(f(n, d))
  95. }
  96. }
  97. fn __moddi3(f: extern "C" fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
  98. let (n, d) = (n.0 as i64, d.0 as i64);
  99. if d == 0 {
  100. None
  101. } else {
  102. Some(f(n, d))
  103. }
  104. }
  105. #[cfg(target_arch = "arm")]
  106. fn __divmoddi4(f: extern "aapcs" fn(i64, i64, &mut i64) -> i64,
  107. n: U64,
  108. d: U64) -> Option<(i64, i64)> {
  109. let (n, d) = (n.0 as i64, d.0 as i64);
  110. if d == 0 {
  111. None
  112. } else {
  113. let mut r = 0;
  114. let q = f(n, d, &mut r);
  115. Some((q, r))
  116. }
  117. }
  118. #[cfg(not(target_arch = "arm"))]
  119. fn __divmoddi4(f: extern "C" fn(i64, i64, &mut i64) -> i64,
  120. n: U64,
  121. d: U64) -> Option<(i64, i64)> {
  122. let (n, d) = (n.0 as i64, d.0 as i64);
  123. if d == 0 {
  124. None
  125. } else {
  126. let mut r = 0;
  127. let q = f(n, d, &mut r);
  128. Some((q, r))
  129. }
  130. }
  131. fn __divsi3(f: extern "C" fn(i32, i32) -> i32,
  132. n: U32,
  133. d: U32) -> Option<i32> {
  134. let (n, d) = (n.0 as i32, d.0 as i32);
  135. if d == 0 {
  136. None
  137. } else {
  138. Some(f(n, d))
  139. }
  140. }
  141. fn __modsi3(f: extern "C" fn(i32, i32) -> i32,
  142. n: U32,
  143. d: U32) -> Option<i32> {
  144. let (n, d) = (n.0 as i32, d.0 as i32);
  145. if d == 0 {
  146. None
  147. } else {
  148. Some(f(n, d))
  149. }
  150. }
  151. fn __divmodsi4(f: extern "C" fn(i32, i32, &mut i32) -> i32,
  152. n: U32,
  153. d: U32) -> Option<(i32, i32)> {
  154. let (n, d) = (n.0 as i32, d.0 as i32);
  155. if d == 0 {
  156. None
  157. } else {
  158. let mut r = 0;
  159. let q = f(n, d, &mut r);
  160. Some((q, r))
  161. }
  162. }
  163. }
  164. }
  165. #[cfg(test)]
  166. #[cfg(all(not(windows),
  167. not(target_arch = "mips64"),
  168. not(target_arch = "mips64el"),
  169. target_pointer_width="64"))]
  170. mod tests_i128 {
  171. use qc::U128;
  172. check! {
  173. fn __divti3(f: extern "C" fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
  174. let (n, d) = (n.0 as i128, d.0 as i128);
  175. if d == 0 {
  176. None
  177. } else {
  178. Some(f(n, d))
  179. }
  180. }
  181. fn __modti3(f: extern "C" fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
  182. let (n, d) = (n.0 as i128, d.0 as i128);
  183. if d == 0 {
  184. None
  185. } else {
  186. Some(f(n, d))
  187. }
  188. }
  189. }
  190. }