2017-09-14 17:43:36 -04:00
|
|
|
|
// Copyright 2009-2016 compiler-builtins Developers
|
|
|
|
|
//
|
|
|
|
|
// The compiler-builtins crate is dual licensed under both the University of
|
|
|
|
|
// Illinois "BSD-Like" license and the MIT license. As a user of this code you may
|
|
|
|
|
// choose to use it under either license. As a contributor, you agree to allow
|
|
|
|
|
// your code to be used under both.
|
|
|
|
|
//
|
|
|
|
|
// Full text of the relevant licenses is found here:
|
|
|
|
|
// https://github.com/rust-lang-nursery/compiler-builtins/blob/master/LICENSE.TXT
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// The following code is based on Rust’s [compiler-builtins crate]
|
|
|
|
|
// (https://github.com/rust-lang-nursery/compiler-builtins) which
|
|
|
|
|
// provides runtime functions for the Rust programs. The Rust
|
|
|
|
|
// compiler will automatically link your programs against this crate.
|
|
|
|
|
//
|
|
|
|
|
// We copied the implementation of '__udivmodti4()' which is an intrinsic
|
|
|
|
|
// implementing division with remainder for architectures without 128-bit integer support.
|
|
|
|
|
// We have done this two reasons, to work around [bad optimization by LLVM]
|
|
|
|
|
// (https://github.com/rust-lang/rust/issues/44545) and to allow function
|
|
|
|
|
// inlining which doesn’t happen with the intrinsic.
|
|
|
|
|
|
2017-09-16 16:18:37 -04:00
|
|
|
|
pub fn udivmod_1e19(n: u128) -> (u128, u64) {
|
|
|
|
|
let d = 10_000_000_000_000_000_000_u64; // 10^19
|
|
|
|
|
|
2017-09-16 14:02:22 -04:00
|
|
|
|
let high = (n >> 64) as u64;
|
|
|
|
|
if high == 0 {
|
|
|
|
|
let low = n as u64;
|
2017-09-16 16:18:37 -04:00
|
|
|
|
return ((low / d) as u128, low % d);
|
2017-09-14 17:43:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 16:18:37 -04:00
|
|
|
|
let sr = 65 - high.leading_zeros();
|
2017-09-14 17:43:36 -04:00
|
|
|
|
|
2017-09-16 16:18:37 -04:00
|
|
|
|
// 2 <= sr <= 65
|
2017-09-16 14:02:22 -04:00
|
|
|
|
let mut q: u128 = n << (128 - sr);
|
|
|
|
|
let mut r: u128 = n >> sr;
|
|
|
|
|
let mut carry: u64 = 0;
|
2017-09-14 17:43:36 -04:00
|
|
|
|
|
|
|
|
|
// Don't use a range because they may generate references to memcpy in unoptimized code
|
2017-09-16 14:02:22 -04:00
|
|
|
|
//
|
2017-09-16 16:18:37 -04:00
|
|
|
|
// Loop invariants: r < d; carry is 0 or 1
|
2017-09-14 17:43:36 -04:00
|
|
|
|
let mut i = 0;
|
|
|
|
|
while i < sr {
|
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
|
|
// r:q = ((r:q) << 1) | carry
|
2017-09-16 14:02:22 -04:00
|
|
|
|
r = (r << 1) | (q >> 127);
|
2017-09-14 17:43:36 -04:00
|
|
|
|
q = (q << 1) | carry as u128;
|
|
|
|
|
|
|
|
|
|
// carry = 0
|
2017-09-16 16:18:37 -04:00
|
|
|
|
// if r >= d {
|
|
|
|
|
// r -= d;
|
2017-09-14 17:43:36 -04:00
|
|
|
|
// carry = 1;
|
|
|
|
|
// }
|
2017-09-16 16:18:37 -04:00
|
|
|
|
let s = (d as u128).wrapping_sub(r).wrapping_sub(1) as i128 >> 127;
|
2017-09-14 17:43:36 -04:00
|
|
|
|
carry = (s & 1) as u64;
|
2017-09-16 16:18:37 -04:00
|
|
|
|
r -= (d as u128) & s as u128;
|
2017-09-14 17:43:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 16:18:37 -04:00
|
|
|
|
((q << 1) | carry as u128, r as u64)
|
2017-09-14 17:43:36 -04:00
|
|
|
|
}
|