mul.rs 3.2 KB

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