shift.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. use int::{Int, LargeInt};
  2. trait Ashl: Int + LargeInt {
  3. /// Returns `a << b`, requires `b < Self::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 < Self::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 < Self::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. }
  86. u128_lang_items! {
  87. #[lang = "i128_shl"]
  88. pub fn rust_i128_shl(a: i128, b: u32) -> i128 {
  89. __ashlti3(a as _, b) as _
  90. }
  91. #[lang = "i128_shlo"]
  92. pub fn rust_i128_shlo(a: i128, b: u128) -> (i128, bool) {
  93. (rust_i128_shl(a, b as _), b >= 128)
  94. }
  95. #[lang = "u128_shl"]
  96. pub fn rust_u128_shl(a: u128, b: u32) -> u128 {
  97. __ashlti3(a, b)
  98. }
  99. #[lang = "u128_shlo"]
  100. pub fn rust_u128_shlo(a: u128, b: u128) -> (u128, bool) {
  101. (rust_u128_shl(a, b as _), b >= 128)
  102. }
  103. #[lang = "i128_shr"]
  104. pub fn rust_i128_shr(a: i128, b: u32) -> i128 {
  105. __ashrti3(a, b)
  106. }
  107. #[lang = "i128_shro"]
  108. pub fn rust_i128_shro(a: i128, b: u128) -> (i128, bool) {
  109. (rust_i128_shr(a, b as _), b >= 128)
  110. }
  111. #[lang = "u128_shr"]
  112. pub fn rust_u128_shr(a: u128, b: u32) -> u128 {
  113. __lshrti3(a, b)
  114. }
  115. #[lang = "u128_shro"]
  116. pub fn rust_u128_shro(a: u128, b: u128) -> (u128, bool) {
  117. (rust_u128_shr(a, b as _), b >= 128)
  118. }
  119. }