shift.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use int::{Int, LargeInt};
  2. trait Ashl: Int + LargeInt {
  3. /// Returns `a << b`, requires `b < $ty::bits()`
  4. fn ashl(self, offset: u32) -> Self
  5. where Self: LargeInt<HighHalf = <Self as LargeInt>::LowHalf>,
  6. {
  7. let half_bits = Self::bits() / 2;
  8. if offset & half_bits != 0 {
  9. Self::from_parts(Int::zero(), self.low() << (offset - half_bits))
  10. } else if offset == 0 {
  11. self
  12. } else {
  13. Self::from_parts(self.low() << offset,
  14. (self.high() << offset) |
  15. (self.low() >> (half_bits - offset)))
  16. }
  17. }
  18. }
  19. impl Ashl for u64 {}
  20. impl Ashl for u128 {}
  21. trait Ashr: Int + LargeInt {
  22. /// Returns arithmetic `a >> b`, requires `b < $ty::bits()`
  23. fn ashr(self, offset: u32) -> Self
  24. where Self: LargeInt<LowHalf = <<Self as LargeInt>::HighHalf as Int>::UnsignedInt>,
  25. {
  26. let half_bits = Self::bits() / 2;
  27. if offset & half_bits != 0 {
  28. Self::from_parts((self.high() >> (offset - half_bits)).unsigned(),
  29. self.high() >> (half_bits - 1))
  30. } else if offset == 0 {
  31. self
  32. } else {
  33. let high_unsigned = self.high().unsigned();
  34. Self::from_parts((high_unsigned << (half_bits - offset)) | (self.low() >> offset),
  35. self.high() >> offset)
  36. }
  37. }
  38. }
  39. impl Ashr for i64 {}
  40. impl Ashr for i128 {}
  41. trait Lshr: Int + LargeInt {
  42. /// Returns logical `a >> b`, requires `b < $ty::bits()`
  43. fn lshr(self, offset: u32) -> Self
  44. where Self: LargeInt<HighHalf = <Self as LargeInt>::LowHalf>,
  45. {
  46. let half_bits = Self::bits() / 2;
  47. if offset & half_bits != 0 {
  48. Self::from_parts(self.high() >> (offset - half_bits), Int::zero())
  49. } else if offset == 0 {
  50. self
  51. } else {
  52. Self::from_parts((self.high() << (half_bits - offset)) |
  53. (self.low() >> offset),
  54. self.high() >> offset)
  55. }
  56. }
  57. }
  58. impl Lshr for u64 {}
  59. impl Lshr for u128 {}
  60. intrinsics! {
  61. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  62. #[arm_aeabi_alias = __aeabi_llsl]
  63. pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 {
  64. a.ashl(b)
  65. }
  66. pub extern "C" fn __ashlti3(a: u128, b: u32) -> u128 {
  67. a.ashl(b)
  68. }
  69. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  70. #[arm_aeabi_alias = __aeabi_lasr]
  71. pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 {
  72. a.ashr(b)
  73. }
  74. pub extern "C" fn __ashrti3(a: i128, b: u32) -> i128 {
  75. a.ashr(b)
  76. }
  77. #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
  78. #[arm_aeabi_alias = __aeabi_llsr]
  79. pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 {
  80. a.lshr(b)
  81. }
  82. pub extern "C" fn __lshrti3(a: u128, b: u32) -> u128 {
  83. a.lshr(b)
  84. }
  85. }