From 8b7d3d396761f7fe4d609f9983bd196ef127e8c9 Mon Sep 17 00:00:00 2001 From: Tobias Decking Date: Tue, 3 Dec 2024 12:40:00 +0100 Subject: [PATCH] Update the definition of `borrowing_sub` This ensures that it matches the one in `carrying_add`. --- library/core/src/num/uint_macros.rs | 2 +- ...bigint-add.rs => x86_64-bigint-helpers.rs} | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) rename tests/assembly/{x86_64-bigint-add.rs => x86_64-bigint-helpers.rs} (52%) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index c853db181cee7..c79b2f7ad8ed3 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -2445,7 +2445,7 @@ macro_rules! uint_impl { // to generate optimal code for now, and LLVM doesn't have an equivalent intrinsic let (a, b) = self.overflowing_sub(rhs); let (c, d) = a.overflowing_sub(borrow as $SelfT); - (c, b || d) + (c, b | d) } /// Calculates `self` - `rhs` with a signed `rhs` diff --git a/tests/assembly/x86_64-bigint-add.rs b/tests/assembly/x86_64-bigint-helpers.rs similarity index 52% rename from tests/assembly/x86_64-bigint-add.rs rename to tests/assembly/x86_64-bigint-helpers.rs index 4bcb9732c6405..198e554353909 100644 --- a/tests/assembly/x86_64-bigint-add.rs +++ b/tests/assembly/x86_64-bigint-helpers.rs @@ -6,8 +6,8 @@ #![no_std] #![feature(bigint_helper_methods)] -// This checks that the `carrying_add` implementation successfully chains, to catch -// issues like +// This checks that the `carrying_add` and `borrowing_sub` implementation successfully chain, +// to catch issues like // This forces the ABI to avoid the windows-vs-linux ABI differences. @@ -31,3 +31,24 @@ pub unsafe extern "sysv64" fn bigint_chain_carrying_add( } carry } + +// CHECK-LABEL: bigint_chain_borrowing_sub: +#[no_mangle] +pub unsafe extern "sysv64" fn bigint_chain_borrowing_sub( + dest: *mut u64, + src1: *const u64, + src2: *const u64, + n: usize, + mut carry: bool, +) -> bool { + // CHECK: mov [[TEMP:r..]], qword ptr [rsi + 8*[[IND:r..]] + 8] + // CHECK: sbb [[TEMP]], qword ptr [rdx + 8*[[IND]] + 8] + // CHECK: mov qword ptr [rdi + 8*[[IND]] + 8], [[TEMP]] + // CHECK: mov [[TEMP]], qword ptr [rsi + 8*[[IND]] + 16] + // CHECK: sbb [[TEMP]], qword ptr [rdx + 8*[[IND]] + 16] + // CHECK: mov qword ptr [rdi + 8*[[IND]] + 16], [[TEMP]] + for i in 0..n { + (*dest.add(i), carry) = u64::borrowing_sub(*src1.add(i), *src2.add(i), carry); + } + carry +}