Skip to content

Commit fa0d183

Browse files
Rollup merge of #163099 - maxdexh:int-from-str-loop-unchecked, r=Darksonn
Use wrapping arithmetic in `from_str_radix` Uses wrapping arithmetic in the fast/unchecked loop of `from_str_radix`. Originally this PR was written using unchecked arithmetic: This generates identical assembly when compiling with `-O`: https://godbolt.org/z/Gvb7G9YEM However, it significantly simplifies the asm of `-O -Coverflow-checks` (e.g. RfL): https://godbolt.org/z/Pah9fMfrd More precisely, the `-O -Coverflow-checks` output becomes identical to the `-O` one: https://godbolt.org/z/WoYMThP8e In conclusion, it makes more sense to use wrapping operations here.
2 parents 6d10830 + 84a341b commit fa0d183

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

‎library/core/src/num/mod.rs‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,16 @@ pub enum FpCategory {
15761576
#[inline(always)]
15771577
#[unstable(issue = "none", feature = "std_internals")]
15781578
pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
1579+
// Assume that `digits` represents a whole number N in base `radix`.
1580+
// Then in infinite precision arithmetic (on whole numbers), we have:
1581+
//
1582+
// |N| <= pow(radix, digits.len()) - 1
1583+
// <= pow(16, 2 * size_of::<T>() - is_signed) - 1
1584+
// == pow(2, 8 * size_of::<T>() - 4 * is_signed) - 1
1585+
// <= pow(2, 8 * size_of::<T>() - is_signed) - 1
1586+
// == T::MAX
1587+
//
1588+
// Therefore this condition is sufficient for having no overflow.
15791589
radix <= 16 && digits.len() <= size_of::<T>() * 2 - is_signed_ty as usize
15801590
}
15811591

@@ -1819,20 +1829,23 @@ macro_rules! from_str_int_impl {
18191829
// Consider radix 16 as it has the highest information density per digit and will thus overflow the earliest:
18201830
// `u8::MAX` is `ff` - any str of len 2 is guaranteed to not overflow.
18211831
// `i8::MAX` is `7f` - only a str of len 1 is guaranteed to not overflow.
1822-
macro_rules! run_unchecked_loop {
1823-
($unchecked_additive_op:tt) => {{
1832+
//
1833+
// NOTE: We could use unchecked arithmetic here, but we don't, based on the observation
1834+
// that it produces the same assembly as wrapping ones. See #163099.
1835+
macro_rules! run_no_check_loop {
1836+
($additive_op:ident) => {{
18241837
while let [c, rest @ ..] = digits {
1825-
result = result * (radix as $int_ty);
1838+
result = <$int_ty>::wrapping_mul(result, radix as _);
18261839
let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit);
1827-
result = result $unchecked_additive_op (x as $int_ty);
1840+
result = result.$additive_op(x as $int_ty);
18281841
digits = rest;
18291842
}
18301843
}};
18311844
}
18321845
if is_positive {
1833-
run_unchecked_loop!(+)
1846+
run_no_check_loop!(wrapping_add)
18341847
} else {
1835-
run_unchecked_loop!(-)
1848+
run_no_check_loop!(wrapping_sub)
18361849
};
18371850
} else {
18381851
macro_rules! run_checked_loop {

0 commit comments

Comments
 (0)