-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
wrong result for i128 division on 32-bit targets #41228
Comments
cc me |
All i128 intrinsics on 32 bit platforms are written in Rust. |
Apparently the top 64 bits are corrupt. |
Simplified: #![feature(i128_type)]
#[inline(never)]
fn check(x: u128, y: u128) {
assert_eq!(1, x / y);
}
fn main() {
check(3 << 64 | 1, 3 << 64);
} This should be a bug in the "XX/H0" case. The error also disappears with -O, but this may simply be an effect of constant evaluation. Edit: Further reduction shows the error comes from rustc's #![feature(compiler_builtins_lib)]
#![feature(i128_type)]
extern crate compiler_builtins;
use std::ptr::null_mut;
fn main() {
let z = compiler_builtins::reimpls::u128_div_mod(3 << 64 | 1, 3 << 64, null_mut());
assert_eq!(1, z);
} |
…g#41228. Added test cases to cover all special-cased branches of udivmodti4.
Fix invalid 128-bit division on 32-bit target (rust-lang#41228) The bug of rust-lang#41228 is a typo, this line: https://github.com/rust-lang/rust/blob/1dca19ae3fd195fa517e326a39bfee729da7cadb/src/libcompiler_builtins/lib.rs#L183 ```rust // 1 <= sr <= u64::bits() - 1 q = n.wrapping_shl(64u32.wrapping_sub(sr)); ``` The **64** should be **128**. (Compare with https://github.com/rust-lang-nursery/compiler-builtins/blob/280d19f1127aa80739f4179152b11a5f7d36d79f/src/int/udiv.rs#L213-L214: ```rust // 1 <= sr <= <hty!($ty)>::bits() - 1 q = n << (<$ty>::bits() - sr); ``` Or compare with the C implementation https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/udivmodti4.c#L113-L116 ```c /* 1 <= sr <= n_udword_bits - 1 */ /* q.all = n.all << (n_utword_bits - sr); */ q.s.low = 0; q.s.high = n.s.low << (n_udword_bits - sr); ``` ) Added a bunch of randomly generated division test cases to try to cover every described branch of `udivmodti4`.
Fix invalid 128-bit division on 32-bit target (rust-lang#41228) The bug of rust-lang#41228 is a typo, this line: https://github.com/rust-lang/rust/blob/1dca19ae3fd195fa517e326a39bfee729da7cadb/src/libcompiler_builtins/lib.rs#L183 ```rust // 1 <= sr <= u64::bits() - 1 q = n.wrapping_shl(64u32.wrapping_sub(sr)); ``` The **64** should be **128**. (Compare with https://github.com/rust-lang-nursery/compiler-builtins/blob/280d19f1127aa80739f4179152b11a5f7d36d79f/src/int/udiv.rs#L213-L214: ```rust // 1 <= sr <= <hty!($ty)>::bits() - 1 q = n << (<$ty>::bits() - sr); ``` Or compare with the C implementation https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/udivmodti4.c#L113-L116 ```c /* 1 <= sr <= n_udword_bits - 1 */ /* q.all = n.all << (n_utword_bits - sr); */ q.s.low = 0; q.s.high = n.s.low << (n_udword_bits - sr); ``` ) Added a bunch of randomly generated division test cases to try to cover every described branch of `udivmodti4`.
Fix invalid 128-bit division on 32-bit target (rust-lang#41228) The bug of rust-lang#41228 is a typo, this line: https://github.com/rust-lang/rust/blob/1dca19ae3fd195fa517e326a39bfee729da7cadb/src/libcompiler_builtins/lib.rs#L183 ```rust // 1 <= sr <= u64::bits() - 1 q = n.wrapping_shl(64u32.wrapping_sub(sr)); ``` The **64** should be **128**. (Compare with https://github.com/rust-lang-nursery/compiler-builtins/blob/280d19f1127aa80739f4179152b11a5f7d36d79f/src/int/udiv.rs#L213-L214: ```rust // 1 <= sr <= <hty!($ty)>::bits() - 1 q = n << (<$ty>::bits() - sr); ``` Or compare with the C implementation https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/udivmodti4.c#L113-L116 ```c /* 1 <= sr <= n_udword_bits - 1 */ /* q.all = n.all << (n_utword_bits - sr); */ q.s.low = 0; q.s.high = n.s.low << (n_udword_bits - sr); ``` ) Added a bunch of randomly generated division test cases to try to cover every described branch of `udivmodti4`.
I’ll confirm tomorrow. |
Fixed on nightly. Closing. Sorry for the delay. |
Thanks @nagisa for investigating :) |
STR
This is also wrong at least on i686-pc-windows-msvc. Haven't checked whether the related intrinsic is written in Rust or in C atm. But the implementation in rust-lang-nursery/compiler-builtins gives the right result.
Meta
cc @est31
The text was updated successfully, but these errors were encountered: