rust/library
Stuart Cook ae196c772d
Rollup merge of #152179 - nickkuk:overflow-direction-note, r=jhpratt
Add documentation note about signed overflow direction

In https://github.com/rust-lang/rust/issues/151989#issuecomment-3845282666 I noticed that signed overflow direction can be determined by returned wrapped value. It is not very obvious (especially, assuming additional `carry: bool` summand), but it is important if we want to add new leading (signed) limb to big integer in this case.

Examples for small summands `x, y: i8` with result extension:

| x     | y    | overflow | result as (u8, i8) |
| ----  | ---- | -------- | ------------------ |
| -1    | -128 | true     | (127, -1)          |
| 0     | -1   | false    | (255, -1)          |
| 2     | 2    | false    | (4, 0)             |
| 127   | 1    | true     | (128, 0)           |

Here is general proof.

1. Set $s=2^{N-1}$ and let's say `iN::carrying_add(x, y, c)` returns `(result, true)` then

$$
\mathrm{result}=\begin{cases}
x + y + c + 2s,& x + y + c \le -s-1,\\
x+y+c-2s,& x+y+c\ge s.
\end{cases}
$$

First case is overflowing below `iN::MIN` and we have

$$
\mathrm{result}\ge -s-s+0+2s =0;\qquad
\mathrm{result}=x + y + c + 2s\le -s-1+2s = s - 1,
$$

so we obtain $[0; s-1]$ which is exactly range of non-negative `iN`.

Second case is overflowing above `iN::MAX` and

$$
\mathrm{result}=x+y+c-2s\ge s-2s =-s;\qquad
\mathrm{result}\le s-1 + s-1+1-2s = -1,
$$

that is, $[-s,-1]$ which is exactly range of negative `iN`.

2. Now suppose that `iN::borrowing_sub(x,y,b)` returns `(result, true)` then

$$
\mathrm{result}=\begin{cases}
x - y - b + 2s,& x - y - b \le -s-1,\\
x - y - b - 2s,& x - y - b\ge s.
\end{cases}
$$

First case is overflowing below `iN::MIN` and we have

$$
\mathrm{result}\ge -s-(s-1)-1+2s =0;\qquad
\mathrm{result}=x - y - b + 2s\le -s-1+2s = s - 1.
$$

Second case is overflowing above `iN::MAX` and

$$
\mathrm{result}=x-y-b-2s\ge s-2s =-s;\qquad
\mathrm{result}\le s-1 - (-s) - 0 - 2s = -1.
$$
2026-02-18 17:29:45 +11:00
..
alloc adjust clippy to fix some of the issues 2026-02-16 17:27:40 +01:00
alloctests Rollup merge of #145024 - Kmeakin:km/optimize-slice-index/v3, r=Mark-Simulacrum 2026-02-14 22:11:52 +01:00
backtrace@28ec93b503 Update backtrace 2026-01-26 10:57:35 +00:00
compiler-builtins libm: Fix tests for lgamma 2026-02-09 04:32:04 -06:00
core Rollup merge of #152179 - nickkuk:overflow-direction-note, r=jhpratt 2026-02-18 17:29:45 +11:00
coretests core: Implement feature float_exact_integer_constants 2026-02-16 16:29:31 -05:00
panic_abort Use core via rustc-std-workspace-core in library/panic* 2025-07-31 22:47:24 +00:00
panic_unwind Remove unused features in library 2026-02-13 09:25:50 +08:00
portable-simd Merge commit 'd9aae8cc54' into sync-from-portable-simd-2026-01-28 2026-01-28 00:56:52 -05:00
proc_macro inline SameThread and CrossThread 2026-02-13 11:24:50 +00:00
profiler_builtins Fix profiler_builtins build script to handle full path to profiler lib 2025-04-11 16:57:38 +02:00
rtstartup Update cfg(bootstrap) 2025-07-01 10:55:49 -07:00
rustc-std-workspace-alloc Disable unit tests for stdlib packages that don't contain any 2025-07-24 09:15:28 +00:00
rustc-std-workspace-core Use core via rustc-std-workspace-core in library/panic* 2025-07-31 22:47:24 +00:00
rustc-std-workspace-std Disable unit tests for stdlib packages that don't contain any 2025-07-24 09:15:28 +00:00
std remove #![allow(stable_features)] from most tests 2026-02-17 08:45:08 +00:00
std_detect Remove or allow unused features in library doc and tests 2026-02-13 09:27:16 +08:00
stdarch Rollup merge of #152188 - cuviper:placeholder-stdarch, r=Mark-Simulacrum 2026-02-14 23:17:40 -05:00
sysroot compiler-builtins: Remove the no-f16-f128 feature 2026-01-11 07:02:27 -06:00
test Remove unused features in library 2026-02-13 09:25:50 +08:00
unwind Rollup merge of #152472 - lizan:wasm, r=Mark-Simulacrum 2026-02-14 23:17:41 -05:00
windows_link Update windows bindings in std 2026-01-26 10:59:16 +00:00
Cargo.lock Rollup merge of #151660 - fmease:bump-backtrace-demangler-alt, r=tgross35 2026-01-28 21:10:51 +01:00
Cargo.toml Update windows bindings in std 2026-01-26 10:59:16 +00:00