Skip to content

Commit b8b8519

Browse files
scottmcmgitbot
authored and
gitbot
committed
Override carrying_mul_add in cg_llvm
1 parent 4780a73 commit b8b8519

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

core/src/intrinsics/fallback.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ impl const CarryingMulAdd for i128 {
100100
fn carrying_mul_add(self, b: i128, c: i128, d: i128) -> (u128, i128) {
101101
let (low, high) = wide_mul_u128(self as u128, b as u128);
102102
let mut high = high as i128;
103-
high = high.wrapping_add((self >> 127) * b);
104-
high = high.wrapping_add(self * (b >> 127));
103+
high = high.wrapping_add(i128::wrapping_mul(self >> 127, b));
104+
high = high.wrapping_add(i128::wrapping_mul(self, b >> 127));
105105
let (low, carry) = u128::overflowing_add(low, c as u128);
106106
high = high.wrapping_add((carry as i128) + (c >> 127));
107107
let (low, carry) = u128::overflowing_add(low, d as u128);

core/tests/intrinsics.rs

+10
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ fn carrying_mul_add_fallback_i32() {
153153

154154
#[test]
155155
fn carrying_mul_add_fallback_u128() {
156+
assert_eq!(fallback_cma::<u128>(u128::MAX, u128::MAX, 0, 0), (1, u128::MAX - 1));
156157
assert_eq!(fallback_cma::<u128>(1, 1, 1, 1), (3, 0));
157158
assert_eq!(fallback_cma::<u128>(0, 0, u128::MAX, u128::MAX), (u128::MAX - 1, 1));
158159
assert_eq!(
@@ -178,8 +179,17 @@ fn carrying_mul_add_fallback_u128() {
178179

179180
#[test]
180181
fn carrying_mul_add_fallback_i128() {
182+
assert_eq!(fallback_cma::<i128>(-1, -1, 0, 0), (1, 0));
181183
let r = fallback_cma::<i128>(-1, -1, -1, -1);
182184
assert_eq!(r, (u128::MAX, -1));
183185
let r = fallback_cma::<i128>(1, -1, 1, 1);
184186
assert_eq!(r, (1, 0));
187+
assert_eq!(
188+
fallback_cma::<i128>(i128::MAX, i128::MAX, i128::MAX, i128::MAX),
189+
(u128::MAX, i128::MAX / 2),
190+
);
191+
assert_eq!(
192+
fallback_cma::<i128>(i128::MIN, i128::MIN, i128::MAX, i128::MAX),
193+
(u128::MAX - 1, -(i128::MIN / 2)),
194+
);
185195
}

0 commit comments

Comments
 (0)