Skip to content
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

Implement feature isolate_most_least_significant_one for integer types #136910

Merged
merged 1 commit into from
Feb 22, 2025

Conversation

okaneco
Copy link
Contributor

@okaneco okaneco commented Feb 12, 2025

Accepted ACP - rust-lang/libs-team#467
Tracking issue - #136909

Implement ACP for functions that isolate the most significant set bit and least significant set bit on unsigned, signed, and NonZero integers.

Add function isolate_most_significant_one
Add function isolate_least_significant_one


This PR adds the following impls

impl {u8, u16, u32, u64, u128, usize} {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
impl {i8, i16, i32, i64, i128, isize} {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
impl NonZeroT {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}

Example behavior

assert_eq!(u8::isolate_most_significant_one(0b01100100), 0b01000000);
assert_eq!(u8::isolate_least_significant_one(0b01100100), 0b00000100);

@rustbot
Copy link
Collaborator

rustbot commented Feb 12, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 12, 2025
Copy link
Member

@thomcc thomcc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind implementing some tests?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 19, 2025
Implement accepted ACP for functions that isolate the most significant
set bit and least significant set bit on unsigned, signed, and NonZero
integers.

Add function `isolate_most_significant_one`
Add function `isolate_least_significant_one`
Add tests
@okaneco
Copy link
Contributor Author

okaneco commented Feb 20, 2025

I wasn't sure of what kinds of tests to add.

Added tests for ints, uints, and NonZero which start with -1 or u*::MAX, then left/right shift appropriately to test each position for the most significant or least significant one.

// Right shift each loop
Bits         : 0b1111 | 0b0111 | 0b0011 | 0b0001
Most Sig One : 0b1000 | 0b0100 | 0b0010 | 0b0001
// Left shift each loop
Bits         : 0b1111 | 0b1110 | 0b1100 | 0b1000
Least Sig One: 0b0001 | 0b0010 | 0b0100 | 0b1000

Diff
https://github.com/rust-lang/rust/compare/03d89a1198e03333f7057edbd973eac89e167af2..97bc99a18f8175bf251a902fe5956e4d42fecebb

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 20, 2025
@thomcc
Copy link
Member

thomcc commented Feb 21, 2025

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Feb 21, 2025

📌 Commit 97bc99a has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 21, 2025
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 22, 2025
Implement feature `isolate_most_least_significant_one` for integer types

Accepted ACP - rust-lang/libs-team#467
Tracking issue - rust-lang#136909

Implement ACP for functions that isolate the most significant set bit and least significant set bit on unsigned, signed, and `NonZero` integers.

Add function `isolate_most_significant_one`
Add function `isolate_least_significant_one`

---

This PR adds the following impls
```rust
impl {u8, u16, u32, u64, u128, usize} {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
impl {i8, i16, i32, i64, i128, isize} {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
impl NonZeroT {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
```
Example behavior
```rust
assert_eq!(u8::isolate_most_significant_one(0b01100100), 0b01000000);
assert_eq!(u8::isolate_least_significant_one(0b01100100), 0b00000100);
```
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2025
…mpiler-errors

Rollup of 10 pull requests

Successful merges:

 - rust-lang#136642 (Put the alloc unit tests in a separate alloctests package)
 - rust-lang#136910 (Implement feature `isolate_most_least_significant_one` for integer types)
 - rust-lang#137183 (Prune dead regionck code)
 - rust-lang#137333 (Use `edition = "2024"` in the compiler (redux))
 - rust-lang#137356 (Ferris 🦀 Identifier naming conventions)
 - rust-lang#137362 (Add build step log for `run-make-support`)
 - rust-lang#137377 (Always allow reusing cratenum in CrateLoader::load)
 - rust-lang#137388 (Fix(lib/fs/tests): Disable rename POSIX semantics FS tests under Windows 7)
 - rust-lang#137410 (Use StableHasher + Hash64 for dep_tracking_hash)
 - rust-lang#137413 (jubilee cleared out the review queue)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2025
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#136910 (Implement feature `isolate_most_least_significant_one` for integer types)
 - rust-lang#137183 (Prune dead regionck code)
 - rust-lang#137333 (Use `edition = "2024"` in the compiler (redux))
 - rust-lang#137356 (Ferris 🦀 Identifier naming conventions)
 - rust-lang#137362 (Add build step log for `run-make-support`)
 - rust-lang#137377 (Always allow reusing cratenum in CrateLoader::load)
 - rust-lang#137388 (Fix(lib/fs/tests): Disable rename POSIX semantics FS tests under Windows 7)
 - rust-lang#137410 (Use StableHasher + Hash64 for dep_tracking_hash)
 - rust-lang#137413 (jubilee cleared out the review queue)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1df3a35 into rust-lang:master Feb 22, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Feb 22, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2025
Rollup merge of rust-lang#136910 - okaneco:sig_ones, r=thomcc

Implement feature `isolate_most_least_significant_one` for integer types

Accepted ACP - rust-lang/libs-team#467
Tracking issue - rust-lang#136909

Implement ACP for functions that isolate the most significant set bit and least significant set bit on unsigned, signed, and `NonZero` integers.

Add function `isolate_most_significant_one`
Add function `isolate_least_significant_one`

---

This PR adds the following impls
```rust
impl {u8, u16, u32, u64, u128, usize} {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
impl {i8, i16, i32, i64, i128, isize} {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
impl NonZeroT {
    const fn isolate_most_significant_one(self) -> Self;
    const fn isolate_least_significant_one(self) -> Self;
}
```
Example behavior
```rust
assert_eq!(u8::isolate_most_significant_one(0b01100100), 0b01000000);
assert_eq!(u8::isolate_least_significant_one(0b01100100), 0b00000100);
```
@okaneco okaneco deleted the sig_ones branch February 22, 2025 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants