From fcdfd5b0b9efcf7797b0f1bf7e7ed47ff99de198 Mon Sep 17 00:00:00 2001 From: Federico Stra Date: Thu, 28 Sep 2023 13:59:19 +0200 Subject: [PATCH] isqrt: `assume` that `isqrt` takes half as many bits https://github.com/rust-lang/rust/issues/89273#issuecomment-970581089 --- library/core/src/num/uint_macros.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 565fc1930ea4f..f2190efa4d3c9 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -2017,6 +2017,13 @@ macro_rules! uint_impl { one >>= 2; } + // SAFETY: the result is positive and fits in an integer with half as many bits. + // Inform the optimizer about it. + unsafe { + intrinsics::assume(0 < res); + intrinsics::assume(res < 1 << (Self::BITS / 2)); + } + res }