You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
tip: 29
title: Bitwise shifting instructions in Tron
author: @ithinker1991
type: Standards Track
discussions to: #29
category: TRC
status: in process
created: 2019-03-07
Simple Summary
To provide native bitwise shifting with cost on par with other arithmetic operations. Just like EIP145 in Ethereum
Abstract
Native bitwise shifting instructions are introduced, which are more efficient processing wise on the host and are cheaper to use by a contract.
Motivation
TVM is lacking bitwise shifting operators, but supports other logical and arithmetic operators. Shift operations can be implemented via arithmetic operators, but that has a higher cost and requires more processing time from the host. Implementing SHL and SHR using arithmetics cost each 35 engine, while the proposed instructions take 3 engine.
Specification
The following instructions are introduced:
0x1b: SHL (shift left)
The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2, and pushes on the stack arg2 shifted to the left by arg1 number of bits. The result is equal to
(arg2 * 2^arg1) mod 2^256
Notes:
The value (arg2) is interpreted as an unsigned number.
The shift amount (arg1) is interpreted as an unsigned number.
If the shift amount (arg1) is greater or equal 256 the result is 0.
This is equivalent to PUSH1 2 EXP MUL.
0x1c: SHR (logical shift right)
The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2, and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill. The result is equal to
floor(arg2 / 2^arg1)
Notes:
The value (arg2) is interpreted as an unsigned number.
The shift amount (arg1) is interpreted as an unsigned number.
If the shift amount (arg1) is greater or equal 256 the result is 0.
This is equivalent to PUSH1 2 EXP DIV.
0x1d: SAR (arithmetic shift right)
The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2, and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension. The result is equal to
floor(arg2 / 2^arg1)
Notes:
The value (arg2) is interpreted as a signed number.
The shift amount (arg1) is interpreted as an unsigned number.
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 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 engine).
Rationale
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
The newly introduced instructions have no effect on bytecode created in the past.
tip: 29
title: Bitwise shifting instructions in Tron
author: @ithinker1991
type: Standards Track
discussions to: #29
category: TRC
status: in process
created: 2019-03-07
Simple Summary
To provide native bitwise shifting with cost on par with other arithmetic operations. Just like EIP145 in Ethereum
Abstract
Native bitwise shifting instructions are introduced, which are more efficient processing wise on the host and are cheaper to use by a contract.
Motivation
TVM is lacking bitwise shifting operators, but supports other logical and arithmetic operators. Shift operations can be implemented via arithmetic operators, but that has a higher cost and requires more processing time from the host. Implementing
SHL
andSHR
using arithmetics cost each 35 engine, while the proposed instructions take 3 engine.Specification
The following instructions are introduced:
0x1b
:SHL
(shift left)The
SHL
instruction (shift left) pops 2 values from the stack, firstarg1
and thenarg2
, and pushes on the stackarg2
shifted to the left byarg1
number of bits. The result is equal toNotes:
arg2
) is interpreted as an unsigned number.arg1
) is interpreted as an unsigned number.arg1
) is greater or equal 256 the result is 0.PUSH1 2 EXP MUL
.0x1c
:SHR
(logical shift right)The
SHR
instruction (logical shift right) pops 2 values from the stack, firstarg1
and thenarg2
, and pushes on the stackarg2
shifted to the right byarg1
number of bits with zero fill. The result is equal toNotes:
arg2
) is interpreted as an unsigned number.arg1
) is interpreted as an unsigned number.arg1
) is greater or equal 256 the result is 0.PUSH1 2 EXP DIV
.0x1d
:SAR
(arithmetic shift right)The
SAR
instruction (arithmetic shift right) pops 2 values from the stack, firstarg1
and thenarg2
, and pushes on the stackarg2
shifted to the right byarg1
number of bits with sign extension. The result is equal toNotes:
arg2
) is interpreted as a signed number.arg1
) is interpreted as an unsigned number.arg1
) is greater or equal 256 the result is 0 ifarg2
is non-negative or -1 ifarg2
is negative.PUSH1 2 EXP SDIV
, since it rounds differently. SeeSDIV(-1, 2) == 0
, whileSAR(-1, 1) == -1
.The cost of the shift instructions is set at
verylow
tier (3 engine).Rationale
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
The newly introduced instructions have no effect on bytecode created in the past.
Test Cases
SHL
(shift left)SHR
(logical shift right)SAR
(arithmetic shift right)The text was updated successfully, but these errors were encountered: