u32 operations

Miden assembly provides a set of instructions which can perform operations on regular two-complement 32-bit integers. These instructions are described in the tables below.

Most instructions have checked variants. These variants ensure that input values are 32-bit integers, and fail if that's not the case. All other variants do not perform these checks, and thus, should be used only if the inputs are known to be 32-bit integers. Supplying inputs which are greater than or equal to to unchecked operations results in undefined behavior.

The primary benefit of using unchecked operations is performance: they can frequently be executed or times faster than their checked counterparts. In general, vast majority of the unchecked operations listed below can be executed in a single VM cycle.

For instructions where one or more operands can be provided as immediate parameters (e.g., u32checked_add and u32checked_add.b), we provide stack transition diagrams only for the non-immediate version. For the immediate version, it can be assumed that the operand with the specified name is not present on the stack.

In all the table below, the number of cycles it takes for the VM to execute each instruction is listed beneath the instruction.

Conversions and tests

InstructionStack inputStack outputNotes
u32test
- (5 cycles)
[a, ...][b, a, ...]
u32testw
- (23 cycles)
[A, ...][b, A, ...]
u32assert
u32assert.1
- (3 cycles)
[a, ...][a, ...]Fails if
u32assert.2
- (1 cycle)
[b, a,...][b, a,...]Fails if or
u32assertw
- (6 cycles)
[A, ...][A, ...]Fails if
u32cast
- (2 cycles)
[a, ...][b, ...]
u32split
- (1 cycle)
[a, ...][c, b, ...],

Arithmetic operations

InstructionStack inputStack outputNotes
u32checked_add
- (4 cycles)
u32checked_add.b
- (5-6 cycles)
[b, a, ...][c, ...]
Fails if
u32overflowing_add
- (1 cycle)
u32overflowing_add.b
- (2-3 cycles)
[b, a, ...][d, c, ...]

Undefined if
u32wrapping_add
- (2 cycles)
u32wrapping_add.b
- (3-4 cycles)
[b, a, ...][c, ...]
Undefined if
u32overflowing_add3
- (1 cycle)
[c, b, a, ...][e, d, ...],

Undefined if
u32wrapping_add3
- (2 cycles)
[c, b, a, ...][d, ...],
Undefined if
u32checked_sub
- (4 cycles)
u32checked_sub.b
- (5-6 cycles)
[b, a, ...][c, ...]
Fails if or
u32overflowing_sub
- (1 cycle)
u32overflowing_sub.b
- (2-3 cycles)
[b, a, ...][d, c, ...]

Undefined if
u32wrapping_sub
- (2 cycles)
u32wrapping_sub.b
- (3-4 cycles)
[b, a, ...][c, ...]
Undefined if
u32checked_mul
- (4 cycles)
u32checked_mul.b
- (5-6 cycles)
[b, a, ...][c, ...]
Fails if
u32overflowing_mul
- (1 cycle)
u32overflowing_mul.b
- (2-3 cycles)
[b, a, ...][d, c, ...]

Undefined if
u32wrapping_mul
- (2 cycles)
u32wrapping_mul.b
- (3-4 cycles)
[b, a, ...][c, ...]
Undefined if
u32overflowing_madd
- (1 cycle)
[b, a, c, ...][e, d, ...]

Undefined if
u32wrapping_madd
- (2 cycles)
[b, a, c, ...][d, ...]
Undefined if
u32checked_div
- (3 cycles)
u32checked_div.b
- (4-5 cycles)
[b, a, ...][c, ...]
Fails if or
u32unchecked_div
- (2 cycles)
u32unchecked_div.b
- (3-4 cycles)
[b, a, ...][c, ...]
Fails if
Undefined if
u32checked_mod
- (4 cycles)
u32checked_mod.b
- (5-6 cycles)
[b, a, ...][c, ...]
Fails if or
u32unchecked_mod
- (3 cycles)
u32unchecked_mod.b
- (4-5 cycles)
[b, a, ...][c, ...]
Fails if
Undefined if
u32checked_divmod
- (2 cycles)
u32checked_divmod.b
- (3-4 cycles)
[b, a, ...][d, c, ...]

Fails if or
u32unchecked_divmod
- (1 cycle)
u32unchecked_divmod.b
- (2-3 cycles)
[b, a, ...][d, c, ...]

Fails if
Undefined if

Bitwise operations

InstructionStack inputStack outputNotes
u32checked_and
- (1 cycle)
[b, a, ...][c, ...]Computes as a bitwise AND of binary representations of and .
Fails if
u32checked_or
- (6 cycle)s
[b, a, ...][c, ...]Computes as a bitwise OR of binary representations of and .
Fails if
u32checked_xor
- (1 cycle)
[b, a, ...][c, ...]Computes as a bitwise XOR of binary representations of and .
Fails if
u32checked_not
- (5 cycles)
[a, ...][b, ...]Computes as a bitwise NOT of binary representation of .
Fails if
u32checked_shl
- (47 cycles)
u32checked_shl.b
- (4 cycles)
[b, a, ...][c, ...]
Fails if or
u32unchecked_shl
- (40 cycles)
u32unchecked_shl.b
- (3 cycles)
[b, a, ...][c, ...]
Undefined if or
u32checked_shr
- (47 cycles)
u32checked_shr.b
- (4 cycles)
[b, a, ...][c, ...]
Fails if or
u32unchecked_shr
- (40 cycles)
u32unchecked_shr.b
- (3 cycles)
[b, a, ...][c, ...]
Undefined if or
u32checked_rotl
- (47 cycles)
u32checked_rotl.b
- (4 cycles)
[b, a, ...][c, ...]Computes by rotating a 32-bit representation of to the left by bits.
Fails if or
u32unchecked_rotl
- (40 cycles)
u32unchecked_rotl.b
- (3 cycles)
[b, a, ...][c, ...]Computes by rotating a 32-bit representation of to the left by bits.
Undefined if or
u32checked_rotr
- (59 cycles)
u32checked_rotr.b
- (6 cycles)
[b, a, ...][c, ...]Computes by rotating a 32-bit representation of to the right by bits.
Fails if or
u32unchecked_rotr
- (44 cycles)
u32unchecked_rotr.b
- (3 cycles)
[b, a, ...][c, ...]Computes by rotating a 32-bit representation of to the right by bits.
Undefined if or
u32checked_popcnt
- (36 cycles)
[a, ...][b, ...]Computes by counting the number of set bits in (hamming weight of ).
Fails if
u32unchecked_popcnt
- (33 cycles)
[a, ...][b, ...]Computes by counting the number of set bits in (hamming weight of ).
Undefined if

Comparison operations

InstructionStack inputStack outputNotes
u32checked_eq
- (2 cycles)
u32checked_eq.b
- (3-4 cycles)
[b, a, ...][c, ...]
Fails if
Note: unchecked version is not provided because it is equivalent to simple eq.
u32checked_neq
- (3 cycles)
u32checked_neq.b
- (4-5 cycles)
[b, a, ...][c, ...]
Fails if
Note: unchecked version is not provided because it is equivalent to simple neq.
u32checked_lt
- (6 cycles)
[b, a, ...][c, ...]
Fails if
u32unchecked_lt
- (5 cycles)
[b, a, ...][c, ...]
Undefined if
u32checked_lte
- (8 cycles)
[b, a, ...][c, ...]
Fails if
u32unchecked_lte
- (7 cycles)
[b, a, ...][c, ...]
Undefined if
u32checked_gt
- (7 cycles)
[b, a, ...][c, ...]
Fails if
u32unchecked_gt
- (6 cycles)
[b, a, ...][c, ...]
Undefined if
u32checked_gte
- (7 cycles)
[b, a, ...][c, ...]
Fails if
u32unchecked_gte
- (6 cycles)
[b, a, ...][c, ...]
Undefined if
u32checked_min
- (9 cycles)
[b, a, ...][c, ...]
Fails if
u32unchecked_min
- (8 cycles)
[b, a, ...][c, ...]
Undefined if
u32checked_max
- (10 cycles)
[b, a, ...][c, ...]
Fails if
u32unchecked_max
- (9 cycles)
[b, a, ...][c, ...]
Undefined if