I ran into this a while back as well.
There are two useful shifts: masking shifts and unbounded shifts. Masking shifts do what the current badly named "wrapping" shifts do and are generally faster, while unbounded shifts are a little slower, but more mathematical. A masking shift by a negative value should do what it currently does: mask the shift value to the required size, which also forces it to be positive. An unbounded shift by a negative value should shift in the other direction, because x.unbounded_shl(y) = floor(x * 2^y) % 2^N when x is an N-bit integer. Even if 2^y is not an integer because y is negative, the floor(...) function forces the final result back into an integer.
P.S. Something I've seen no one else propose is this:
impl Shl<i16> for f32 {
type Output = f32;
fn shl(self, s: i16) -> f32 {
libm::ldexpf(self, i32::from(s))
}
}
What does ldexp(x, i) do? x * 2^i of course, just like a left-shift, though it also works if i is negative.
Do they?! Iād wager that the majority of C or C++ programmers donāt know either that shifting by k>=N is undefined behavior or that the actual HW behavior on many archs is k&(N-1).
They don't. The linked article doesn't actually say programmers expect it. It just says that there do exist C++ programs that end up relying on this undefined behavior behaving a certain way. But that's always true for every observable behavior no matter how arcane. It doesn't mean programmers wanted such behavior up-front.
Are masking shifts ever actually useful?
I can't think of any such situation. That's different from wrapping arithmetic where at least uN::MAX + 10 - 10 produces the infinite-precision-correct value.
(Maybe they're slightly cheaper that a cmov for the unbounded one, but that's still not useful to me.)
Fair point. I honestly just assumed that it would be useful for maximising performance. Decided to actually check the assembly for all currently available shifts. unchecked and wrapping are identical on x86_64, aarch64, and riscv64, all being a single instruction (plus moves on x86_64 and the return) but could have different effects on surrounding code when optimising. Unbounded shifts cost 3 extra instructions on x86_64, for right shifts on aarch64, and on riscv64 without zicond. Left shift on aarch64 only cost 2 extra instructions, and adding zicond on riscv64 cost 4 extra instructions instead of 3, but saves a branch. Manually adding a mask properly removes the (fairly small) overhead, so the current behavior and performance is recoverable from it. Adding a bidirectional shift cost quite a bit more overhead than a normal unbounded shift, but of course that's only needed when the compiler doesn't know if it's always positive, which with unsigned shift amounts it does.
Here's the code I was testing with: Compiler Explorer
So ya, unbounded shifts, at least when unidirectional, are actually decently cheap, and masking shifts are probably less useful than checked shifts, since unless you actually want unbounded shifts, any shift amount greater than the bit width is most likely a bug, and amount less than that will behave the same for everything other than the exact shifts.
On that note, the overflowing shifts don't seem very useful either since, again, the condition they're catching is almost always a bug, and the name initially makes me think of a widening shift, which would be useful, though pretty easy to implement outside the case where the shift amount equals the number of bits, which seems less pathological than for typical shifts.
They are ādecently cheapā only in a very bash-like way there are 10 āobviousā ways of doing something, but the āproperā way, if you want to get a decent speed, is crazy convoluted and non-obvious. I'm not sure we want to see that in Rust, even if in era of agents one may just add rule about āproperā way to the SKILL.md file.
That's precisely the story: in practice you very rarely want to have shifts that are larger than width of the type but if default in not wrapping shift then it's very easy to to introduce inefficiency that compiler wouldn't be able to eliminate.
The real proper way here would be to have range-limited numbers like Pascal from half-century ago, but Rust still doesn't have them.
But that's something that may be change in an some Edition if anyone would care enough.