rust/library/core/src
León Orell Valerian Liehr 4f7d9d4ad8
Rollup merge of #125038 - ivan-shrimp:checked_sub, r=joboet
Invert comparison in `uN::checked_sub`

After #124114, LLVM no longer combines the comparison and subtraction in `uN::checked_sub` when either operand is a constant (demo: https://rust.godbolt.org/z/MaeoYbsP1). The difference is more pronounced when the expression is slightly more complex (https://rust.godbolt.org/z/4rPavsYdc).

This is due to the use of `>=` here:

ee97564e3a/library/core/src/num/uint_macros.rs (L581-L593)

For constant `C`, LLVM eagerly converts `a >= C` into `a > C - 1`, but the backend can only combine `a < C` with `a - C`, not `C - 1 < a` and `a - C`: e586556e37/llvm/lib/CodeGen/CodeGenPrepare.cpp (L1697-L1742)

This PR[^1] simply inverts the `>=` into `<` to restore the LLVM magic, and somewhat align this with the implementation of `uN::overflowing_sub` from #103299.

When the result is stored as an `Option` (rather than being branched/cmoved on), the discriminant is `self >= rhs`. This PR doesn't affect the codegen (and relevant tests) of that since LLVM will negate `self < rhs` to `self >= rhs` when necessary.

[^1]: Note to `self`: My very first contribution to publicly-used code. Hopefully like what I should learn to always be, tiny and humble.
2024-05-15 14:21:38 +02:00
..
alloc alloc::Layout: explicitly document size invariant on the type level 2024-03-25 20:18:46 +01:00
array Stabilize generic NonZero. 2024-04-22 18:48:47 +02:00
ascii implement Default for AsciiChar 2024-02-13 12:04:44 +01:00
async_iter Hide async_gen_internals from standard library documentation 2024-04-05 18:54:38 -07:00
cell impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock 2024-03-27 16:16:08 +08:00
char Improve escape methods. 2024-05-09 17:04:30 +02:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert Remove From impls for unstable types that break inference 2024-04-11 18:04:31 -04:00
ffi Rollup merge of #124593 - GKFX:cstr-literals-in-api-docs, r=workingjubilee 2024-05-03 20:33:46 +02:00
fmt Document proper usage of fmt::Error and fmt()'s Result. 2024-05-09 17:58:38 -07:00
future Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
hash Add fn const BuildHasherDefault::new 2024-03-29 17:10:17 +01:00
intrinsics Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
io core: panic on overflow in BorrowedCursor 2024-04-11 18:33:46 +02:00
iter Use generic NonZero. 2024-05-08 21:37:55 +02:00
macros Workaround rustfmt bug replacing type ascription 2024-05-01 23:23:00 -04:00
mem Fix typo in ManuallyDrop's documentation 2024-05-10 23:13:32 +00:00
net Avoid invalid socket address in length calculation 2024-04-11 17:27:23 +02:00
num reverse condition in uN::checked_sub 2024-05-12 11:29:24 +08:00
ops Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
panic Replace version placeholders for 1.79 2024-05-01 21:01:51 -04:00
prelude Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
ptr Rollup merge of #116675 - joshlf:patch-10, r=scottmcm 2024-05-13 21:14:15 -04:00
slice Stabilize byte_slice_trim_ascii for &[u8]/&str 2024-05-10 23:42:40 -04:00
str Stabilize byte_slice_trim_ascii for &[u8]/&str 2024-05-10 23:42:40 -04:00
sync Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
task Rollup merge of #122201 - coolreader18:doc-clone_from, r=dtolnay 2024-04-17 18:01:37 +02:00
unicode Add a lower bound check to unicode-table-generator output 2024-04-20 10:16:45 +02:00
any.rs Improve wording in std::any explanation 2024-03-29 10:10:52 -07:00
arch.rs Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
ascii.rs Improve escape methods. 2024-05-09 17:04:30 +02:00
asserting.rs [RFC 2011] Library code 2022-05-22 07:18:32 -03:00
bool.rs core is now compilable 2023-04-16 07:20:26 +00:00
borrow.rs doc: replace wrong punctuation mark 2023-07-28 14:46:17 +02:00
cell.rs Rollup merge of #123480 - Nadrieril:impl-all-derefpures, r=compiler-errors 2024-05-03 06:04:19 +02:00
clone.rs Revert "Put basic impls for f16 and f128 behind cfg(not(bootstrap))" 2024-04-10 13:50:27 -04:00
cmp.rs Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
default.rs Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
error.md Fix minor grammar typo 2023-09-06 09:47:22 -07:00
error.rs style library/core/src/error.rs 2024-03-02 16:03:23 +08:00
escape.rs Improve escape methods. 2024-05-09 17:04:30 +02:00
hint.rs Add support for Arm64EC to the Standard Library 2024-04-15 16:05:16 -07:00
internal_macros.rs add track_caller for arith ops 2023-11-24 00:54:06 +08:00
intrinsics.rs Rollup merge of #124293 - oli-obk:miri_intrinsic_fallback_body, r=RalfJung 2024-05-04 12:37:22 +02:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Rollup merge of #124678 - UserIsntAvailable:feat/stabilize-split-at-checked, r=jhpratt 2024-05-03 23:34:24 -04:00
marker.rs Revert "Put basic impls for f16 and f128 behind cfg(not(bootstrap))" 2024-04-10 13:50:27 -04:00
option.rs panic_str only exists for the migration to 2021 panic macros 2024-03-26 08:11:34 +01:00
panic.rs panic_str only exists for the migration to 2021 panic macros 2024-03-26 08:11:34 +01:00
panicking.rs Step bootstrap cfgs 2024-05-01 22:19:11 -04:00
pat.rs Add pattern types to parser 2024-04-08 11:57:17 +00:00
pin.rs Rollup merge of #123480 - Nadrieril:impl-all-derefpures, r=compiler-errors 2024-05-03 06:04:19 +02:00
primitive.rs
primitive_docs.rs References must also be non-null 2024-05-11 12:08:19 -07:00
result.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00
time.rs Fix cannot usage in time.rs 2024-04-24 17:37:44 +00:00
tuple.rs Rollup merge of #118307 - scottmcm:tuple-eq-simpler, r=joshtriplett 2024-02-11 08:25:41 +01:00
ub_checks.rs export assert_unsafe_precondition macro for std-internal use 2024-04-22 19:02:37 +02:00
unit.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00