shift.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use testcrate::*;
  2. macro_rules! shift {
  3. ($($i:ty, $fn_std:ident, $fn_builtins:ident);*;) => {
  4. $(
  5. fuzz_shift(|x: $i, s: u32| {
  6. let tmp0: $i = x.$fn_std(s);
  7. let tmp1: $i = $fn_builtins(x, s);
  8. if tmp0 != tmp1 {
  9. panic!(
  10. "{}({}, {}): std: {}, builtins: {}",
  11. stringify!($fn_builtins), x, s, tmp0, tmp1
  12. );
  13. }
  14. });
  15. )*
  16. };
  17. }
  18. macro_rules! overflowing_shift {
  19. ($($i:ty, $fn_std:ident, $fn_builtins:ident);*;) => {
  20. $(
  21. fuzz_shift(|x: $i, s: u32| {
  22. let tmp0: $i = x.$fn_std(s);
  23. let (tmp1, o1): ($i, bool) = $fn_builtins(x, s.into());
  24. if tmp0 != tmp1 || o1 {
  25. panic!(
  26. "{}({}, {}): std: {}, builtins: {}",
  27. stringify!($fn_builtins), x, s, tmp0, tmp1
  28. );
  29. }
  30. });
  31. )*
  32. };
  33. }
  34. #[test]
  35. fn shift() {
  36. use compiler_builtins::int::shift::{
  37. __ashldi3, __ashlsi3, __ashlti3, __ashrdi3, __ashrsi3, __ashrti3, __lshrdi3, __lshrsi3,
  38. __lshrti3, __rust_i128_shlo, __rust_i128_shro, __rust_u128_shlo, __rust_u128_shro,
  39. };
  40. shift!(
  41. u32, wrapping_shl, __ashlsi3;
  42. u64, wrapping_shl, __ashldi3;
  43. u128, wrapping_shl, __ashlti3;
  44. i32, wrapping_shr, __ashrsi3;
  45. i64, wrapping_shr, __ashrdi3;
  46. i128, wrapping_shr, __ashrti3;
  47. u32, wrapping_shr, __lshrsi3;
  48. u64, wrapping_shr, __lshrdi3;
  49. u128, wrapping_shr, __lshrti3;
  50. );
  51. overflowing_shift!(
  52. u128, wrapping_shl, __rust_u128_shlo;
  53. i128, wrapping_shl, __rust_i128_shlo;
  54. u128, wrapping_shr, __rust_u128_shro;
  55. i128, wrapping_shr, __rust_i128_shro;
  56. );
  57. }