Skip to content

core: Improve the documentation for funnel shifts#154150

Open
tgross35 wants to merge 3 commits intorust-lang:mainfrom
tgross35:funnel-updates
Open

core: Improve the documentation for funnel shifts#154150
tgross35 wants to merge 3 commits intorust-lang:mainfrom
tgross35:funnel-updates

Conversation

@tgross35
Copy link
Contributor

Change the variable name to make things a bit more clear, then rework funnel shift documentation. Examples now cover shifting by zero and emulating rotates.

Tracking issue: #145686

@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2026

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@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 Mar 20, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 20, 2026

r? @joboet

rustbot has assigned @joboet.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet, scottmcm

@tgross35 tgross35 force-pushed the funnel-updates branch 3 times, most recently from 8683922 to 5ae8d71 Compare March 20, 2026 18:13
@tgross35
Copy link
Contributor Author

Cc @clarfonthey since you requested some of this at #145686 (comment)

@tgross35
Copy link
Contributor Author

Output since it's impossible to read the macros:

image

@tgross35
Copy link
Contributor Author

Some ascii art I sketched for this but decided to exclude:

//   high    low   
// |0x1234  0x5678| <- shift left 12 bits
//  \            /
//   \--|       /
//      |0x4567|
//       result
// 
//   high    low   
// |0x1234  0x5678| -> shift right 12 bits
//  \            /
//   \       |--/
//    |0x2345|
//     result

without modifying the original"]
#[inline(always)]
pub const fn funnel_shr(self, rhs: Self, n: u32) -> Self {
pub const fn funnel_shr(self, low: Self, n: u32) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

Personally I think "right" gives a better intuition here than "low". This is about the number as a bitstring after all, not about it's numeric value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Easy enough, renamed

Copy link
Member

Choose a reason for hiding this comment

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

This extends into the text though -- there's still talk about the "upper half" and "lower half" of the result.

Copy link
Contributor Author

@tgross35 tgross35 Mar 22, 2026

Choose a reason for hiding this comment

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

I did consider changing that portion but decided against it. To me at least, "upper half of a u128" is pretty unambiguous and unlikely to confuse anybody, whereas "right half of a u128" sounds more like how I would describe an endian-dependent memory layout.

(The args seem fine because that's cohesive with right/left shift, the ambiguity is only when talking about widening/narrowing as a different size)

Copy link
Member

Choose a reason for hiding this comment

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

Well, but then it is odd to call the 2nd argument right if you're not consistently using that terminology.

I know upper/lower is natural to some people, though I personally have to always pause and translate it into left/right.

Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly would prefer to just keep the naming of LHS and RHS and just clarify what x FSH[LR] y means instead of renaming arguments.

`rhs` is confusing since for the `Shl` and `Shr` traits, that is the
name used for the value to shift by (i.e. `self >> rhs`).
It's a bit easier to understand the results of shifts and otherwise
bitwise operations when leading zeros are present.
Shorten the summary line, cover shifts by zeros, rotates, and panics in
examples, and turn the "Panics" section into a complete sentence.
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

r=me from my side (but happy to see other discussion resolved too)

View changes since this review

#[doc = concat!("assert_eq!(a.funnel_shl(0, ", $rot, "), a << ", $rot, ");")]
///
/// // Shifting by 0 returns `self` unchanged
#[doc = concat!("assert_eq!(a.funnel_shl(0, 0), a);")]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#[doc = concat!("assert_eq!(a.funnel_shl(0, 0), a);")]
#[doc = concat!("assert_eq!(a.funnel_shl(b, 0), a);")]

Or really "nonzero" value perhaps? It feels a little confusing otherwise since technically the shift doesn't actually matter if the first argument is zero already...

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 24, 2026

☔ The latest upstream changes (presumably #154289) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants