mul.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use testcrate::*;
  2. macro_rules! mul {
  3. ($($i:ty, $fn:ident);*;) => {
  4. $(
  5. fuzz_2(N, |x: $i, y: $i| {
  6. let mul0 = x.wrapping_mul(y);
  7. let mul1: $i = $fn(x, y);
  8. if mul0 != mul1 {
  9. panic!(
  10. "{}({}, {}): std: {}, builtins: {}",
  11. stringify!($fn), x, y, mul0, mul1
  12. );
  13. }
  14. });
  15. )*
  16. };
  17. }
  18. #[test]
  19. fn mul() {
  20. use compiler_builtins::int::mul::{__muldi3, __multi3};
  21. mul!(
  22. u64, __muldi3;
  23. i128, __multi3;
  24. );
  25. }
  26. macro_rules! overflowing_mul {
  27. ($($i:ty, $fn:ident);*;) => {
  28. $(
  29. fuzz_2(N, |x: $i, y: $i| {
  30. let (mul0, o0) = x.overflowing_mul(y);
  31. let mut o1 = 0i32;
  32. let mul1: $i = $fn(x, y, &mut o1);
  33. let o1 = o1 != 0;
  34. if mul0 != mul1 || o0 != o1 {
  35. panic!(
  36. "{}({}, {}): std: ({}, {}), builtins: ({}, {})",
  37. stringify!($fn), x, y, mul0, o0, mul1, o1
  38. );
  39. }
  40. });
  41. )*
  42. };
  43. }
  44. #[test]
  45. fn overflowing_mul() {
  46. use compiler_builtins::int::mul::{
  47. __mulodi4, __mulosi4, __muloti4, __rust_i128_mulo, __rust_u128_mulo,
  48. };
  49. overflowing_mul!(
  50. i32, __mulosi4;
  51. i64, __mulodi4;
  52. i128, __muloti4;
  53. );
  54. fuzz_2(N, |x: u128, y: u128| {
  55. let (mul0, o0) = x.overflowing_mul(y);
  56. let (mul1, o1) = __rust_u128_mulo(x, y);
  57. if mul0 != mul1 || o0 != o1 {
  58. panic!(
  59. "__rust_u128_mulo({}, {}): std: ({}, {}), builtins: ({}, {})",
  60. x, y, mul0, o0, mul1, o1
  61. );
  62. }
  63. let x = x as i128;
  64. let y = y as i128;
  65. let (mul0, o0) = x.overflowing_mul(y);
  66. let (mul1, o1) = __rust_i128_mulo(x, y);
  67. if mul0 != mul1 || o0 != o1 {
  68. panic!(
  69. "__rust_i128_mulo({}, {}): std: ({}, {}), builtins: ({}, {})",
  70. x, y, mul0, o0, mul1, o1
  71. );
  72. }
  73. });
  74. }
  75. macro_rules! float_mul {
  76. ($($f:ty, $fn:ident);*;) => {
  77. $(
  78. fuzz_float_2(N, |x: $f, y: $f| {
  79. let mul0 = x * y;
  80. let mul1: $f = $fn(x, y);
  81. // multiplication of subnormals is not currently handled
  82. if !(Float::is_subnormal(&mul0) || Float::is_subnormal(&mul1)) {
  83. if !Float::eq_repr(mul0, mul1) {
  84. panic!(
  85. "{}({}, {}): std: {}, builtins: {}",
  86. stringify!($fn), x, y, mul0, mul1
  87. );
  88. }
  89. }
  90. });
  91. )*
  92. };
  93. }
  94. #[cfg(not(all(target_arch = "x86", not(target_feature = "sse"))))]
  95. #[test]
  96. fn float_mul() {
  97. use compiler_builtins::float::{
  98. mul::{__muldf3, __mulsf3},
  99. Float,
  100. };
  101. float_mul!(
  102. f32, __mulsf3;
  103. f64, __muldf3;
  104. );
  105. }