vendredi 7 juin 2019

`select` vs `if then else` in wasm

What is the difference between if and select in wasm and what is the best application of these commands?

Example in wat2wasm:

WAT code:

(module
  (func (export "Select") (param i32) (result i32)
    (select (i32.const 3)
            (i32.const 5)
            (local.get 0) ))

  (func (export "If") (param i32) (result i32)
    (if (result i32) (local.get 0)
        (then (i32.const  7))
        (else (i32.const 11)) )) )

JS code:

const wasmInstance = new WebAssembly.Instance(wasmModule, {});
const { Select, If } = wasmInstance.exports;
console.log(Select(1)); // =>  3
console.log(Select(0)); // =>  5
console.log(If(1));     // =>  7
console.log(If(0));     // => 11

According to the docs:

The select operator selects one of its first two operands based on whether its third operand is zero or not.

The block, loop and if instructions are structured instructions. They bracket nested sequences of instructions, called blocks, terminated with, or separated by, end or else pseudo-instructions. As the grammar prescribes, they must be well-nested. A structured instruction can produce a value as described by the annotated result type.

Is it the difference that the if may contain blocks in the branches, but the select parameter contains only scalar values?

Aucun commentaire:

Enregistrer un commentaire