sdiv.rs 6.1 KB

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