about summary refs log tree commit diff
path: root/library/core/src/num/mod.rs
AgeCommit message (Collapse)AuthorLines
2025-09-21Change the cfg to a dashBen Kimock-2/+2
2025-09-21Add panic=immediate-abortBen Kimock-2/+2
2025-09-04Rollup merge of #145690 - sayantn:integer-funnel-shift, r=tgross35Jacob Pratt-0/+24
Implement Integer funnel shifts Tracking issue: rust-lang/rust#145686 ACP: https://github.com/rust-lang/libs-team/issues/642 This implements funnel shifts on primitive integer types. Implements this for cg_llvm, with a fallback impl for everything else Thanks `@folkertdev` for the fixes and tests cc `@rust-lang/libs-api`
2025-09-03Add `funnel_sh{l,r}` functions and intrinsicssayantn-0/+24
- Add a fallback implementation for the intrinsics - Add LLVM backend support for funnel shifts Co-Authored-By: folkertdev <folkert@folkertdev.nl>
2025-09-01Constify conversion traitsltdk-1/+1
2025-07-21Constify Try, From, TryFromEvgenii Zheltonozhskii-1/+2
2025-06-20integer docs: remove extraneous textTshepang Mbambo-4/+0
"Basic usage" implies there is an example that shows advanced usage, but these APIs are extra simple.
2025-06-12chore(doctest): Remove redundant blank linesShun Sakai-1/+0
2025-05-17Auto merge of #138087 - tgross35:core-float-math, r=Amanieubors-0/+1
Initial implementation of `core_float_math` Since [1], `compiler-builtins` makes a certain set of math symbols weakly available on all platforms. This means we can begin exposing some of the related functions in `core`, so begin this process here. It is not possible to provide inherent methods in both `core` and `std` while giving them different stability gates, so standalone functions are added instead. This provides a way to experiment with the functionality while unstable; once it is time to stabilize, they can be converted to inherent. For `f16` and `f128`, everything is unstable so we can move the inherent methods. The following are included to start: * floor * ceil * round * round_ties_even * trunc * fract * mul_add * div_euclid * rem_euclid * powi * sqrt * abs_sub * cbrt These mirror the set of functions that we have in `compiler-builtins` since [1], with the exception of `powi` that has been there longer. Details for each of the changes is in the commit messages. Tracking issue: https://github.com/rust-lang/rust/issues/137578 [1]: https://github.com/rust-lang/compiler-builtins/pull/763 try-job: aarch64-gnu tru-job: armhf-gnu try-job: i686-msvc-1 try-job: test-various try-job: x86_64-mingw-1 try-job: x86_64-mingw-2
2025-05-16Add assert_unsafe_precondition!()s to as_ascii_unchecked() methodssam skeoch-0/+6
2025-05-16Add as_ascii_unchecked() methods to char, str, and u8sam skeoch-0/+14
2025-05-13Initial implementation of `core_float_math`Trevor Gross-0/+1
Since [1], `compiler-builtins` makes a certain set of math symbols weakly available on all platforms. This means we can begin exposing some of the related functions in `core`, so begin this process here. It is not possible to provide inherent methods in both `core` and `std` while giving them different stability gates, so standalone functions are added instead. This provides a way to experiment with the functionality while unstable; once it is time to stabilize, they can be converted to inherent. For `f16` and `f128`, everything is unstable so we can move the inherent methods. The following are included to start: * floor * ceil * round * round_ties_even * trunc * fract * mul_add * div_euclid * rem_euclid * powi * sqrt * abs_sub * cbrt These mirror the set of functions that we have in `compiler-builtins` since [1]. Tracking issue: https://github.com/rust-lang/rust/issues/137578 [1]: https://github.com/rust-lang/compiler-builtins/pull/763
2025-04-24Mention average in midpoint documentationsUrgau-4/+14
2025-04-10Auto merge of #139279 - BoxyUwU:bump-boostrap, r=jieyouxubors-4/+4
Bump boostrap compiler to new beta try-job: `*msvc*`
2025-04-09replace version placeholderBoxy-4/+4
2025-04-08Update 'u8'-to-and-from-'i8' suggestions;Gabriel BjΓΈrnager Jensen-2/+2
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-3/+3
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-02-20Rollup merge of #134340 - Urgau:stabilize-num_midpoint_signed, r=scottmcmJubilee-4/+4
Stabilize `num_midpoint_signed` feature This PR proposes that we stabilize the signed variants of [`iN::midpoint`](https://github.com/rust-lang/rust/issues/110840#issue-1684506201), the operation is equivalent to doing `(a + b) / 2` in a sufficiently large number. The stabilized API surface would be: ```rust /// Calculates the middle point of `self` and `rhs`. /// /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a /// sufficiently-large signed integer type. This implies that the result is /// always rounded towards zero and that no overflow will ever occur. impl i{8,16,32,64,128,size} { pub const fn midpoint(self, rhs: Self) -> Self; } ``` T-libs-api previously stabilized the unsigned (and float) variants in #131784, the signed variants were left out because of the rounding that should be used in case of negative midpoint. This stabilization proposal proposes that we round towards zero because: - it makes the obvious `(a + b) / 2` in a sufficiently-large number always true - using another rounding for the positive result would be inconsistent with the unsigned variants - it makes `midpoint(-a, -b)` == `-midpoint(a, b)` always true - it is consistent with `midpoint(a as f64, b as f64) as i64` - it makes it possible to always suggest `midpoint` as a replacement for `(a + b) / 2` expressions *(which we may want to do as a future work given the 21.2k hits on [GitHub Search](https://github.com/search?q=lang%3Arust+%2F%5C%28%5Ba-zA-Z_%5D*+%5C%2B+%5Ba-zA-Z_%5D*%5C%29+%5C%2F+2%2F&type=code&p=1))* `@scottmcm` mentioned a drawback in https://github.com/rust-lang/rust/pull/132191#issuecomment-2439891200: > I'm torn, because rounding towards zero makes it "wider" than other values, which `>> 1` avoids -- `(a + b) >> 1` has the nice behaviour that `midpoint(a, b) + 2 == midpoint(a + 2, b + 2)`. > > But I guess overall sticking with `(a + b) / 2` makes sense as well, and I do like the negation property 🀷 Which I think is outweigh by the advantages cited above. Closes #110840 cc `@rust-lang/libs-api` cc `@scottmcm` r? `@dtolnay`
2025-01-30Auto merge of #134824 - niklasf:int_from_ascii, r=ibraheemdevbors-89/+131
Implement `int_from_ascii` (#134821) Provides unstable `T::from_ascii()` and `T::from_ascii_radix()` for integer types `T`, as drafted in tracking issue #134821. To deduplicate documentation without additional macros, implementations of `isize` and `usize` no longer delegate to equivalent integer types. After #132870 they are inlined anyway.
2025-01-27Stabilize `num_midpoint_signed` featureUrgau-4/+4
2025-01-09Update a bunch of library types for MCP807Scott McMurray-0/+4
This greatly reduces the number of places that actually use the `rustc_layout_scalar_valid_range_*` attributes down to just 3: ``` library/core\src\ptr\non_null.rs 68:#[rustc_layout_scalar_valid_range_start(1)] library/core\src\num\niche_types.rs 19: #[rustc_layout_scalar_valid_range_start($low)] 20: #[rustc_layout_scalar_valid_range_end($high)] ``` Everything else -- PAL Nanoseconds, alloc's `Cap`, niched FDs, etc -- all just wrap those `niche_types` types.
2025-01-08update version placeholdersPietro Albini-4/+4
2024-12-27Implement `int_from_ascii` (#134821)Niklas Fiekas-89/+131
Provides unstable `T::from_ascii()` and `T::from_ascii_radix()` for integer types `T`, as drafted in tracking issue #134821. To deduplicate documentation without additional macros, implementations of `isize` and `usize` no longer delegate to equivalent integer types. After #132870 they are inlined anyway.
2024-12-27Move `{widening, carrying}_mul` to an intrinsic with fallback MIRScott McMurray-135/+0
Including implementing it for `u128`, so it can be defined in `uint_impl!`. This way it works for all backends, including CTFE.
2024-12-11Add a note saying that `{u8,i8}::from_{be,le,ne}_bytes` is meaninglessTobias Bucher-4/+29
2024-12-01Rollup merge of #131784 - Urgau:stabilize-midpoint, r=dtolnayJacob Pratt-14/+14
Stabilize unsigned and float variants of `num_midpoint` feature This PR proposes that we stabilize the unsigned variants of the [`num_midpoint`](https://github.com/rust-lang/rust/issues/110840#issue-1684506201) feature as well as the floats variants, since they are not subject to any unresolved questions, which is equivalent to doing `(a + b) / 2` (and `(a + b) >> 1`) in a sufficiently large number. The stabilized API surface would be: ```rust /// Calculates the middle point of `self` and `rhs`. /// /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a sufficiently-large unsigned integral type. /// This implies that the result is always rounded towards negative infinity and that no overflow will ever occur. impl u{8,16,32,64,128,size} { pub const fn midpoint(self, rhs: Self) -> Self; } impl NonZeroU{8,16,32,64,size} { pub const fn midpoint(self, rhs: Self) -> Self; } impl f{32,64} { pub const fn midpoint(self, rhs: Self) -> Self; } ``` The signed variants `u{8,16,32,64,128,size}` would remain gated, until a decision is made about the rounding mode, in other words that the [unresolved questions](https://github.com/rust-lang/rust/issues/110840#issue-1684506201) are resolved. cc `@rust-lang/libs-api` cc `@scottmcm` r? libs-api
2024-12-01Stabilize unsigned `num_midpoint` featureUrgau-14/+14
2024-11-27update cfgsBoxy-1/+0
2024-11-27replace placeholder versionBoxy-2/+2
2024-11-13Auto merge of #132972 - matthiaskrgr:rollup-456osr7, r=matthiaskrgrbors-1/+1
Rollup of 7 pull requests Successful merges: - #132702 (CFI: Append debug location to CFI blocks) - #132851 (Update the doc comment of `ASCII_CASE_MASK`) - #132948 (stabilize const_unicode_case_lookup) - #132950 (Use GNU ld on m68k-unknown-linux-gnu) - #132962 (triagebot: add codegen reviewers) - #132966 (stabilize const_option_ext) - #132970 (Add tracking issue number to unsigned_nonzero_div_ceil feature) r? `@ghost` `@rustbot` modify labels: rollup
2024-11-12Auto merge of #132870 - Noratrieb:inline-int-parsing, r=tgross35bors-0/+3
`#[inline]` integer parsing functions This improves the performance of `str::parse` into integers. Before: ``` compare fastest β”‚ slowest β”‚ median β”‚ mean β”‚ samples β”‚ iters ╰─ std β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€ 328920585 10.23 ns β”‚ 24.8 ns β”‚ 10.34 ns β”‚ 10.48 ns β”‚ 100 β”‚ 25600 β”œβ”€ 3255 8.551 ns β”‚ 8.59 ns β”‚ 8.551 ns β”‚ 8.56 ns β”‚ 100 β”‚ 25600 ╰─ 5 7.847 ns β”‚ 7.887 ns β”‚ 7.847 ns β”‚ 7.853 ns β”‚ 100 β”‚ 25600 ``` After: ``` compare fastest β”‚ slowest β”‚ median β”‚ mean β”‚ samples β”‚ iters ╰─ std β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€ 328920585 8.316 ns β”‚ 23.7 ns β”‚ 8.355 ns β”‚ 8.491 ns β”‚ 100 β”‚ 25600 β”œβ”€ 3255 4.566 ns β”‚ 4.588 ns β”‚ 4.586 ns β”‚ 4.576 ns β”‚ 100 β”‚ 51200 ╰─ 5 2.877 ns β”‚ 3.697 ns β”‚ 2.896 ns β”‚ 2.945 ns β”‚ 100 β”‚ 102400 ``` Benchmark: ```rust fn std(input: &str) -> Result<u64, ParseIntError> { input.parse() } ```
2024-11-10`#[inline]` integer parsing functionsNoratrieb-0/+3
This improves the performance of `str::parse` into integers. Before: ``` compare fastest β”‚ slowest β”‚ median β”‚ mean β”‚ samples β”‚ iters ╰─ std β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€ 328920585 10.23 ns β”‚ 24.8 ns β”‚ 10.34 ns β”‚ 10.48 ns β”‚ 100 β”‚ 25600 β”œβ”€ 3255 8.551 ns β”‚ 8.59 ns β”‚ 8.551 ns β”‚ 8.56 ns β”‚ 100 β”‚ 25600 ╰─ 5 7.847 ns β”‚ 7.887 ns β”‚ 7.847 ns β”‚ 7.853 ns β”‚ 100 β”‚ 25600 ``` After: ``` compare fastest β”‚ slowest β”‚ median β”‚ mean β”‚ samples β”‚ iters ╰─ std β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€ 328920585 8.316 ns β”‚ 23.7 ns β”‚ 8.355 ns β”‚ 8.491 ns β”‚ 100 β”‚ 25600 β”œβ”€ 3255 4.566 ns β”‚ 4.588 ns β”‚ 4.586 ns β”‚ 4.576 ns β”‚ 100 β”‚ 51200 ╰─ 5 2.877 ns β”‚ 3.697 ns β”‚ 2.896 ns β”‚ 2.945 ns β”‚ 100 β”‚ 102400 ``` Benchmark: ```rust fn std(input: &str) -> Result<u64, ParseIntError> { input.parse() } ```
2024-11-10Update the doc comment of `ASCII_CASE_MASK`chansuke-1/+1
2024-11-05add const_eval_select macro to reduce redundancyRalf Jung-1/+1
also move internal const_panic helpers to a better location
2024-11-03Auto merge of #132542 - RalfJung:const_panic, r=tgross35bors-14/+7
add const_panic macro to make it easier to fall back to non-formatting panic in const Suggested by `@tgross35` r? `@tgross35`
2024-11-03add const_panic macro to make it easier to fall back to non-formatting panic ↡Ralf Jung-14/+7
in const
2024-11-02get rid of a whole bunch of unnecessary rustc_const_unstable attributesRalf Jung-8/+0
2024-10-27Use Hacker's Delight impl in `i64::midpoint` instead of wide `i128` implUrgau-2/+2
As LLVM seems to be outperformed by the complexity of signed 128-bits number compared to our Hacker's Delight implementation.[^1] It doesn't seems like it's an improvement for the other sizes[^2], so we let them with the wide implementation. [^1]: https://rust.godbolt.org/z/ravE75EYj [^2]: https://rust.godbolt.org/z/fzr171zKh
2024-10-26Round negative signed integer towards zero in `iN::midpoint`Urgau-0/+65
Instead of towards negative infinity as is currently the case. This done so that the obvious expectations of `midpoint(a, b) == midpoint(b, a)` and `midpoint(-a, -b) == -midpoint(a, b)` are true, which makes the even more obvious implementation `(a + b) / 2` true. https://github.com/rust-lang/rust/issues/110840#issuecomment-2336753931
2024-10-25get rid of the internal unlikely macroRalf Jung-7/+0
2024-10-25Re-do recursive const stability checksRalf Jung-2/+3
Fundamentally, we have *three* disjoint categories of functions: 1. const-stable functions 2. private/unstable functions that are meant to be callable from const-stable functions 3. functions that can make use of unstable const features This PR implements the following system: - `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions. - `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category. - `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls. Also, several holes in recursive const stability checking are being closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to *not* be `rustc_const_unstable` (or manually get a `rustc_const_stable_indirect`) to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine. The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability. Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.
2024-10-15update bootstrap configsJosh Stone-2/+0
2024-10-14Stabilise 'const_make_ascii'Gabriel BjΓΈrnager Jensen-2/+4
2024-09-23Rollup merge of #130713 - bjoernager:const-char-make-ascii, r=NoratriebMatthias KrΓΌger-2/+4
Mark `u8::make_ascii_uppercase` and `u8::make_ascii_lowercase` as const. Relevant tracking issue: #130698 This PR extends #130697 by also marking the `make_ascii_uppercase` and `make_ascii_lowercase` methods in `u8` as const. The `const_char_make_ascii` feature gate is additionally renamed to `const_make_ascii`.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-2/+2
2024-09-22Mark 'make_ascii_uppercase' and 'make_ascii_lowercase' in 'u8' as const; ↡Gabriel BjΓΈrnager Jensen-2/+4
Rename 'const_char_make_ascii' feature gate to 'const_make_ascii';
2024-09-19Rollup merge of #128001 - Krappa322:master, r=scottmcmMatthias KrΓΌger-16/+53
Improve documentation for <integer>::from_str_radix Two improvements to the documentation: - Document `-` as a valid character for signed integer destinations - Make the documentation even more clear that extra whitespace and non-digit characters is invalid. Many other languages, e.g. c++, are very permissive in string to integer routines and simply try to consume as much as they can, ignoring the rest. This is trying to make the transition for developers who are used to the conversion semantics in these languages a bit easier.
2024-09-08add FIXME(const-hack)Ralf Jung-1/+1
2024-09-03replace placeholder versionBoxy-3/+3
2024-08-31Improve documentation for <integer>::from_str_radixKappa322-16/+53
Two improvements to the documentation: - Document `-` as a valid character for signed integer destinations - Make the documentation even more clear that extra whitespace and non-digit characters is invalid. Many other languages, e.g. c++, are very permissive in string to integer routines and simply try to consume as much as they can, ignoring the rest. This is trying to make the transition for developers who are used to the conversion semantics in these languages a bit easier.