Skip to content

Commit 401e98e

Browse files
authored
Eliminate WideWord casts from borrowing_sub (#1029)
This works around rust-lang/rust#149522, which was leading to hangs on linux-aarch64 in some code paths dependent upon `borrowing_sub`.
1 parent 12cffe9 commit 401e98e

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/primitives.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub(crate) const fn overflowing_add(lhs: Word, rhs: Word) -> (Word, Word) {
3131
/// Computes `lhs - (rhs + borrow)`, returning the result along with the new borrow.
3232
#[inline(always)]
3333
pub(crate) const fn borrowing_sub(lhs: Word, rhs: Word, borrow: Word) -> (Word, Word) {
34-
let a = lhs as WideWord;
35-
let b = rhs as WideWord;
36-
let borrow = (borrow >> (Word::BITS - 1)) as WideWord;
37-
let ret = a.wrapping_sub(b + borrow);
38-
(ret as Word, (ret >> Word::BITS) as Word)
34+
// XXX we cannot use WideWord casts here: https://github.com/rust-lang/rust/issues/149522
35+
// rustc 1.87 through 1.91 incorrectly optimize some WideWord bit arithmetic.
36+
let (ret, b2) = lhs.overflowing_sub(borrow >> (Word::BITS - 1));
37+
let (ret, b1) = ret.overflowing_sub(rhs);
38+
(ret, Word::MIN.wrapping_sub((b1 | b2) as Word))
3939
}
4040

4141
/// Computes `lhs * rhs`, returning the low and the high words of the result.

0 commit comments

Comments
 (0)