Skip to content

Commit

Permalink
u128 sdiv intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Jan 6, 2017
1 parent 211d602 commit e4091a9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ These builtins are needed to support 128-bit integers, which are in the process

- [x] ashlti3.c
- [x] ashrti3.c
- [ ] divti3.c
- [x] divti3.c
- [ ] fixdfti.c
- [ ] fixsfti.c
- [ ] fixunsdfti.c
Expand All @@ -205,7 +205,7 @@ These builtins are needed to support 128-bit integers, which are in the process
- [ ] floatuntidf.c
- [ ] floatuntisf.c
- [x] lshrti3.c
- [ ] modti3.c
- [x] modti3.c
- [x] muloti4.c
- [x] multi3.c
- [x] udivmodti4.c
Expand Down
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ fn main() {
"cmpti2.c",
"ctzti2.c",
"divtf3.c",
"divti3.c",
"ffsti2.c",
"fixdfti.c",
"fixsfti.c",
Expand All @@ -196,7 +195,6 @@ fn main() {
"floatuntidf.c",
"floatuntisf.c",
"floatuntixf.c",
"modti3.c",
"multf3.c",
"mulvti3.c",
"negti2.c",
Expand Down
26 changes: 22 additions & 4 deletions src/int/sdiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ use int::Int;

macro_rules! div {
($intrinsic:ident: $ty:ty, $uty:ty) => {
div!($intrinsic: $ty, $uty, $ty, |i| {i});
};
($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => {
/// Returns `a / b`
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
let s_a = a >> (<$ty>::bits() - 1);
let s_b = b >> (<$ty>::bits() - 1);
let a = (a ^ s_a) - s_a;
let b = (b ^ s_b) - s_b;
let s = s_a ^ s_b;

let r = udiv!(a as $uty, b as $uty);
(r as $ty ^ s) - s
($conv)((r as $ty ^ s) - s)
}
}
}

macro_rules! mod_ {
($intrinsic:ident: $ty:ty, $uty:ty) => {
mod_!($intrinsic: $ty, $uty, $ty, |i| {i});
};
($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => {
/// Returns `a % b`
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
let s = b >> (<$ty>::bits() - 1);
let b = (b ^ s) - s;
let s = a >> (<$ty>::bits() - 1);
let a = (a ^ s) - s;

let r = urem!(a as $uty, b as $uty);
(r as $ty ^ s) - s
($conv)((r as $ty ^ s) - s)
}
}
}
Expand Down Expand Up @@ -61,12 +67,24 @@ div!(__divsi3: i32, u32);
#[cfg(not(all(feature = "c", target_arch = "x86")))]
div!(__divdi3: i64, u64);

#[cfg(not(all(windows, target_pointer_width="64", not(stage0))))]
div!(__divti3: ::I128_, ::U128_);

#[cfg(all(windows, target_pointer_width="64", not(stage0)))]
div!(__divti3: i128, u128, ::U64x2, ::sconv);

#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
mod_!(__modsi3: i32, u32);

#[cfg(not(all(feature = "c", target_arch = "x86")))]
mod_!(__moddi3: i64, u64);

#[cfg(not(all(windows, target_pointer_width="64", not(stage0))))]
mod_!(__modti3: ::I128_, ::U128_);

#[cfg(all(windows, target_pointer_width="64", not(stage0)))]
mod_!(__modti3: i128, u128, ::U64x2, ::sconv);

#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
divmod!(__divmodsi4, __divsi3: i32);

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ fn conv(i: u128) -> U64x2 {
U64x2(i.low(), i.high())
}

#[cfg(all(windows, target_pointer_width="64"))]
fn sconv(i: i128) -> U64x2 {
use int::LargeInt;
let j = i as u128;
U64x2(j.low(), j.high())
}

#[cfg(test)]
#[macro_use]
extern crate quickcheck;
Expand Down

0 comments on commit e4091a9

Please sign in to comment.