Skip to content

Latest commit

 

History

History
67 lines (40 loc) · 1.72 KB

select.md

File metadata and controls

67 lines (40 loc) · 1.72 KB

select

Selects one of the first two operands based on the value of the third operand 1 2.

The valtype immediate is optional, so long as the result type (and type of the first two operands) is a numeric type 1.

Otherwise, the valtype immediate declares the result type 1.

$$ \mathsf{select} \enspace \mathsf{valtype}(T)^? \enspace (a: T, b: T, c: \mathsf{i32}) \to \begin{cases} a &\text{if } c \not = 0 \\ b &\text{if } c = 0 \end{cases} $$

Instructions

Opcode Instruction Immediates Stack Arity
0x1B select none $[ \mathsf{numtype}_1, \mathsf{numtype}_1, \mathsf{i32} ] \to [ \mathsf{numtype}_1 ]$
0x1C select $\mathsf{valtype}(T)$ $[ T, T, \mathsf{i32} ] \to [ T ]$

WAT Examples

Without a valtype immediate

;; The first operand will be the result if the condition is not 0
i32.const 10

;; The second operand will be the result if the condition is 0
i32.const 20

;; The third operand is the condition
i32.const 1
select

With a valtype immediate

;; The first operand will be the result if the condition is not 0
ref.func $func1

;; The second operand will be the result if the condition is 0
ref.func $func2

;; The third operand is the condition
i32.const 1
select funcref

References

WebAssembly Core Specification

Footnotes

  1. Structure, Parametric Instructions - https://www.w3.org/TR/wasm-core-2/syntax/instructions.html#parametric-instructions 2 3

  2. Execution, Parametric Instructions, select(t*) - https://www.w3.org/TR/wasm-core-2/exec/instructions.html#exec-select