-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue for more const int functions #53718
Comments
|
@TheDarkula Thank you, I'll start implementing them as soon as #53699 is merged. |
Added rest of |
I'm currently rebasing #53697 and testing. Then the first bunch should be ready to merge. |
Add more const int ops r? @oli-obk Tracking Issue: #53718 list of `const fn`s in this PR: - `feature = const_int_rotate` - `rotate_left` - `rotate_right` - `feature = const_int_wrapping` - `wrapping_add` - `wrapping_sub` - `wrapping_mul` - `wrapping_shl` - `wrapping_shr` - `feature = const_int_overflowing` - `overflowing_add` - `overflowing_sub` - `overflowing_mul` - `overflowing_shl` - `overflowing_shr` - `feature = const_int_sign` - `is_positive` - `is_negative` - `feature = const_int_conversion` - `reverse_bits` - `to_le_bytes` - `to_ne_bytes` - `from_be_bytes` - `from_le_bytes` - `from_ne_bytes` - `reverse_bits`
Adding a comment here so I don't have to hunt for the following related issue: #51267 is the tracking issue for
|
wrapping_neg isn't on the list here it seems (though overflowing_neg is) |
It's possible to make a great number of these |
In fact, we are already doing that to make Lines 51 to 63 in d132f54
|
@mark-i-m That's probably where I first saw this technique! My point was that making a similar modification would likely slow down these arithmetic operations considerably. The use in |
Make `i*::signum` a `const fn`. Ticks a box in #53718. This uses a well-known branchless implementation of `signum`: `(n > 0) as i32 - (n < 0) as i32`. Here's a [playground](https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=747cf191c4974bf66c9d75e509ae6e6e) comparing the two techniques. On x86 in release mode, the branchless implementation is able to replace a `mov` and `cmov` with a `sar` and `add`, so this should be a bit faster as well. ~~This is marked as a draft since I think I'll need to add `#[rustc_const_unstable]` somewhere. Perhaps the reviewer can point me in the right direction.~~
@TimDiekmann. Now that #61635 has been merged, |
Some of these could be made
For
|
Also, |
|
@jonas-schievink Currently, |
Actually, quite a few of these are now implemented. I've edited the post to reflect this. If anyone sees something out-of-date, let me know. |
@ecstatic-morse The intrinsic can be |
You need to handle zero somehow without branching, and you need to do so without regressing performance for non-const code. (Also, I meant leading zeros above, my bad). |
Should |
With rust-lang#49146 merged, NonZero*::new can be const; see rust-lang#53718.
With rust-lang#49146 merged, {u8,i8,u16,...}::overflowing_div and ::overflowing_rem can be const; see rust-lang#53718.
With rust-lang#49146 merged, these can be const; see rust-lang#53718.
With rust-lang#49146 merged, these can be const; see rust-lang#53718.
With rust-lang#49146 merged, these can be const; see rust-lang#53718.
… r=oli-obk Make more arithmetic functions unstably const This is a smaller version of rust-lang#66884 (thanks @9999years) that constifies many of the arithmetic functions on integer primitives from rust-lang#53718 that were blocked on rust-lang#49146. This makes the following things unstably const. - `feature = const_int_unchecked_arith` - `intrinsics::unchecked_add` - `intrinsics::unchecked_sub` - `intrinsics::unchecked_mul` - `intrinsics::unchecked_div` - `intrinsics::unchecked_rem` - `feature = const_checked_int_methods` - `checked_add` - `checked_sub` - `checked_mul` - `checked_div` (Uses `intrinsics::unchecked_div` internally) - `checked_rem` (Uses `intrinsics::unchecked_rem` internally) - `checked_neg` - `checked_shl` - `checked_shr` - `checked_abs` - `feature = const_saturating_int_methods` - `saturating_mul` - `saturating_neg` (Uses `intrinsics::unchecked_sub` internally) - `saturating_abs` (Uses `intrinsics::unchecked_sub` internally) - `feature = const_wrapping_int_methods` - `wrapping_div` - `wrapping_rem` - `feature = const_overflowing_int_methods` - `overflowing_div` - `overflowing_rem` - `feature = const_euclidean_int_methods` - `checked_div_euclid` - `checked_rem_euclid` - `wrapping_div_euclid` - `wrapping_rem_euclid` - `overflowing_div_euclid` - `overflowing_rem_euclid` Exponentiation and operations on the `NonZero` types are left to a later PR. r? @oli-obk cc @rust-lang/wg-const-eval @rust-lang/libs
…olnay Make `num::NonZeroX::new` an unstable `const fn` cc rust-lang#53718 These require `#[feature(const_if_match)]`, meaning they must remain unstable for the time being.
@rustbot release-assignment |
Are these changes alone sufficient to allow to use arithmetic ops like “+” in a constant context or does is require landing of const traits (for e.g. std::ops::Add)? |
…-obk Make integer exponentiation methods unstably const cc rust-lang#53718 This makes the following inherent methods on integer primitives into unstable `const fn`: - `pow` - `checked_pow` - `wrapping_pow` - `overflowing_pow` - `saturating_pow` - `next_power_of_two` - `checked_next_power_of_two` - `wrapping_next_power_of_two` Only two changes were made to the implementation of these methods. First, I had to switch from the `?` operator, which is not yet implemented in a const context, to a `try_opt` macro. Second, `next_power_of_two` was using `ops::Add::add` (see the first commit) to "get overflow checks", so I switched to `#[rustc_inherit_overflow_checks]`. I'm not quite sure why the attribute wasn't used in the first place.
…-obk Make integer exponentiation methods unstably const cc rust-lang#53718 This makes the following inherent methods on integer primitives into unstable `const fn`: - `pow` - `checked_pow` - `wrapping_pow` - `overflowing_pow` - `saturating_pow` - `next_power_of_two` - `checked_next_power_of_two` - `wrapping_next_power_of_two` Only two changes were made to the implementation of these methods. First, I had to switch from the `?` operator, which is not yet implemented in a const context, to a `try_opt` macro. Second, `next_power_of_two` was using `ops::Add::add` (see the first commit) to "get overflow checks", so I switched to `#[rustc_inherit_overflow_checks]`. I'm not quite sure why the attribute wasn't used in the first place.
I think that the |
Would a PR making |
It doesn't give the same results for negative y. But it would overflow for example when |
Can we stabilize the |
@myrrlyn It's already stabilized in beta. |
Hello, as far as I can tell, both [1] and [2] are now stable, right? so there is no extra blocking issue to implement/stabilize those, right? Is there something that can be done to get them stabilized? |
@Razican Most of [1] were marked stable in #73858, so they will be stabilized in 1.47. The PR did not include division and remainder because they can panic, so maybe they need another footnote. To stabilize the rest that can be stabilized (the |
#80962 is up to stabilize all remaining methods mentioned at the top of this issue. The diagnostic on nightly is functionally the same as already-stable equivalents ( |
(none exhaustive) list of
libcore::num
, which should be aconst fn
:feature = const_int_rotate
rotate_left
Add more const int ops #53697 -- stabilizedrotate_right
Add more const int ops #53697 -- stabilizedfeature = const_checked_int_methods
checked_add
[1] Make more arithmetic functions unstably const #68809checked_sub
[1] Make more arithmetic functions unstably const #68809checked_mul
[1] Make more arithmetic functions unstably const #68809checked_div
[1] Make more arithmetic functions unstably const #68809checked_rem
[1] Make more arithmetic functions unstably const #68809checked_neg
[1] Make more arithmetic functions unstably const #68809checked_shl
[1] Make more arithmetic functions unstably const #68809checked_shr
[1] Make more arithmetic functions unstably const #68809checked_abs
[1] Make more arithmetic functions unstably const #68809feature = const_saturating_int_methods
saturating_add
Makesaturating_add
andsaturating_sub
const
functions #58246saturating_sub
Makesaturating_add
andsaturating_sub
const
functions #58246saturating_mul
[1] Make more arithmetic functions unstably const #68809feature = const_int_wrapping
wrapping_add
Add more const int ops #53697 -- stabilizedwrapping_sub
Add more const int ops #53697 -- stabilizedwrapping_mul
Add more const int ops #53697 -- stabilizedwrapping_div
[1] Make more arithmetic functions unstably const #68809wrapping_rem
[1] Make more arithmetic functions unstably const #68809wrapping_neg
Make overflowing and wrapping negation const #58044 -- stabilizedwrapping_shl
Add more const int ops #53697 -- stabilizedwrapping_shr
Add more const int ops #53697 -- stabilizedwrapping_abs
Makeabs
,wrapping_abs
,overflowing_abs
const functions #63786 -- stabilized in 1.39feature = const_int_overflowing
overflowing_add
Add more const int ops #53697overflowing_sub
Add more const int ops #53697overflowing_mul
Add more const int ops #53697overflowing_div
[1] Make more arithmetic functions unstably const #68809overflowing_rem
[1] Make more arithmetic functions unstably const #68809overflowing_neg
Make overflowing and wrapping negation const #58044 -- stabilizedoverflowing_shl
Add more const int ops #53697overflowing_shr
Add more const int ops #53697overflowing_abs
Makeabs
,wrapping_abs
,overflowing_abs
const functions #63786 -- stabilized in 1.39feature = const_euclidean_int_methods
checked_div_euc
[1] Make more arithmetic functions unstably const #68809checked_mod_euc
[1] Make more arithmetic functions unstably const #68809wrapping_div_euc
[1] Make more arithmetic functions unstably const #68809wrapping_mod_euc
[1] Make more arithmetic functions unstably const #68809overflowing_div_euc
[1] Make more arithmetic functions unstably const #68809overflowing_mod_euc
[1] Make more arithmetic functions unstably const #68809feature = const_int_pow
pow
[1, 2] Make integer exponentiation methods unstably const #68978is_power_of_two
make is_power_of_two a const function #65092next_power_of_two
[1] Make integer exponentiation methods unstably const #68978checked_pow
[1] Make integer exponentiation methods unstably const #68978checked_next_power_of_two
[1] Make integer exponentiation methods unstably const #68978saturating_pow
[1, 2] Make integer exponentiation methods unstably const #68978wrapping_pow
[1, 2] Make integer exponentiation methods unstably const #68978wrapping_next_power_of_two
[1] Make integer exponentiation methods unstably const #68978overflowing_pow
[1, 2] Make integer exponentiation methods unstably const #68978feature = const_int_sign
abs
Makeabs
,wrapping_abs
,overflowing_abs
const functions #63786 -- stabilized in 1.39signum
Makei*::signum
aconst fn
. #61635is_positive
Add more const int ops #53697 -- stabilizedis_negative
Add more const int ops #53697 -- stabilizedfeature = const_int_conversion
to_be_bytes
Add more const int ops #53697to_le_bytes
Add more const int ops #53697to_ne_bytes
Add more const int ops #53697from_be_bytes
Add more const int ops #53697from_le_bytes
Add more const int ops #53697from_ne_bytes
Add more const int ops #53697reverse_bits
Add more const int ops #53697feature = const_nonzero_int_methods
NonZero*::new
[1] Makenum::NonZeroX::new
an unstableconst fn
#68976if
andmatch
in constants" #49146 (implemented on nightly)loop
in constant evaluation" #52000 (implemented on nightly)This issue has been assigned to @9999years via this comment.
The text was updated successfully, but these errors were encountered: