addsub.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use testcrate::*;
  2. macro_rules! sum {
  3. ($($i:ty, $fn_add:ident, $fn_sub:ident);*;) => {
  4. $(
  5. fuzz_2(N, |x: $i, y: $i| {
  6. let add0 = x.wrapping_add(y);
  7. let sub0 = x.wrapping_sub(y);
  8. let add1: $i = $fn_add(x, y);
  9. let sub1: $i = $fn_sub(x, y);
  10. if add0 != add1 {
  11. panic!(
  12. "{}({}, {}): std: {}, builtins: {}",
  13. stringify!($fn_add), x, y, add0, add1
  14. );
  15. }
  16. if sub0 != sub1 {
  17. panic!(
  18. "{}({}, {}): std: {}, builtins: {}",
  19. stringify!($fn_sub), x, y, sub0, sub1
  20. );
  21. }
  22. });
  23. )*
  24. };
  25. }
  26. macro_rules! overflowing_sum {
  27. ($($i:ty, $fn_add:ident, $fn_sub:ident);*;) => {
  28. $(
  29. fuzz_2(N, |x: $i, y: $i| {
  30. let add0 = x.overflowing_add(y);
  31. let sub0 = x.overflowing_sub(y);
  32. let add1: ($i, bool) = $fn_add(x, y);
  33. let sub1: ($i, bool) = $fn_sub(x, y);
  34. if add0.0 != add1.0 || add0.1 != add1.1 {
  35. panic!(
  36. "{}({}, {}): std: {:?}, builtins: {:?}",
  37. stringify!($fn_add), x, y, add0, add1
  38. );
  39. }
  40. if sub0.0 != sub1.0 || sub0.1 != sub1.1 {
  41. panic!(
  42. "{}({}, {}): std: {:?}, builtins: {:?}",
  43. stringify!($fn_sub), x, y, sub0, sub1
  44. );
  45. }
  46. });
  47. )*
  48. };
  49. }
  50. #[test]
  51. fn addsub() {
  52. use compiler_builtins::int::addsub::{
  53. __rust_i128_add, __rust_i128_addo, __rust_i128_sub, __rust_i128_subo, __rust_u128_add,
  54. __rust_u128_addo, __rust_u128_sub, __rust_u128_subo,
  55. };
  56. // Integer addition and subtraction is very simple, so 100 fuzzing passes should be plenty.
  57. sum!(
  58. u128, __rust_u128_add, __rust_u128_sub;
  59. i128, __rust_i128_add, __rust_i128_sub;
  60. );
  61. overflowing_sum!(
  62. u128, __rust_u128_addo, __rust_u128_subo;
  63. i128, __rust_i128_addo, __rust_i128_subo;
  64. );
  65. }
  66. macro_rules! float_sum {
  67. ($($f:ty, $fn_add:ident, $fn_sub:ident);*;) => {
  68. $(
  69. fuzz_float_2(N, |x: $f, y: $f| {
  70. let add0 = x + y;
  71. let sub0 = x - y;
  72. let add1: $f = $fn_add(x, y);
  73. let sub1: $f = $fn_sub(x, y);
  74. if !Float::eq_repr(add0, add1) {
  75. panic!(
  76. "{}({}, {}): std: {}, builtins: {}",
  77. stringify!($fn_add), x, y, add0, add1
  78. );
  79. }
  80. if !Float::eq_repr(sub0, sub1) {
  81. panic!(
  82. "{}({}, {}): std: {}, builtins: {}",
  83. stringify!($fn_sub), x, y, sub0, sub1
  84. );
  85. }
  86. });
  87. )*
  88. };
  89. }
  90. #[cfg(not(all(target_arch = "x86", not(target_feature = "sse"))))]
  91. #[test]
  92. fn float_addsub() {
  93. use compiler_builtins::float::{
  94. add::{__adddf3, __addsf3},
  95. sub::{__subdf3, __subsf3},
  96. Float,
  97. };
  98. float_sum!(
  99. f32, __addsf3, __subsf3;
  100. f64, __adddf3, __subdf3;
  101. );
  102. }