ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.34k stars 2.51k forks source link

Proposal: Circular shift operator #7183

Open mrakh opened 3 years ago

mrakh commented 3 years ago

Circular shift (or bit rotation) is a common operation to perform and most every architecture out there has hardware support for it. Since we already use the % token to qualify existing operators with wraparound behavior, I propose we use >>%/>>%=/<<%/<<%= to denote circular shift operations.

SpexGuy commented 3 years ago

I think the standard meaning of % elsewhere in the language (discard overflow) is kind of the opposite of this use. Other than that though I think this is worth doing. Maybe as a builtin instead?

mrakh commented 3 years ago

I think the standard meaning of % elsewhere in the language (discard overflow) is kind of the opposite of this use. Other than that though I think this is worth doing. Maybe as a builtin instead?

For addition, subtraction and multiplication with %, there is no distinction between wraparound vs. overflow/underflow, since the operations are performed modulo a power of two. The % token is described as having wrapping behavior in the docs, so that's what I went with. But if the operator is too obscure, I suppose a built-in would serve just as well.

LemonBoy commented 3 years ago

math.rotl is only one import away, theres no need to have a thousand builtin functions for every operation.

Korporal commented 1 year ago

@mrakh I'm wondering about this myself, for a different language though.

These have come up as options:

rotate right: @> rotate left: <@

One can devise variants of these too.

rotate right: >@> rorate left: <@<

Not sure about Zig, in my case @ is able to be used in a token like this.

lunacookies commented 1 year ago

An option I like is <<< and >>> from Jai. I see the point about just using a library function for this, though.

rohlem commented 1 year ago

No opinion about this, just wanted to note that in Java >> sign-extends while >>> shifts in 0s. I generally agree we don't need operators (nor builtins) for everything, though I don't know how to justify what to include and what not to.

farteryhr commented 1 year ago

i think we're safe to have >>>> <<<< for being infrequently used, cheap, and cool. also see #5220 like proposals.

Validark commented 10 months ago

No opinion about this, just wanted to note that in Java >> sign-extends while >>> shifts in 0s. I generally agree we don't need operators (nor builtins) for everything, though I don't know how to justify what to include and what not to.

In my opinion, what we include as fundamental operators affects how people think about and approach problems. If not given an operator, it has more friction to use it, and fewer people will know about it, even though it's a 1 cycle operation on pretty much all hardware, a fact that I think most programmers just forget about since it is not an operator.

I personally think an operator, not a builtin, is the appropriate level of at-your-fingertips Zig should provide for such a simple, low-level, and widely supported operation.

<<< and >>> are my preferences. Yes, a Java or JavaScript person might need to learn that these are rotate operators, but that's fine, they can learn that.

Khitiara commented 4 months ago

Im more in favor of the >>%, <<% version personally, given the wrapping idea common to %-based operators for arithmetic