Skip to content

Commit

Permalink
u128 udiv intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Jan 5, 2017
1 parent d053642 commit d2eae2e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ These builtins are needed to support 128-bit integers, which are in the process
- [ ] modti3.c
- [x] muloti4.c
- [x] multi3.c
- [ ] udivmodti4.c
- [ ] udivti3.c
- [ ] umodti3.c
- [x] udivmodti4.c
- [x] udivti3.c
- [x] umodti3.c

## Unimplemented functions

Expand Down
5 changes: 1 addition & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ fn main() {
"subtf3.c",
"subvti3.c",
"trampoline_setup.c",
"ucmpti2.c",
"udivmodti4.c",
"udivti3.c",
"umodti3.c"]);
"ucmpti2.c"]);
}

if target_vendor == "apple" {
Expand Down
73 changes: 60 additions & 13 deletions src/int/udiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,45 @@ pub extern "C" fn __udivmodsi4(n: u32, d: u32, rem: Option<&mut u32>) -> u32 {
q
}

/// Returns `n / d`
#[cfg_attr(not(test), no_mangle)]
#[cfg(not(all(feature = "c", target_arch = "x86")))]
pub extern "C" fn __udivdi3(n: u64, d: u64) -> u64 {
__udivmoddi4(n, d, None)
macro_rules! div_mod_intrinsics {
($udiv_intr:ident, $umod_intr:ident : $ty:ty) => {
div_mod_intrinsics!($udiv_intr, $umod_intr : $ty,
__udivmoddi4);
};
($udiv_intr:ident, $umod_intr:ident : $ty:ty, $divmod_intr:expr) => {
div_mod_intrinsics!($udiv_intr, $umod_intr : $ty,
$divmod_intr, $ty, |i|{ i });
};
($udiv_intr:ident, $umod_intr:ident : $ty:ty, $divmod_intr:expr,
$tyret:ty, $conv:expr) => {
/// Returns `n / d`
pub extern "C" fn $udiv_intr(n: $ty, d: $ty) -> $tyret {
let r = $divmod_intr(n, d, None);
($conv)(r)
}

/// Returns `n % d`
pub extern "C" fn $umod_intr(a: $ty, b: $ty) -> $tyret {
use core::mem;

let mut rem = unsafe { mem::uninitialized() };
$divmod_intr(a, b, Some(&mut rem));
($conv)(rem)
}
}
}

/// Returns `n % d`
#[cfg(not(all(feature = "c", target_arch = "x86")))]
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __umoddi3(a: u64, b: u64) -> u64 {
use core::mem;
#[cfg(not(all(feature = "c", target_arch = "x86")))]
div_mod_intrinsics!(__udivdi3, __umoddi3: u64);

let mut rem = unsafe { mem::uninitialized() };
__udivmoddi4(a, b, Some(&mut rem));
rem
}
#[cfg(not(stage0))]
#[cfg(not(all(windows, target_pointer_width="64")))]
div_mod_intrinsics!(__udivti3, __umodti3: u128, u128_div_mod);

#[cfg(not(stage0))]
#[cfg(all(windows, target_pointer_width="64"))]
div_mod_intrinsics!(__udivti3, __umodti3: u128, u128_div_mod, ::U64x2, ::conv);

macro_rules! udivmod_inner {
($n:expr, $d:expr, $rem:expr, $ty:ty) => {{
Expand Down Expand Up @@ -269,6 +291,31 @@ pub extern "C" fn __udivmoddi4(n: u64, d: u64, rem: Option<&mut u64>) -> u64 {
udivmod_inner!(n, d, rem, u64)
}

macro_rules! udivmodti4 {
($tyret:ty, $conv:expr) => {
/// Returns `n / d` and sets `*rem = n % d`
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __udivmodti4(n: u128, d: u128, rem: Option<&mut u128>) -> $tyret {
let r = u128_div_mod(n, d, rem);
($conv)(r)
}
}
}

/// Returns `n / d` and sets `*rem = n % d`
#[cfg(not(stage0))]
fn u128_div_mod(n: u128, d: u128, rem: Option<&mut u128>) -> u128 {
udivmod_inner!(n, d, rem, u128)
}

#[cfg(not(stage0))]
#[cfg(all(windows, target_pointer_width="64"))]
udivmodti4!(::U64x2, ::conv);

#[cfg(not(stage0))]
#[cfg(not(all(windows, target_pointer_width="64")))]
udivmodti4!(u128, |i|{ i });

#[cfg(test)]
mod tests {
use qc::{U32, U64};
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ type U128_ = u128;
#[cfg(not(stage0))]
type I128_ = i128;

// Hack for LLVM expectations for ABI on windows
#[cfg(all(windows, target_pointer_width="64"))]
#[repr(simd)]
struct U64x2(u64, u64);

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

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

0 comments on commit d2eae2e

Please sign in to comment.