Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement all the 128-bit operator lang items (Rust 45676 part 2) #210

Merged
merged 5 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
482 changes: 482 additions & 0 deletions build.rs

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions src/int/addsub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use int::LargeInt;
use int::Int;

trait UAddSub: LargeInt {
fn uadd(self, other: Self) -> Self {
let (low, carry) = self.low().overflowing_add(other.low());
let high = self.high().wrapping_add(other.high());
let carry = if carry { Self::HighHalf::ONE } else { Self::HighHalf::ZERO };
Self::from_parts(low, high.wrapping_add(carry))
}
fn uadd_one(self) -> Self {
let (low, carry) = self.low().overflowing_add(Self::LowHalf::ONE);
let carry = if carry { Self::HighHalf::ONE } else { Self::HighHalf::ZERO };
Self::from_parts(low, self.high().wrapping_add(carry))
}
fn usub(self, other: Self) -> Self {
let uneg = (!other).uadd_one();
self.uadd(uneg)
}
}

impl UAddSub for u128 {}

trait AddSub: Int
where <Self as Int>::UnsignedInt: UAddSub
{
fn add(self, other: Self) -> Self {
Self::from_unsigned(self.unsigned().uadd(other.unsigned()))
}
fn sub(self, other: Self) -> Self {
Self::from_unsigned(self.unsigned().usub(other.unsigned()))
}
}

impl AddSub for u128 {}
impl AddSub for i128 {}

trait Addo: AddSub
where <Self as Int>::UnsignedInt: UAddSub
{
fn addo(self, other: Self, overflow: &mut i32) -> Self {
*overflow = 0;
let result = AddSub::add(self, other);
if other >= Self::ZERO {
if result < self {
*overflow = 1;
}
} else {
if result >= self {
*overflow = 1;
}
}
result
}
}

impl Addo for i128 {}
impl Addo for u128 {}

trait Subo: AddSub
where <Self as Int>::UnsignedInt: UAddSub
{
fn subo(self, other: Self, overflow: &mut i32) -> Self {
*overflow = 0;
let result = AddSub::sub(self, other);
if other >= Self::ZERO {
if result > self {
*overflow = 1;
}
} else {
if result <= self {
*overflow = 1;
}
}
result
}
}

impl Subo for i128 {}
impl Subo for u128 {}

#[cfg_attr(not(stage0), lang = "i128_add")]
pub fn rust_i128_add(a: i128, b: i128) -> i128 {
rust_u128_add(a as _, b as _) as _
}
#[cfg_attr(not(stage0), lang = "i128_addo")]
pub fn rust_i128_addo(a: i128, b: i128) -> (i128, bool) {
let mut oflow = 0;
let r = a.addo(b, &mut oflow);
(r, oflow != 0)
}
#[cfg_attr(not(stage0), lang = "u128_add")]
pub fn rust_u128_add(a: u128, b: u128) -> u128 {
a.add(b)
}
#[cfg_attr(not(stage0), lang = "u128_addo")]
pub fn rust_u128_addo(a: u128, b: u128) -> (u128, bool) {
let mut oflow = 0;
let r = a.addo(b, &mut oflow);
(r, oflow != 0)
}

#[cfg_attr(not(stage0), lang = "i128_sub")]
pub fn rust_i128_sub(a: i128, b: i128) -> i128 {
rust_u128_sub(a as _, b as _) as _
}
#[cfg_attr(not(stage0), lang = "i128_subo")]
pub fn rust_i128_subo(a: i128, b: i128) -> (i128, bool) {
let mut oflow = 0;
let r = a.subo(b, &mut oflow);
(r, oflow != 0)
}
#[cfg_attr(not(stage0), lang = "u128_sub")]
pub fn rust_u128_sub(a: u128, b: u128) -> u128 {
a.sub(b)
}
#[cfg_attr(not(stage0), lang = "u128_subo")]
pub fn rust_u128_subo(a: u128, b: u128) -> (u128, bool) {
let mut oflow = 0;
let r = a.subo(b, &mut oflow);
(r, oflow != 0)
}
6 changes: 6 additions & 0 deletions src/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ macro_rules! os_ty {
}
}

pub mod addsub;
pub mod mul;
pub mod sdiv;
pub mod shift;
Expand Down Expand Up @@ -72,6 +73,7 @@ pub trait Int:
fn wrapping_mul(self, other: Self) -> Self;
fn wrapping_sub(self, other: Self) -> Self;
fn wrapping_shl(self, other: u32) -> Self;
fn overflowing_add(self, other: Self) -> (Self, bool);
fn aborting_div(self, other: Self) -> Self;
fn aborting_rem(self, other: Self) -> Self;
fn leading_zeros(self) -> u32;
Expand Down Expand Up @@ -119,6 +121,10 @@ macro_rules! int_impl_common {
<Self>::wrapping_shl(self, other)
}

fn overflowing_add(self, other: Self) -> (Self, bool) {
<Self>::overflowing_add(self, other)
}

fn aborting_div(self, other: Self) -> Self {
unwrap(<Self>::checked_div(self, other))
}
Expand Down
33 changes: 33 additions & 0 deletions src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ impl Mulo for i32 {}
impl Mulo for i64 {}
impl Mulo for i128 {}

trait UMulo : Int {
fn mulo(self, other: Self, overflow: &mut i32) -> Self {
*overflow = 0;
let result = self.wrapping_mul(other);
if self > Self::max_value().aborting_div(other) {
*overflow = 1;
}
result
}
}
impl UMulo for u128 {}

intrinsics! {
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
#[arm_aeabi_alias = __aeabi_lmul]
Expand All @@ -95,3 +107,24 @@ intrinsics! {
a.mulo(b, oflow)
}
}

#[cfg_attr(not(stage0), lang = "i128_mul")]
pub fn rust_i128_mul(a: i128, b: i128) -> i128 {
__multi3(a, b)
}
#[cfg_attr(not(stage0), lang = "i128_mulo")]
pub fn rust_i128_mulo(a: i128, b: i128) -> (i128, bool) {
let mut oflow = 0;
let r = __muloti4(a, b, &mut oflow);
(r, oflow != 0)
}
#[cfg_attr(not(stage0), lang = "u128_mul")]
pub fn rust_u128_mul(a: u128, b: u128) -> u128 {
__multi3(a as _, b as _) as _
}
#[cfg_attr(not(stage0), lang = "u128_mulo")]
pub fn rust_u128_mulo(a: u128, b: u128) -> (u128, bool) {
let mut oflow = 0;
let r = a.mulo(b, &mut oflow);
(r, oflow != 0)
}
9 changes: 9 additions & 0 deletions src/int/sdiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ intrinsics! {
a.divmod(b, rem, |a, b| __divdi3(a, b))
}
}

#[cfg_attr(not(stage0), lang = "i128_div")]
pub fn rust_i128_div(a: i128, b: i128) -> i128 {
__divti3(a, b)
}
#[cfg_attr(not(stage0), lang = "i128_rem")]
pub fn rust_i128_rem(a: i128, b: i128) -> i128 {
__modti3(a, b)
}
34 changes: 34 additions & 0 deletions src/int/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,37 @@ intrinsics! {
a.lshr(b)
}
}

#[cfg_attr(not(stage0), lang = "i128_shl")]
pub fn rust_i128_shl(a: i128, b: u32) -> i128 {
__ashlti3(a as _, b) as _
}
#[cfg_attr(not(stage0), lang = "i128_shlo")]
pub fn rust_i128_shlo(a: i128, b: u128) -> (i128, bool) {
(rust_i128_shl(a, b as _), b >= 128)
}
#[cfg_attr(not(stage0), lang = "u128_shl")]
pub fn rust_u128_shl(a: u128, b: u32) -> u128 {
__ashlti3(a, b)
}
#[cfg_attr(not(stage0), lang = "u128_shlo")]
pub fn rust_u128_shlo(a: u128, b: u128) -> (u128, bool) {
(rust_u128_shl(a, b as _), b >= 128)
}

#[cfg_attr(not(stage0), lang = "i128_shr")]
pub fn rust_i128_shr(a: i128, b: u32) -> i128 {
__ashrti3(a, b)
}
#[cfg_attr(not(stage0), lang = "i128_shro")]
pub fn rust_i128_shro(a: i128, b: u128) -> (i128, bool) {
(rust_i128_shr(a, b as _), b >= 128)
}
#[cfg_attr(not(stage0), lang = "u128_shr")]
pub fn rust_u128_shr(a: u128, b: u32) -> u128 {
__lshrti3(a, b)
}
#[cfg_attr(not(stage0), lang = "u128_shro")]
pub fn rust_u128_shro(a: u128, b: u128) -> (u128, bool) {
(rust_u128_shr(a, b as _), b >= 128)
}
9 changes: 9 additions & 0 deletions src/int/udiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,12 @@ intrinsics! {
udivmod_inner!(n, d, rem, u128)
}
}

#[cfg_attr(not(stage0), lang = "u128_div")]
pub fn rust_u128_div(a: u128, b: u128) -> u128 {
__udivti3(a, b)
}
#[cfg_attr(not(stage0), lang = "u128_rem")]
pub fn rust_u128_rem(a: u128, b: u128) -> u128 {
__umodti3(a, b)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![feature(repr_simd)]
#![feature(abi_unadjusted)]
#![feature(linkage)]
#![feature(lang_items)]
#![allow(unused_features)]
#![no_builtins]
#![unstable(feature = "compiler_builtins_lib",
Expand Down
8 changes: 8 additions & 0 deletions tests/i128_add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/i128_add.rs"));
8 changes: 8 additions & 0 deletions tests/i128_addo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/i128_addo.rs"));
8 changes: 8 additions & 0 deletions tests/i128_sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/i128_sub.rs"));
8 changes: 8 additions & 0 deletions tests/i128_subo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/i128_subo.rs"));
8 changes: 8 additions & 0 deletions tests/u128_add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/u128_add.rs"));
8 changes: 8 additions & 0 deletions tests/u128_addo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/u128_addo.rs"));
8 changes: 8 additions & 0 deletions tests/u128_sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/u128_sub.rs"));
8 changes: 8 additions & 0 deletions tests/u128_subo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(compiler_builtins_lib)]
#![feature(i128_type)]
#![cfg_attr(all(target_arch = "arm",
not(any(target_env = "gnu", target_env = "musl")),
target_os = "linux",
test), no_std)]

include!(concat!(env!("OUT_DIR"), "/u128_subo.rs"));