2016-08-18 04:50:24 +08:00
|
|
|
use int::{Int, LargeInt};
|
2016-08-13 16:51:54 +08:00
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
trait Ashl: Int + LargeInt {
|
2017-09-14 03:01:40 +08:00
|
|
|
/// Returns `a << b`, requires `b < Self::BITS`
|
2017-06-23 13:03:07 +08:00
|
|
|
fn ashl(self, offset: u32) -> Self
|
|
|
|
where Self: LargeInt<HighHalf = <Self as LargeInt>::LowHalf>,
|
|
|
|
{
|
2017-09-14 03:01:40 +08:00
|
|
|
let half_bits = Self::BITS / 2;
|
2017-06-23 13:03:07 +08:00
|
|
|
if offset & half_bits != 0 {
|
2017-09-14 03:01:40 +08:00
|
|
|
Self::from_parts(Int::ZERO, self.low() << (offset - half_bits))
|
2017-06-23 13:03:07 +08:00
|
|
|
} else if offset == 0 {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
Self::from_parts(self.low() << offset,
|
|
|
|
(self.high() << offset) |
|
|
|
|
(self.low() >> (half_bits - offset)))
|
2016-08-13 16:51:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
impl Ashl for u64 {}
|
|
|
|
impl Ashl for u128 {}
|
|
|
|
|
|
|
|
trait Ashr: Int + LargeInt {
|
2017-09-14 03:01:40 +08:00
|
|
|
/// Returns arithmetic `a >> b`, requires `b < Self::BITS`
|
2017-06-23 13:03:07 +08:00
|
|
|
fn ashr(self, offset: u32) -> Self
|
|
|
|
where Self: LargeInt<LowHalf = <<Self as LargeInt>::HighHalf as Int>::UnsignedInt>,
|
|
|
|
{
|
2017-09-14 03:01:40 +08:00
|
|
|
let half_bits = Self::BITS / 2;
|
2017-06-23 13:03:07 +08:00
|
|
|
if offset & half_bits != 0 {
|
|
|
|
Self::from_parts((self.high() >> (offset - half_bits)).unsigned(),
|
|
|
|
self.high() >> (half_bits - 1))
|
|
|
|
} else if offset == 0 {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
let high_unsigned = self.high().unsigned();
|
|
|
|
Self::from_parts((high_unsigned << (half_bits - offset)) | (self.low() >> offset),
|
|
|
|
self.high() >> offset)
|
2016-08-13 16:51:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
impl Ashr for i64 {}
|
|
|
|
impl Ashr for i128 {}
|
|
|
|
|
|
|
|
trait Lshr: Int + LargeInt {
|
2017-09-14 03:01:40 +08:00
|
|
|
/// Returns logical `a >> b`, requires `b < Self::BITS`
|
2017-06-23 13:03:07 +08:00
|
|
|
fn lshr(self, offset: u32) -> Self
|
|
|
|
where Self: LargeInt<HighHalf = <Self as LargeInt>::LowHalf>,
|
|
|
|
{
|
2017-09-14 03:01:40 +08:00
|
|
|
let half_bits = Self::BITS / 2;
|
2017-06-23 13:03:07 +08:00
|
|
|
if offset & half_bits != 0 {
|
2017-09-14 03:01:40 +08:00
|
|
|
Self::from_parts(self.high() >> (offset - half_bits), Int::ZERO)
|
2017-06-23 13:03:07 +08:00
|
|
|
} else if offset == 0 {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
Self::from_parts((self.high() << (half_bits - offset)) |
|
|
|
|
(self.low() >> offset),
|
|
|
|
self.high() >> offset)
|
2016-08-13 16:51:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
impl Lshr for u64 {}
|
|
|
|
impl Lshr for u128 {}
|
2016-09-27 04:55:11 +08:00
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
intrinsics! {
|
2017-06-23 22:52:38 +08:00
|
|
|
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
|
2017-06-24 06:16:07 +08:00
|
|
|
#[arm_aeabi_alias = __aeabi_llsl]
|
2017-06-23 13:03:07 +08:00
|
|
|
pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 {
|
|
|
|
a.ashl(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub extern "C" fn __ashlti3(a: u128, b: u32) -> u128 {
|
|
|
|
a.ashl(b)
|
|
|
|
}
|
2017-01-04 10:04:27 +08:00
|
|
|
|
2017-06-23 22:52:38 +08:00
|
|
|
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
|
2017-06-24 06:16:07 +08:00
|
|
|
#[arm_aeabi_alias = __aeabi_lasr]
|
2017-06-23 13:03:07 +08:00
|
|
|
pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 {
|
|
|
|
a.ashr(b)
|
|
|
|
}
|
2016-09-27 04:55:11 +08:00
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
pub extern "C" fn __ashrti3(a: i128, b: u32) -> i128 {
|
|
|
|
a.ashr(b)
|
|
|
|
}
|
2017-01-04 10:04:27 +08:00
|
|
|
|
2017-06-23 22:52:38 +08:00
|
|
|
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
|
2017-06-24 06:16:07 +08:00
|
|
|
#[arm_aeabi_alias = __aeabi_llsr]
|
2017-06-23 13:03:07 +08:00
|
|
|
pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 {
|
|
|
|
a.lshr(b)
|
|
|
|
}
|
2016-08-13 16:51:54 +08:00
|
|
|
|
2017-06-23 13:03:07 +08:00
|
|
|
pub extern "C" fn __lshrti3(a: u128, b: u32) -> u128 {
|
|
|
|
a.lshr(b)
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 20:21:05 +08:00
|
|
|
|
2017-12-09 00:19:28 +08:00
|
|
|
u128_lang_items! {
|
|
|
|
#[lang = "i128_shl"]
|
|
|
|
pub fn rust_i128_shl(a: i128, b: u32) -> i128 {
|
|
|
|
__ashlti3(a as _, b) as _
|
|
|
|
}
|
|
|
|
#[lang = "i128_shlo"]
|
|
|
|
pub fn rust_i128_shlo(a: i128, b: u128) -> (i128, bool) {
|
|
|
|
(rust_i128_shl(a, b as _), b >= 128)
|
|
|
|
}
|
|
|
|
#[lang = "u128_shl"]
|
|
|
|
pub fn rust_u128_shl(a: u128, b: u32) -> u128 {
|
|
|
|
__ashlti3(a, b)
|
|
|
|
}
|
|
|
|
#[lang = "u128_shlo"]
|
|
|
|
pub fn rust_u128_shlo(a: u128, b: u128) -> (u128, bool) {
|
|
|
|
(rust_u128_shl(a, b as _), b >= 128)
|
|
|
|
}
|
2017-11-25 20:21:05 +08:00
|
|
|
|
2017-12-09 00:19:28 +08:00
|
|
|
#[lang = "i128_shr"]
|
|
|
|
pub fn rust_i128_shr(a: i128, b: u32) -> i128 {
|
|
|
|
__ashrti3(a, b)
|
|
|
|
}
|
|
|
|
#[lang = "i128_shro"]
|
|
|
|
pub fn rust_i128_shro(a: i128, b: u128) -> (i128, bool) {
|
|
|
|
(rust_i128_shr(a, b as _), b >= 128)
|
|
|
|
}
|
|
|
|
#[lang = "u128_shr"]
|
|
|
|
pub fn rust_u128_shr(a: u128, b: u32) -> u128 {
|
|
|
|
__lshrti3(a, b)
|
|
|
|
}
|
|
|
|
#[lang = "u128_shro"]
|
|
|
|
pub fn rust_u128_shro(a: u128, b: u128) -> (u128, bool) {
|
|
|
|
(rust_u128_shr(a, b as _), b >= 128)
|
|
|
|
}
|
2017-11-25 20:21:05 +08:00
|
|
|
}
|