Skip to content

Commit

Permalink
Merge pull request #423 from krau5/main
Browse files Browse the repository at this point in the history
Split TVM Instructions page
  • Loading branch information
reveloper committed Nov 17, 2023
2 parents 181f249 + 41d084a commit f7f031f
Show file tree
Hide file tree
Showing 26 changed files with 9,500 additions and 984 deletions.
982 changes: 0 additions & 982 deletions docs/learn/tvm-instructions/instructions.mdx

This file was deleted.

102 changes: 102 additions & 0 deletions docs/learn/tvm-instructions/instructions/app-specific.mdx

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions docs/learn/tvm-instructions/instructions/arithmetic.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { SearchField } from '@site/src/components/SearchField';
import { arithmeticOpcodes as opcodes } from '@site/src/data/opcodes';

# Arithmetic primitives

<SearchField
data={opcodes}
searchBy="doc_opcode"
placeholder="Search for an opcode"
showKeys={[
{ key: 'doc_opcode', name: 'Opcode', isGrouped: true },
{ key: 'doc_fift', name: 'Fift syntax', isGrouped: true },
{ key: 'doc_stack', name: 'Stack', isGrouped: true },
{ key: 'doc_gas', name: 'Gas', isGrouped: true },
{ key: 'doc_description', name: 'Description' },
]}
/>

### Addition, subtraction, multiplication

| xxxxxxx<br/>Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Fift syntax | xxxxxxxxxxxxxxxxx<br/>Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Description | xxxx<br/>Gas |
|:-|:-|:-|:-|:-|
| **`A0`** | `ADD` | _`x y - x+y`_ | | `18` |
| **`A1`** | `SUB` | _`x y - x-y`_ | | `18` |
| **`A2`** | `SUBR` | _`x y - y-x`_ | Equivalent to [`SWAP`](#instr-swap) [`SUB`](#instr-sub). | `18` |
| **`A3`** | `NEGATE` | _`x - -x`_ | Equivalent to [`-1 MULCONST`](#instr-mulconst) or to [`ZERO SUBR`](#instr-subr).<br/>Notice that it triggers an integer overflow exception if `x=-2^256`. | `18` |
| **`A4`** | `INC` | _`x - x+1`_ | Equivalent to [`1 ADDCONST`](#instr-addconst). | `18` |
| **`A5`** | `DEC` | _`x - x-1`_ | Equivalent to [`-1 ADDCONST`](#instr-addconst). | `18` |
| **`A6cc`** | `[cc] ADDCONST`<br/>`[cc] ADDINT`<br/>`[-cc] SUBCONST`<br/>`[-cc] SUBINT` | _`x - x+cc`_ | `-128 <= cc <= 127`. | `26` |
| **`A7cc`** | `[cc] MULCONST`<br/>`[cc] MULINT` | _`x - x*cc`_ | `-128 <= cc <= 127`. | `26` |
| **`A8`** | `MUL` | _`x y - x*y`_ | | `18` |

### Division

| xxxxxxx<br/>Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Fift syntax | xxxxxxxxxxxxxxxxx<br/>Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Description | xxxx<br/>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:<br/>`0 <= m <= 1` - Indicates whether there is pre-multiplication ([`MULDIV`](#instr-muldiv) and its variants), possibly replaced by a left shift.<br/>`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`).<br/>`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`.<br/>`1 <= d <= 3` - Indicates which results of division are required: `1` - only the quotient, `2` - only the remainder, `3` - both.<br/>`0 <= f <= 2` - Rounding mode: `0` - floor, `1` - nearest integer, `2` - ceiling.<br/>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<br/>Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Fift syntax | xxxxxxxxxxxxxxxxx<br/>Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Description | xxxx<br/>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`<br/>Equivalent to [`ONE`](#instr-one) [`SWAP`](#instr-swap) [`LSHIFT`](#instr-lshift-var). | `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`).<br/>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`<br/>`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`](#instr-add) instead of [`QADD`](#instr-qadd)).
Quiet versions of integer comparison primitives are also available ([`QUIET SGN`](#instr-sgn), [`QUIET LESS`](#instr-less) etc).

| xxxxxxx<br/>Opcode | xxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Fift syntax | xxxxxxxxxxxxxxxxx<br/>Stack | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br/>Description | xxxx<br/>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` |
Loading

0 comments on commit f7f031f

Please sign in to comment.