We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0507842 commit c93b32bCopy full SHA for c93b32b
src/int/sdiv.rs
@@ -10,8 +10,12 @@ macro_rules! div {
10
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
11
let s_a = a >> (<$ty>::bits() - 1);
12
let s_b = b >> (<$ty>::bits() - 1);
13
- let a = (a ^ s_a) - s_a;
14
- let b = (b ^ s_b) - s_b;
+ // NOTE it's OK to overflow here because of the `as $uty` cast below
+ // This whole operation is computing the absolute value of the inputs
15
+ // So some overflow will happen when dealing with e.g. `i64::MIN`
16
+ // where the absolute value is `(-i64::MIN) as u64`
17
+ let a = (a ^ s_a).wrapping_sub(s_a);
18
+ let b = (b ^ s_b).wrapping_sub(s_b);
19
let s = s_a ^ s_b;
20
21
let r = udiv!(a as $uty, b as $uty);
0 commit comments