Conversation
specgen/spec/sec-mem.s
Outdated
| 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 |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
|
Any opposition to having |
|
@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 |
|
To be consistent with the existing bitwise operations, is there a preferred abbreviated form of the new bitwise shift instructions?
|
|
@mingyoungjeng Good call, thank you! Since @stylewarning wrote |
|
@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 |
This PR adds bitwise shifts to Quil:
These allow expressing the remainder of the bitwise operations besides
&(AND),|(IOR), and^(XOR) that round out typical bitwise support:SHIFT-LEFT n bshifts the value innleft bybbits, filling in zeros. The equivalent ton <<= b.SHIFT-RIGHT n bshifts the value innright bybbits, filling in zeros. The equivalent ton >>= bfor unsignedn(or Java's>>=).ARITHMETIC-SHIFT-RIGHT n bshifts the value innright bybbits, preserving the high bit. The equivalent ton >>= bfor signedn(or Java's>>>=).