addsub.rs 3.6 KB

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