From c93b32b3e50182a2a0ff5ee776c5fa534102e3e7 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 6 Mar 2017 17:46:11 -0500 Subject: [PATCH] fix debug assertion in divdi3 fixes #148 --- src/int/sdiv.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/int/sdiv.rs b/src/int/sdiv.rs index 51d9c37..f9fd466 100644 --- a/src/int/sdiv.rs +++ b/src/int/sdiv.rs @@ -10,8 +10,12 @@ macro_rules! div { 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; + // 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 + // So some overflow will happen when dealing with e.g. `i64::MIN` + // where the absolute value is `(-i64::MIN) as u64` + let a = (a ^ s_a).wrapping_sub(s_a); + let b = (b ^ s_b).wrapping_sub(s_b); let s = s_a ^ s_b; let r = udiv!(a as $uty, b as $uty);