Swapp operand order for bitwise shifting

This commit is contained in:
Alex Beregszaszi 2017-09-22 14:15:50 +01:00
parent 9464fa7fa5
commit d01c9af47f
1 changed files with 19 additions and 19 deletions

View File

@ -27,51 +27,51 @@ The following instructions are introduced:
### `0x1b`: `SHL` (shift left) ### `0x1b`: `SHL` (shift left)
The `SHL` instruction (shift left) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the first popped value `arg1` shifted to the left by the number of bits in the second popped value `arg2`. The result is equal to The `SHL` instruction (shift left) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the second popped value `arg2` shifted to the left by the number of bits in the first popped value `arg1`. The result is equal to
``` ```
(arg1 * 2^arg2) mod 2^256 (arg2 * 2^arg1) mod 2^256
``` ```
Notes: Notes:
- The value (`arg1`) is interpreted as an unsigned number. - The value (`arg2`) is interpreted as an unsigned number.
- The shift amount (`arg2`) is interpreted as an unsigned number. - The shift amount (`arg1`) is interpreted as an unsigned number.
- If the shift amount (`arg2`) is greater or equal 256 the result is 0. - If the shift amount (`arg1`) is greater or equal 256 the result is 0.
- This is equivalent to `SWAP1 PUSH1 2 EXP MUL`. - This is equivalent to `PUSH1 2 EXP MUL`.
### `0x1c`: `SHR` (logical shift right) ### `0x1c`: `SHR` (logical shift right)
The `SHR` instruction (logical shift right) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the first popped value `arg1` shifted to the right by the number of bits in the second popped value `arg2` with zero fill. The result is equal to The `SHR` instruction (logical shift right) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the second popped value `arg2` shifted to the right by the number of bits in the first popped value `arg1` with zero fill. The result is equal to
``` ```
floor(arg1 / 2^arg2) floor(arg2 / 2^arg1)
``` ```
Notes: Notes:
- The value (`arg1`) is interpreted as an unsigned number. - The value (`arg2`) is interpreted as an unsigned number.
- The shift amount (`arg2`) is interpreted as an unsigned number. - The shift amount (`arg1`) is interpreted as an unsigned number.
- If the shift amount (`arg2`) is greater or equal 256 the result is 0. - If the shift amount (`arg1`) is greater or equal 256 the result is 0.
- This is equivalent to `SWAP1 PUSH1 2 EXP DIV`. - This is equivalent to `PUSH1 2 EXP DIV`.
### `0x1d`: `SAR` (arithmetic shift right) ### `0x1d`: `SAR` (arithmetic shift right)
The `SAR` instruction (arithmetic shift right) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the first popped value `arg1` shifted to the right by the number of bits in the second popped value `arg2` with sign extension. The result is equal to The `SAR` instruction (arithmetic shift right) pops 2 values from the stack, `arg1` and `arg2`, and pushes on the stack the second popped value `arg2` shifted to the right by the number of bits in the first popped value `arg1` with sign extension. The result is equal to
``` ```
floor(arg1 / 2^arg2) floor(arg2 / 2^arg1)
``` ```
Notes: Notes:
- The value (`arg1`) is interpreted as a signed number. - The value (`arg2`) is interpreted as a signed number.
- The shift amount (`arg2`) is interpreted as an unsigned number. - The shift amount (`arg1`) is interpreted as an unsigned number.
- If the shift amount (`arg2`) is greater or equal 256 the result is 0 if `arg1` is non-negative or -1 if `arg1` is negative. - If the shift amount (`arg1`) is greater or equal 256 the result is 0 if `arg2` is non-negative or -1 if `arg2` is negative.
- This is **not** equivalent to `SWAP1 PUSH1 2 EXP SDIV`, since it rounds differently. See `SDIV(-1, 2) == 0`, while `SAR(-1, 1) == -1`. - This is **not** equivalent to `PUSH1 2 EXP SDIV`, since it rounds differently. See `SDIV(-1, 2) == 0`, while `SAR(-1, 1) == -1`.
The cost of the shift instructions is set at `verylow` tier (3 gas). The cost of the shift instructions is set at `verylow` tier (3 gas).
## Rationale ## Rationale
Instruction operands were chosen to match the other logical and arithmetic instructions. Instruction operands were chosen to fit the more natural use case of shifting a value already on the stack. This means the operand order is swapped compared to most arithmetic insturctions.
## Backwards Compatibility ## Backwards Compatibility