sdiv.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ($intrinsic:ident, $div:ident: $ty:ty) => {
  39. /// Returns `a / b` and sets `*rem = n % d`
  40. #[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)]
  41. #[cfg_attr(all(not(test), target_arch = "arm"), inline(always))]
  42. pub extern "C" fn $intrinsic(a: $ty, b: $ty, rem: &mut $ty) -> $ty {
  43. #[cfg(all(feature = "c", any(target_arch = "x86")))]
  44. extern {
  45. fn $div(a: $ty, b: $ty) -> $ty;
  46. }
  47. let r = match () {
  48. #[cfg(not(all(feature = "c", any(target_arch = "x86"))))]
  49. () => $div(a, b),
  50. #[cfg(all(feature = "c", any(target_arch = "x86")))]
  51. () => unsafe { $div(a, b) },
  52. };
  53. *rem = a - (r * b);
  54. r
  55. }
  56. }
  57. }
  58. #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
  59. div!(__divsi3: i32, u32);
  60. #[cfg(not(all(feature = "c", target_arch = "x86")))]
  61. div!(__divdi3: i64, u64);
  62. #[cfg(not(all(windows, target_pointer_width="64")))]
  63. div!(__divti3: i128, u128);
  64. #[cfg(all(windows, target_pointer_width="64"))]
  65. div!(__divti3: i128, u128, ::U64x2, ::sconv);
  66. #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
  67. mod_!(__modsi3: i32, u32);
  68. #[cfg(not(all(feature = "c", target_arch = "x86")))]
  69. mod_!(__moddi3: i64, u64);
  70. #[cfg(not(all(windows, target_pointer_width="64")))]
  71. mod_!(__modti3: i128, u128);
  72. #[cfg(all(windows, target_pointer_width="64"))]
  73. mod_!(__modti3: i128, u128, ::U64x2, ::sconv);
  74. #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
  75. divmod!(__divmodsi4, __divsi3: i32);
  76. divmod!(__divmoddi4, __divdi3: i64);
  77. #[cfg(test)]
  78. mod tests {
  79. use qc::{U32, U64};
  80. check! {
  81. fn __divdi3(f: extern fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
  82. let (n, d) = (n.0 as i64, d.0 as i64);
  83. if d == 0 {
  84. None
  85. } else {
  86. Some(f(n, d))
  87. }
  88. }
  89. fn __moddi3(f: extern 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 __divmoddi4(f: extern fn(i64, i64, &mut i64) -> i64,
  98. n: U64,
  99. d: U64) -> Option<(i64, i64)> {
  100. let (n, d) = (n.0 as i64, d.0 as i64);
  101. if d == 0 {
  102. None
  103. } else {
  104. let mut r = 0;
  105. let q = f(n, d, &mut r);
  106. Some((q, r))
  107. }
  108. }
  109. fn __divsi3(f: extern fn(i32, i32) -> i32,
  110. n: U32,
  111. d: U32) -> Option<i32> {
  112. let (n, d) = (n.0 as i32, d.0 as i32);
  113. if d == 0 {
  114. None
  115. } else {
  116. Some(f(n, d))
  117. }
  118. }
  119. fn __modsi3(f: extern fn(i32, i32) -> i32,
  120. n: U32,
  121. d: U32) -> Option<i32> {
  122. let (n, d) = (n.0 as i32, d.0 as i32);
  123. if d == 0 {
  124. None
  125. } else {
  126. Some(f(n, d))
  127. }
  128. }
  129. fn __divmodsi4(f: extern fn(i32, i32, &mut i32) -> i32,
  130. n: U32,
  131. d: U32) -> Option<(i32, i32)> {
  132. let (n, d) = (n.0 as i32, d.0 as i32);
  133. if d == 0 {
  134. None
  135. } else {
  136. let mut r = 0;
  137. let q = f(n, d, &mut r);
  138. Some((q, r))
  139. }
  140. }
  141. }
  142. }
  143. #[cfg(test)]
  144. #[cfg(all(not(windows),
  145. not(target_arch = "mips64"),
  146. not(target_arch = "mips64el"),
  147. target_pointer_width="64"))]
  148. mod tests_i128 {
  149. use qc::U128;
  150. check! {
  151. fn __divti3(f: extern fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
  152. let (n, d) = (n.0 as i128, d.0 as i128);
  153. if d == 0 {
  154. None
  155. } else {
  156. Some(f(n, d))
  157. }
  158. }
  159. fn __modti3(f: extern fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
  160. let (n, d) = (n.0 as i128, d.0 as i128);
  161. if d == 0 {
  162. None
  163. } else {
  164. Some(f(n, d))
  165. }
  166. }
  167. }
  168. }