rust/library/core/src
Mara Bos e7a247dba4
Rollup merge of #85017 - clarfonthey:carrying_widening, r=m-ou-se
Add carrying_add, borrowing_sub, widening_mul, carrying_mul methods to integers

This comes in part from my own attempts to make (crude) big integer implementations, and also due to the stalled discussion in [RFC 2417](https://github.com/rust-lang/rfcs/pull/2417). My understanding is that changes like these are best offered directly as code and then an RFC can be opened if there needs to be more discussion before stabilisation. Since all of these methods are unstable from the start, I figured I might as well offer them now.

I tried looking into intrinsics, messed around with a few different implementations, and ultimately concluded that these are "good enough" implementations for now to at least put up some code and maybe start bikeshedding on a proper API for these.

For the `carrying_add` and `borrowing_sub`, I tried looking into potential architecture-specific code and realised that even using the LLVM intrinsics for `addcarry` and `subborrow` on x86 specifically, I was getting exactly the same assembly as the naive implementation using `overflowing_add` and `overflowing_sub`, although the LLVM IR did differ because of the architecture-specific code. Longer-term I think that they would be best suited to specific intrinsics as that would make optimisations easier (instructions like add-carry tend to use implicit flags, and thus can only be optimised if they're done one-after-another, and thus it would make the most sense to have compact intrinsics that can be merged together easily).

For `widening_mul` and `carrying_mul`, for now at least, I simply cast to the larger type and perform arithmetic that way, since we currently have no intrinsic that would work better for 128-bit integers. In the future, I also think that some form of intrinsic would work best to cover that case, but for now at least, I think that they're "good enough" for now.

The main reasoning for offering these directly to the standard library even though they're relatively niche optimisations is to help ensure that the code generated for them is optimal. Plus, these operations alone aren't enough to create big integer implementations, although they could help simplify the code required to do so and make it a bit more accessible for the average implementor.

That said, I 100% understand if any or all of these methods are not desired simply because of how niche they are. Up to you. 🤷🏻
2021-08-31 17:54:52 +02:00
..
alloc Consistent spelling of "adapter" in the standard library 2021-07-30 17:23:07 +02:00
array Fix more “a”/“an” typos 2021-08-22 16:35:29 +02:00
char Fix more “a”/“an” typos 2021-08-22 17:27:18 +02:00
convert impl const From<num> for num 2021-08-07 19:03:08 +02:00
fmt Get piece unchecked in write 2021-08-16 16:28:16 +00:00
future Fix may not to appropriate might not or must not 2021-07-29 01:15:20 -04:00
hash Rollup merge of #88031 - ibraheemdev:build-hasher-object-safe, r=m-ou-se 2021-08-18 19:54:57 +02:00
iter Rollup merge of #88273 - jhpratt:update-iterator-docs, r=jyn514 2021-08-25 15:48:55 +02:00
macros Make Arguments constructors unsafe 2021-08-16 16:28:16 +00:00
mem Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
num Rollup merge of #85017 - clarfonthey:carrying_widening, r=m-ou-se 2021-08-31 17:54:52 +02:00
ops Remove the TryV2 alias 2021-08-21 15:09:03 -07:00
panic Fix unused sync::atomic import on targets without atomics 2021-07-31 17:27:29 -07:00
prelude Move asm! and global_asm! to core::arch 2021-07-18 18:30:58 -04:00
ptr Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
slice Add SAFETY comments to core::slice::sort::partition_in_blocks 2021-08-24 16:47:26 -03:00
str Constified Default implementations 2021-08-17 07:15:54 +00:00
stream Add core::stream::from_iter 2021-06-23 17:49:26 +02:00
sync Constified Default implementations 2021-08-17 07:15:54 +00:00
task Remove the TryV2 alias 2021-08-21 15:09:03 -07:00
unicode Use HTTPS links where possible 2021-06-23 16:26:46 -04:00
any.rs Added diagnostic items to structs and traits for Clippy 2021-07-15 23:57:02 +02:00
ascii.rs Replace all fmt.pad with debug_struct 2021-04-21 14:38:24 +02:00
bool.rs Fix tracking issue for bool_to_option 2021-07-10 18:43:52 -04:00
borrow.rs Fix borrow and deref 2021-03-03 11:23:29 +01:00
cell.rs add Cell::as_array_of_cells, similar to Cell::as_slice_of_cells 2021-08-23 01:00:34 -04:00
clone.rs Updates Clone docs for Copy comparison. 2021-06-10 11:28:26 -04:00
cmp.rs Added diagnostic items to functions for Clippy 2021-07-15 23:47:03 +02:00
default.rs Constified Default implementations 2021-08-17 07:15:54 +00:00
ffi.rs Replace all fmt.pad with debug_struct 2021-04-21 14:38:24 +02:00
hint.rs Allow the use of the deprecated llvm_asm! in black_box 2021-08-15 13:14:32 +01:00
internal_macros.rs Remove unnecessary #[unstable] from internal macro. 2021-08-05 12:55:35 +02:00
intrinsics.rs Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
lazy.rs Capitalize safety comments 2020-09-08 22:26:44 -04:00
lib.rs Add carrying_add, borrowing_sub, widening_mul, carrying_mul methods to integers 2021-08-26 19:52:06 -04:00
marker.rs Constified Default implementations 2021-08-17 07:15:54 +00:00
option.rs Remove the TryV2 alias 2021-08-21 15:09:03 -07:00
panic.rs Move UnwindSafe, RefUnwindSafe, AssertUnwindSafe to core 2021-07-30 10:42:15 -07:00
panicking.rs Make Arguments constructors unsafe 2021-08-16 16:28:16 +00:00
pin.rs Fix may not to appropriate might not or must not 2021-07-29 01:15:20 -04:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
result.rs Remove the TryV2 alias 2021-08-21 15:09:03 -07:00
time.rs Adding examples to docs of std::time module 2021-08-29 23:59:35 -03:00
tuple.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
unit.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00