Skip to main content

Arithmetic primitives

TVM Instructions Content List

OpcodeFift syntaxStackDescriptionGas
Please enter a search query
No results found

Arithmetic primitives

Addition, subtraction, multiplication

xxxxxxx
Opcode
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fift syntax
xxxxxxxxxxxxxxxxx
Stack
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Description
xxxx
Gas
A0ADDx y - x+y18
A1SUBx y - x-y18
A2SUBRx y - y-xEquivalent to SWAP SUB.18
A3NEGATEx - -xEquivalent to -1 MULCONST or to ZERO SUBR.
Notice that it triggers an integer overflow exception if x=-2^256.
18
A4INCx - x+1Equivalent to 1 ADDCONST.18
A5DECx - x-1Equivalent 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
A8MULx y - x*y18

Division

xxxxxxx
Opcode
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fift syntax
xxxxxxxxxxxxxxxxx
Stack
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Description
xxxx
Gas
A9mscdfThis 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
A904DIVx y - qq=floor(x/y), r=x-y*q26
A905DIVRx y - q’q’=round(x/y), r’=x-y*q’26
A906DIVCx y - q''q’’=ceil(x/y), r’’=x-y*q’’26
A908MODx y - r26
A90CDIVMODx y - q r26
A90DDIVMODRx y - q' r'26
A90EDIVMODCx y - q'' r''26
A925RSHIFTRx y - round(x/2^y)26
A926RSHIFTCx 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
A98MULDIVx y z - qq=floor(x*y/z)26
A985MULDIVRx y z - q'q'=round(x*y/z)26
A98CMULDIVMODx y z - q rq=floor(x*y/z), r=x*y-z*q26
A9A4MULRSHIFTx y z - floor(x*y/2^z)0 <= z <= 25626
A9A5MULRSHIFTRx y z - round(x*y/2^z)0 <= z <= 25626
A9A6MULRSHIFTCx y z - ceil(x*y/2^z)0 <= z <= 25634
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
A9C4LSHIFTDIVx y z - floor(2^z*x/y)0 <= z <= 25626
A9C5LSHIFTDIVRx y z - round(2^z*x/y)0 <= z <= 25626
A9C6LSHIFTDIVCx y z - ceil(2^z*x/y)0 <= z <= 25634
A9D4tt[tt+1] LSHIFT#DIVx y - floor(2^(tt+1)*x/y)34
A9D5tt[tt+1] LSHIFT#DIVRx y - round(2^(tt+1)*x/y)34
A9D6tt[tt+1] LSHIFT#DIVCx 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 <= 25526
ABcc[cc+1] RSHIFT#x - floor(x/2^(cc+1))0 <= cc <= 25518
ACLSHIFTx y - x*2^y0 <= y <= 102318
ADRSHIFTx y - floor(x/2^y)0 <= y <= 102318
AEPOW2y - 2^y0 <= y <= 1023
Equivalent to ONE SWAP LSHIFT.
18
B0ANDx y - x&yBitwise and of two signed integers x and y, sign-extended to infinity.18
B1ORx y - x\|yBitwise or of two integers.18
B2XORx y - x xor yBitwise xor of two integers.18
B3NOTx - ~xBitwise not of an integer.26
B4cc[cc+1] FITSx - xChecks 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
B400CHKBOOLx - xChecks whether x is a “boolean value'' (i.e., either 0 or -1).26/76
B5cc[cc+1] UFITSx - xChecks whether x is a cc+1-bit unsigned integer for 0 <= cc <= 255 (i.e., whether 0 <= x < 2^(cc+1)).26/76
B500CHKBITx - xChecks whether x is a binary digit (i.e., zero or one).26/76
B600FITSXx c - xChecks whether x is a c-bit signed integer for 0 <= c <= 1023.26/76
B601UFITSXx c - xChecks whether x is a c-bit unsigned integer for 0 <= c <= 1023.26/76
B602BITSIZEx - cComputes smallest c >= 0 such that x fits into a c-bit signed integer (-2^(c-1) <= c < 2^(c-1)).26
B603UBITSIZEx - cComputes smallest c >= 0 such that x fits into a c-bit unsigned integer (0 <= x < 2^c), or throws a range check exception.26
B608MINx y - x or yComputes the minimum of two integers x and y.26
B609MAXx y - x or yComputes the maximum of two integers x and y.26
B60AMINMAX
INTSORT2
x y - x y or y xSorts two integers. Quiet version of this operation returns two NaNs if any of the arguments are NaNs.26
B60BABSx - \|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
B7A0QADDx y - x+y26
B7A1QSUBx y - x-y26
B7A2QSUBRx y - y-x26
B7A3QNEGATEx - -x26
B7A4QINCx - x+126
B7A5QDECx - x-126
B7A8QMULx y - x*y26
B7A904QDIVx y - qDivision returns NaN if y=0.34
B7A905QDIVRx y - q’34
B7A906QDIVCx y - q''34
B7A908QMODx y - r34
B7A90CQDIVMODx y - q r34
B7A90DQDIVMODRx y - q' r'34
B7A90EQDIVMODCx y - q'' r''34
B7A985QMULDIVRx y z - q'34
B7A98CQMULDIVMODx y z - q r34
B7ACQLSHIFTx y - x*2^y26
B7ADQRSHIFTx y - floor(x/2^y)26
B7AEQPOW2y - 2^y26
B7B0QANDx y - x&y26
B7B1QORx y - x\|y26
B7B2QXORx y - x xor y26
B7B3QNOTx - ~x26
B7B4cc[cc+1] QFITSx - xReplaces x with a NaN if x is not a cc+1-bit signed integer, leaves it intact otherwise.34
B7B5cc[cc+1] QUFITSx - xReplaces x with a NaN if x is not a cc+1-bit unsigned integer, leaves it intact otherwise.34
B7B600QFITSXx c - xReplaces x with a NaN if x is not a c-bit signed integer, leaves it intact otherwise.34
B7B601QUFITSXx c - xReplaces x with a NaN if x is not a c-bit unsigned integer, leaves it intact otherwise.34

TVM Instructions Content List