cmp.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #![allow(unused_macros)]
  2. use testcrate::*;
  3. macro_rules! cmp {
  4. ($x:ident, $y:ident, $($unordered_val:expr, $fn:ident);*;) => {
  5. $(
  6. let cmp0 = if $x.is_nan() || $y.is_nan() {
  7. $unordered_val
  8. } else if $x < $y {
  9. -1
  10. } else if $x == $y {
  11. 0
  12. } else {
  13. 1
  14. };
  15. let cmp1 = $fn($x, $y);
  16. if cmp0 != cmp1 {
  17. panic!("{}({}, {}): std: {}, builtins: {}", stringify!($fn_builtins), $x, $y, cmp0, cmp1);
  18. }
  19. )*
  20. };
  21. }
  22. // PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
  23. #[cfg(not(target_arch = "powerpc64"))]
  24. #[test]
  25. fn float_comparisons() {
  26. use compiler_builtins::float::cmp::{
  27. __eqdf2, __eqsf2, __gedf2, __gesf2, __gtdf2, __gtsf2, __ledf2, __lesf2, __ltdf2, __ltsf2,
  28. __nedf2, __nesf2, __unorddf2, __unordsf2,
  29. };
  30. fuzz_float_2(N, |x: f32, y: f32| {
  31. assert_eq!(__unordsf2(x, y) != 0, x.is_nan() || y.is_nan());
  32. cmp!(x, y,
  33. 1, __ltsf2;
  34. 1, __lesf2;
  35. 1, __eqsf2;
  36. -1, __gesf2;
  37. -1, __gtsf2;
  38. 1, __nesf2;
  39. );
  40. });
  41. fuzz_float_2(N, |x: f64, y: f64| {
  42. assert_eq!(__unorddf2(x, y) != 0, x.is_nan() || y.is_nan());
  43. cmp!(x, y,
  44. 1, __ltdf2;
  45. 1, __ledf2;
  46. 1, __eqdf2;
  47. -1, __gedf2;
  48. -1, __gtdf2;
  49. 1, __nedf2;
  50. );
  51. });
  52. }
  53. macro_rules! cmp2 {
  54. ($x:ident, $y:ident, $($unordered_val:expr, $fn_std:expr, $fn_builtins:ident);*;) => {
  55. $(
  56. let cmp0: i32 = if $x.is_nan() || $y.is_nan() {
  57. $unordered_val
  58. } else {
  59. $fn_std as i32
  60. };
  61. let cmp1: i32 = $fn_builtins($x, $y);
  62. if cmp0 != cmp1 {
  63. panic!("{}({}, {}): std: {}, builtins: {}", stringify!($fn_builtins), $x, $y, cmp0, cmp1);
  64. }
  65. )*
  66. };
  67. }
  68. #[cfg(target_arch = "arm")]
  69. #[test]
  70. fn float_comparisons_arm() {
  71. use compiler_builtins::float::cmp::{
  72. __aeabi_dcmpeq, __aeabi_dcmpge, __aeabi_dcmpgt, __aeabi_dcmple, __aeabi_dcmplt,
  73. __aeabi_fcmpeq, __aeabi_fcmpge, __aeabi_fcmpgt, __aeabi_fcmple, __aeabi_fcmplt, __eqdf2vfp,
  74. __eqsf2vfp, __gedf2vfp, __gesf2vfp, __gtdf2vfp, __gtsf2vfp, __ledf2vfp, __lesf2vfp,
  75. __ltdf2vfp, __ltsf2vfp, __nedf2vfp, __nesf2vfp,
  76. };
  77. fuzz_float_2(N, |x: f32, y: f32| {
  78. cmp2!(x, y,
  79. 0, x < y, __aeabi_fcmplt;
  80. 0, x <= y, __aeabi_fcmple;
  81. 0, x == y, __aeabi_fcmpeq;
  82. 0, x >= y, __aeabi_fcmpge;
  83. 0, x > y, __aeabi_fcmpgt;
  84. 0, x < y, __ltsf2vfp;
  85. 0, x <= y, __lesf2vfp;
  86. 0, x == y, __eqsf2vfp;
  87. 0, x >= y, __gesf2vfp;
  88. 0, x > y, __gtsf2vfp;
  89. 1, x != y, __nesf2vfp;
  90. );
  91. });
  92. fuzz_float_2(N, |x: f64, y: f64| {
  93. cmp2!(x, y,
  94. 0, x < y, __aeabi_dcmplt;
  95. 0, x <= y, __aeabi_dcmple;
  96. 0, x == y, __aeabi_dcmpeq;
  97. 0, x >= y, __aeabi_dcmpge;
  98. 0, x > y, __aeabi_dcmpgt;
  99. 0, x < y, __ltdf2vfp;
  100. 0, x <= y, __ledf2vfp;
  101. 0, x == y, __eqdf2vfp;
  102. 0, x >= y, __gedf2vfp;
  103. 0, x > y, __gtdf2vfp;
  104. 1, x != y, __nedf2vfp;
  105. );
  106. });
  107. }