div_rem.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use rand_xoshiro::rand_core::{RngCore, SeedableRng};
  2. use rand_xoshiro::Xoshiro128StarStar;
  3. use compiler_builtins::int::sdiv::{__divmoddi4, __divmodsi4, __divmodti4};
  4. use compiler_builtins::int::udiv::{__udivmoddi4, __udivmodsi4, __udivmodti4};
  5. /// Creates intensive test functions for division functions of a certain size
  6. macro_rules! test {
  7. (
  8. $n:expr, // the number of bits in a $iX or $uX
  9. $uX:ident, // unsigned integer that will be shifted
  10. $iX:ident, // signed version of $uX
  11. $test_name:ident, // name of the test function
  12. $unsigned_name:ident, // unsigned division function
  13. $signed_name:ident // signed division function
  14. ) => {
  15. #[test]
  16. fn $test_name() {
  17. fn assert_invariants(lhs: $uX, rhs: $uX) {
  18. let rem: &mut $uX = &mut 0;
  19. let quo: $uX = $unsigned_name(lhs, rhs, Some(rem));
  20. let rem = *rem;
  21. if rhs <= rem || (lhs != rhs.wrapping_mul(quo).wrapping_add(rem)) {
  22. panic!(
  23. "unsigned division function failed with lhs:{} rhs:{} \
  24. expected:({}, {}) found:({}, {})",
  25. lhs,
  26. rhs,
  27. lhs.wrapping_div(rhs),
  28. lhs.wrapping_rem(rhs),
  29. quo,
  30. rem
  31. );
  32. }
  33. // test the signed division function also
  34. let lhs = lhs as $iX;
  35. let rhs = rhs as $iX;
  36. let mut rem: $iX = 0;
  37. let quo: $iX = $signed_name(lhs, rhs, &mut rem);
  38. // We cannot just test that
  39. // `lhs == rhs.wrapping_mul(quo).wrapping_add(rem)`, but also
  40. // need to make sure the remainder isn't larger than the divisor
  41. // and has the correct sign.
  42. let incorrect_rem = if rem == 0 {
  43. false
  44. } else if rhs == $iX::MIN {
  45. // `rhs.wrapping_abs()` would overflow, so handle this case
  46. // separately.
  47. (lhs.is_negative() != rem.is_negative()) || (rem == $iX::MIN)
  48. } else {
  49. (lhs.is_negative() != rem.is_negative())
  50. || (rhs.wrapping_abs() <= rem.wrapping_abs())
  51. };
  52. if incorrect_rem || lhs != rhs.wrapping_mul(quo).wrapping_add(rem) {
  53. panic!(
  54. "signed division function failed with lhs:{} rhs:{} \
  55. expected:({}, {}) found:({}, {})",
  56. lhs,
  57. rhs,
  58. lhs.wrapping_div(rhs),
  59. lhs.wrapping_rem(rhs),
  60. quo,
  61. rem
  62. );
  63. }
  64. }
  65. // Specially designed random fuzzer
  66. let mut rng = Xoshiro128StarStar::seed_from_u64(0);
  67. let mut lhs: $uX = 0;
  68. let mut rhs: $uX = 0;
  69. // all ones constant
  70. let ones: $uX = !0;
  71. // Alternating ones and zeros (e.x. 0b1010101010101010). This catches second-order
  72. // problems that might occur for algorithms with two modes of operation (potentially
  73. // there is some invariant that can be broken for large `duo` and maintained via
  74. // alternating between modes, breaking the algorithm when it reaches the end).
  75. let mut alt_ones: $uX = 1;
  76. for _ in 0..($n / 2) {
  77. alt_ones <<= 2;
  78. alt_ones |= 1;
  79. }
  80. // creates a mask for indexing the bits of the type
  81. let bit_indexing_mask = $n - 1;
  82. for _ in 0..1_000_000 {
  83. // Randomly OR, AND, and XOR randomly sized and shifted continuous strings of
  84. // ones with `lhs` and `rhs`. This results in excellent fuzzing entropy such as:
  85. // lhs:10101010111101000000000100101010 rhs: 1010101010000000000000001000001
  86. // lhs:10101010111101000000000101001010 rhs: 1010101010101010101010100010100
  87. // lhs:10101010111101000000000101001010 rhs:11101010110101010101010100001110
  88. // lhs:10101010000000000000000001001010 rhs:10100010100000000000000000001010
  89. // lhs:10101010000000000000000001001010 rhs: 10101010101010101000
  90. // lhs:10101010000000000000000001100000 rhs:11111111111101010101010101001111
  91. // lhs:10101010000000101010101011000000 rhs:11111111111101010101010100000111
  92. // lhs:10101010101010101010101011101010 rhs: 1010100000000000000
  93. // lhs:11111111110101101010101011010111 rhs: 1010100000000000000
  94. // The msb is set half of the time by the fuzzer, but `assert_invariants` tests
  95. // both the signed and unsigned functions.
  96. let r0: u32 = bit_indexing_mask & rng.next_u32();
  97. let r1: u32 = bit_indexing_mask & rng.next_u32();
  98. let mask = ones.wrapping_shr(r0).rotate_left(r1);
  99. match rng.next_u32() % 8 {
  100. 0 => lhs |= mask,
  101. 1 => lhs &= mask,
  102. // both 2 and 3 to make XORs as common as ORs and ANDs combined, otherwise
  103. // the entropy gets destroyed too often
  104. 2 | 3 => lhs ^= mask,
  105. 4 => rhs |= mask,
  106. 5 => rhs &= mask,
  107. _ => rhs ^= mask,
  108. }
  109. // do the same for alternating ones and zeros
  110. let r0: u32 = bit_indexing_mask & rng.next_u32();
  111. let r1: u32 = bit_indexing_mask & rng.next_u32();
  112. let mask = alt_ones.wrapping_shr(r0).rotate_left(r1);
  113. match rng.next_u32() % 8 {
  114. 0 => lhs |= mask,
  115. 1 => lhs &= mask,
  116. // both 2 and 3 to make XORs as common as ORs and ANDs combined, otherwise
  117. // the entropy gets destroyed too often
  118. 2 | 3 => lhs ^= mask,
  119. 4 => rhs |= mask,
  120. 5 => rhs &= mask,
  121. _ => rhs ^= mask,
  122. }
  123. if rhs != 0 {
  124. assert_invariants(lhs, rhs);
  125. }
  126. }
  127. }
  128. };
  129. }
  130. test!(32, u32, i32, div_rem_si4, __udivmodsi4, __divmodsi4);
  131. test!(64, u64, i64, div_rem_di4, __udivmoddi4, __divmoddi4);
  132. test!(128, u128, i128, div_rem_ti4, __udivmodti4, __divmodti4);