Arithmetic primitives
TVM Instructions Content List
- Overview
- Stack Manipulation
- Tuple, List and Null
- Constants and Literals
- Arithmetic Operations
- Data Comparison
- Cell Manipulation
- Continuation and Control Flow
- Exception Generation and Handling
- Dictionary Manipulation
- Application-specific Primitives
- Miscellaneous
Opcode | Fift syntax | Stack | Description | Gas |
---|---|---|---|---|
Please enter a search query | ||||
No results found |
Arithmetic primitives
Addition, subtraction, multiplication
xxxxxxx Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx Fift syntax | xxxxxxxxxxxxxxxxx Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Description | xxxx Gas |
---|---|---|---|---|
A0 | ADD | x y - x+y | 18 | |
A1 | SUB | x y - x-y | 18 | |
A2 | SUBR | x y - y-x | Equivalent to SWAP SUB . | 18 |
A3 | NEGATE | x - -x | Equivalent to -1 MULCONST or to ZERO SUBR .Notice that it triggers an integer overflow exception if x=-2^256 . | 18 |
A4 | INC | x - x+1 | Equivalent to 1 ADDCONST . | 18 |
A5 | DEC | x - x-1 | Equivalent to -1 ADDCONST . | 18 |
A6cc | [cc] ADDCONST [cc] ADDINT [-cc] SUBCONST [-cc] SUBINT | x - x+cc | -128 <= cc <= 127 . | 26 |
A7cc | [cc] MULCONST [cc] MULINT | x - x*cc | -128 <= cc <= 127 . | 26 |
A8 | MUL | x y - x*y | 18 |
Division
xxxxxxx Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx Fift syntax | xxxxxxxxxxxxxxxxx Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Description | xxxx Gas |
---|---|---|---|---|
A9mscdf | This is the general encoding of division, with an optional pre-multiplication and an optional replacement of the division or multiplication by a shift. Variable fields are as follows:0 <= m <= 1 - Indicates whether there is pre-multiplication (MULDIV and its variants), possibly replaced by a left shift.0 <= s <= 2 - Indicates whether either the multiplication or the division have been replaced by shifts: s=0 - no replacement, s=1 - division replaced by a right shift, s=2 - multiplication replaced by a left shift (possible only for m=1 ).0 <= c <= 1 - Indicates whether there is a constant one-byte argument tt for the shift operator (if s!=0 ). For s=0 , c=0 . If c=1 , then 0 <= tt <= 255 , and the shift is performed by tt+1 bits. If s!=0 and c=0 , then the shift amount is provided to the instruction as a top-of-stack Integer in range 0...256 .1 <= d <= 3 - Indicates which results of division are required: 1 - only the quotient, 2 - only the remainder, 3 - both.0 <= f <= 2 - Rounding mode: 0 - floor, 1 - nearest integer, 2 - ceiling.All instructions below are variants of this. | 26 | ||
A904 | DIV | x y - q | q=floor(x/y) , r=x-y*q | 26 |
A905 | DIVR | x y - q’ | q’=round(x/y) , r’=x-y*q’ | 26 |
A906 | DIVC | x y - q'' | q’’=ceil(x/y) , r’’=x-y*q’’ | 26 |
A908 | MOD | x y - r | 26 | |
A90C | DIVMOD | x y - q r | 26 | |
A90D | DIVMODR | x y - q' r' | 26 | |
A90E | DIVMODC | x y - q'' r'' | 26 | |
A925 | RSHIFTR | x y - round(x/2^y) | 26 | |
A926 | RSHIFTC | x y - ceil(x/2^y) | 34 | |
A935tt | [tt+1] RSHIFTR# | x y - round(x/2^(tt+1)) | 34 | |
A936tt | [tt+1] RSHIFTC# | x y - ceil(x/2^(tt+1)) | 34 | |
A938tt | [tt+1] MODPOW2# | x - x mod 2^(tt+1) | 34 | |
A98 | MULDIV | x y z - q | q=floor(x*y/z) | 26 |
A985 | MULDIVR | x y z - q' | q'=round(x*y/z) | 26 |
A98C | MULDIVMOD | x y z - q r | q=floor(x*y/z) , r=x*y-z*q | 26 |
A9A4 | MULRSHIFT | x y z - floor(x*y/2^z) | 0 <= z <= 256 | 26 |
A9A5 | MULRSHIFTR | x y z - round(x*y/2^z) | 0 <= z <= 256 | 26 |
A9A6 | MULRSHIFTC | x y z - ceil(x*y/2^z) | 0 <= z <= 256 | 34 |
A9B4tt | [tt+1] MULRSHIFT# | x y - floor(x*y/2^(tt+1)) | 34 | |
A9B5tt | [tt+1] MULRSHIFTR# | x y - round(x*y/2^(tt+1)) | 34 | |
A9B6tt | [tt+1] MULRSHIFTC# | x y - ceil(x*y/2^(tt+1)) | 26 | |
A9C4 | LSHIFTDIV | x y z - floor(2^z*x/y) | 0 <= z <= 256 | 26 |
A9C5 | LSHIFTDIVR | x y z - round(2^z*x/y) | 0 <= z <= 256 | 26 |
A9C6 | LSHIFTDIVC | x y z - ceil(2^z*x/y) | 0 <= z <= 256 | 34 |
A9D4tt | [tt+1] LSHIFT#DIV | x y - floor(2^(tt+1)*x/y) | 34 | |
A9D5tt | [tt+1] LSHIFT#DIVR | x y - round(2^(tt+1)*x/y) | 34 | |
A9D6tt | [tt+1] LSHIFT#DIVC | x y - ceil(2^(tt+1)*x/y) | 26 |
Shifts, logical operations
xxxxxxx Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx Fift syntax | xxxxxxxxxxxxxxxxx Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Description | xxxx Gas |
---|---|---|---|---|
AAcc | [cc+1] LSHIFT# | x - x*2^(cc+1) | 0 <= cc <= 255 | 26 |
ABcc | [cc+1] RSHIFT# | x - floor(x/2^(cc+1)) | 0 <= cc <= 255 | 18 |
AC | LSHIFT | x y - x*2^y | 0 <= y <= 1023 | 18 |
AD | RSHIFT | x y - floor(x/2^y) | 0 <= y <= 1023 | 18 |
AE | POW2 | y - 2^y | 0 <= y <= 1023 Equivalent to ONE SWAP LSHIFT . | 18 |
B0 | AND | x y - x&y | Bitwise and of two signed integers x and y , sign-extended to infinity. | 18 |
B1 | OR | x y - x\|y | Bitwise or of two integers. | 18 |
B2 | XOR | x y - x xor y | Bitwise xor of two integers. | 18 |
B3 | NOT | x - ~x | Bitwise not of an integer. | 26 |
B4cc | [cc+1] FITS | x - x | Checks whether x is a cc+1 -bit signed integer for 0 <= cc <= 255 (i.e., whether -2^cc <= x < 2^cc ).If not, either triggers an integer overflow exception, or replaces x with a NaN (quiet version). | 26/76 |
B400 | CHKBOOL | x - x | Checks whether x is a “boolean value'' (i.e., either 0 or -1). | 26/76 |
B5cc | [cc+1] UFITS | x - x | Checks whether x is a cc+1 -bit unsigned integer for 0 <= cc <= 255 (i.e., whether 0 <= x < 2^(cc+1) ). | 26/76 |
B500 | CHKBIT | x - x | Checks whether x is a binary digit (i.e., zero or one). | 26/76 |
B600 | FITSX | x c - x | Checks whether x is a c -bit signed integer for 0 <= c <= 1023 . | 26/76 |
B601 | UFITSX | x c - x | Checks whether x is a c -bit unsigned integer for 0 <= c <= 1023 . | 26/76 |
B602 | BITSIZE | x - c | Computes smallest c >= 0 such that x fits into a c -bit signed integer (-2^(c-1) <= c < 2^(c-1) ). | 26 |
B603 | UBITSIZE | x - c | Computes smallest c >= 0 such that x fits into a c -bit unsigned integer (0 <= x < 2^c ), or throws a range check exception. | 26 |
B608 | MIN | x y - x or y | Computes the minimum of two integers x and y . | 26 |
B609 | MAX | x y - x or y | Computes the maximum of two integers x and y . | 26 |
B60A | MINMAX INTSORT2 | x y - x y or y x | Sorts two integers. Quiet version of this operation returns two NaN s if any of the arguments are NaN s. | 26 |
B60B | ABS | x - \|x\| | Computes the absolute value of an integer x . | 26 |
Quiet arithmetic primitives
Quiet operations return NaN
instead of throwing exceptions if one of their arguments is a NaN
or in the case of an integer overflow.
Quiet operations have a prefix Q
as shown below. Another way to make an operation quiet is to add QUIET
before it (i.e. one can write QUIET ADD
instead of QADD
).
Quiet versions of integer comparison primitives are also available (QUIET SGN
, QUIET LESS
etc).
xxxxxxx Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx Fift syntax | xxxxxxxxxxxxxxxxx Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Description | xxxx Gas |
---|---|---|---|---|
B7A0 | QADD | x y - x+y | 26 | |
B7A1 | QSUB | x y - x-y | 26 | |
B7A2 | QSUBR | x y - y-x | 26 | |
B7A3 | QNEGATE | x - -x | 26 | |
B7A4 | QINC | x - x+1 | 26 | |
B7A5 | QDEC | x - x-1 | 26 | |
B7A8 | QMUL | x y - x*y | 26 | |
B7A904 | QDIV | x y - q | Division returns NaN if y=0 . | 34 |
B7A905 | QDIVR | x y - q’ | 34 | |
B7A906 | QDIVC | x y - q'' | 34 | |
B7A908 | QMOD | x y - r | 34 | |
B7A90C | QDIVMOD | x y - q r | 34 | |
B7A90D | QDIVMODR | x y - q' r' | 34 | |
B7A90E | QDIVMODC | x y - q'' r'' | 34 | |
B7A985 | QMULDIVR | x y z - q' | 34 | |
B7A98C | QMULDIVMOD | x y z - q r | 34 | |
B7AC | QLSHIFT | x y - x*2^y | 26 | |
B7AD | QRSHIFT | x y - floor(x/2^y) | 26 | |
B7AE | QPOW2 | y - 2^y | 26 | |
B7B0 | QAND | x y - x&y | 26 | |
B7B1 | QOR | x y - x\|y | 26 | |
B7B2 | QXOR | x y - x xor y | 26 | |
B7B3 | QNOT | x - ~x | 26 | |
B7B4cc | [cc+1] QFITS | x - x | Replaces x with a NaN if x is not a cc+1 -bit signed integer, leaves it intact otherwise. | 34 |
B7B5cc | [cc+1] QUFITS | x - x | Replaces x with a NaN if x is not a cc+1 -bit unsigned integer, leaves it intact otherwise. | 34 |
B7B600 | QFITSX | x c - x | Replaces x with a NaN if x is not a c-bit signed integer, leaves it intact otherwise. | 34 |
B7B601 | QUFITSX | x c - x | Replaces x with a NaN if x is not a c-bit unsigned integer, leaves it intact otherwise. | 34 |