Skip to content

Add bitwise shifts to Quil#99

Open
antalsz wants to merge 2 commits intoquil-lang:masterfrom
antalsz:bitshifts
Open

Add bitwise shifts to Quil#99
antalsz wants to merge 2 commits intoquil-lang:masterfrom
antalsz:bitshifts

Conversation

@antalsz
Copy link
Contributor

@antalsz antalsz commented Feb 4, 2026

This PR adds bitwise shifts to Quil:

SHIFT-LEFT n b
SHIFT-RIGHT n b
ARITHMETIC-SHIFT-RIGHT n b

These allow expressing the remainder of the bitwise operations besides & (AND), | (IOR), and ^ (XOR) that round out typical bitwise support:

  • SHIFT-LEFT n b shifts the value in n left by b bits, filling in zeros. The equivalent to n <<= b.
  • SHIFT-RIGHT n b shifts the value in n right by b bits, filling in zeros. The equivalent to n >>= b for unsigned n (or Java's >>=).
  • ARITHMETIC-SHIFT-RIGHT n b shifts the value in n right by b bits, preserving the high bit. The equivalent to n >>= b for signed n (or Java's >>>=).

SHIFT-RIGHT a b # a := a >> b; shift the bits of a right by b bits
# filling in 0s
ARITHMETIC-SHIFT-RIGHT a b # a := a >>> b; shift the bits of a right by b bits,
# preserving the high bit

Choose a reason for hiding this comment

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

This threw me off - "shift the bits of a left", took me a second to read it as "shift the bits of a left".

So to check my understanding, is this what the result is?

SHIFT-LEFT              0b10101 2   # 0b10100

SHIFT-RIGHT             0b10101 2   # 0b00101

ARITHMETIC-SHIFT-RIGHT  0b10101 2   # 0b10101

# overshifting

SHIFT-LEFT              0b10101 9   # 0b00000

SHIFT-RIGHT             0b10101 9   # 0b00000

ARITHMETIC-SHIFT-RIGHT  0b10101 9   # 0b10000

Also, why is there no corresponding ARITHMETIC-SHIFT-LEFT? Is there no need to preserve the high bit in that case, or is it achieved through other means (e.g. with an OR)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your SHIFT-LEFT and SHIFT-RIGHT are correct, ARITHMETIC-SHIFT-RIGHT is not. Assuming 6-bit words, you'd get:

ARITHMETIC-SHIFT-RIGHT  0b10101 2   # 0b11101
ARITHMETIC-SHIFT-RIGHT  0b10101 9   # 0b11111

This makes arithmetic right shift signed division by 2 in 2's complement. There's not typically, AFAIK, a need for arithmetic left shift; the low bit doesn't have the same special semantics that the high bit does.

@stylewarning
Copy link
Member

Any opposition to having ASH n k (Arithmetic SHift) with a positive or negative argument (for left and right arithmetic shifting) being defined as something like n * 2^k, alongside the usual interpretation for octets? Given Quil doesn't have a notion of signedness explicitly, I'm not sure arithmetic vs logical is very useful.

@antalsz
Copy link
Contributor Author

antalsz commented Feb 4, 2026

@stylewarning I'm open to redefining ASHR if it's useful, as long as it corresponds naturally to a shift. The logical shifts are more immediately relevant for us, at any rate.

For ARITHMETIC-SHIFT-RIGHT n k, are you considering negative n or negative k? If you mean negative n, yeah, n / 2^k is a good definition; I would still want to include a descriptive statement that this is the same as bit-shifting for 2's complement words so that it's clear why this is called ARITHMETIC-SHIFT-RIGHT. You make a good point that while we have a notion of words we don't commit to their representation, and ARITHMETIC-SHIFT-RIGHT does assume some sort of 1 or 2's complement representation (or something else that uses a sign bit).

@mingyoungjeng
Copy link

x-posting this comment here.

To be consistent with the existing bitwise operations, is there a preferred abbreviated form of the new bitwise shift instructions?
Should they be written as:

  1. SHL, SHR, ASHR
  2. LSH, RSH, ARSH
  3. some other variant?

@antalsz
Copy link
Contributor Author

antalsz commented Feb 5, 2026

@mingyoungjeng Good call, thank you! Since @stylewarning wrote ASH and I wrote ASHR without thinking about it, it seems like SHL, SHR, and ASHR are the way to go, and a Google indicates that they're common. I also updated the wording around ASHR to include the arithmetic definition that @stylewarning requested.

@antalsz
Copy link
Contributor Author

antalsz commented Feb 11, 2026

@stylewarning Does this look good to you? If you have no serious objections, maybe we try to get this in by the end of the week

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants