Globals | The AssemblyScript Book

The following global constants and functions are present alongside the standard library's classes.

# Constants

  • Not a number as a 32-bit or 64-bit float depending on context. Compiles to a constant.

  • Positive infinity as a 32-bit or 64-bit float depending on context. Compiles to a constant.

# Functions

  • Tests if a 32-bit or 64-bit float is NaN.

  • Tests if a 32-bit or 64-bit float is finite, that is not NaN or +/-Infinity.

  • Parses a string representing an integer to an f64 number Returns NaN on invalid inputs.

    Type-specific variants of parseInt are available separately:

    • F32.parseInt to parse to a 32-bit float.
    • I8.parseInt to parse to a signed 8-bit integer, respectively U8.parseInt if unsigned.
    • I16.parseInt to parse to a signed 16-bit integer, respectively U16.parseInt if unsigned.
    • I32.parseInt to parse to a signed 32-bit integer, respectively U32.parseInt if unsigned.
    • I64.parseInt to parse to a signed 64-bit integer, respectively U64.parseInt if unsigned.
  • Parses a string to a 64-bit float. Returns NaN on invalid inputs.

# Builtins

The following builtins provide direct access to WebAssembly and compiler features. They form the low-level foundation of the standard library, while also being available for everyone to utilize where directly tapping into WebAssembly or the compiler is desired.

# Static type checks

By making use of the following special type checks, especially in generic contexts, untaken branches can be eliminated statically, leading to concrete WebAssembly functions that handle one type specificially.

  • Tests if the specified type or expression is of an integer type and not a reference. Compiles to a constant.

  • Tests if the specified type or expression can represent negative numbers. Compiles to a constant.

  • Tests if the specified type or expression is of a float type. Compiles to a constant.

  • Tests if the specified type or expression is of a SIMD vector type. Compiles to a constant.

  • Tests if the specified type or expression is of a reference type. Compiles to a constant.

  • Tests if the specified type or expression can be used as a string. Compiles to a constant.

  • Tests if the specified type or expression can be used as an array. Compiles to a constant.

  • Tests if the specified type or expression is of a function type. Compiles to a constant.

  • Tests if the specified type or expression is of a nullable reference type. Compiles to a constant.

  • Tests if the specified expression resolves to a defined element. Compiles to a constant.

  • Tests if the specified expression evaluates to a constant value. Compiles to a constant.

  • Tests if the specified type or expression is of a managed type. Compiles to a constant. Usually only relevant when implementing custom collection-like classes.

# Example of static type checking

TIP

If you are not going to use low-level WebAssembly in the foreseeable future, feel free to come back to the following paragraphs at a later time.

# Utilities

  • Reverses the byte order of the specified integer.

  • Determines the byte size of the respective basic type. Means: If T is a class type, the size of usize, the pointer type, is returned. To obtain the size of a class in memory, use offsetof<T>() instead. Compiles to a constant.

  • Determines the offset of the specified field within the given class type. If fieldName is omitted, this returns what could be interpreted as either the size of the class, or the offset where the next field would be located, before alignment. Compiles to a constant. The fieldName argument must be a compile-time constant string because there is no information about field names anymore at runtime. Therefore, the field's name must be known at the time the returned constant is computed.

  • Determines the alignment (log2) of the specified underlying basic type; i.e. If T is a class type, the alignment of usize is returned. Compiles to a constant.

  • Traps if the specified value is not true-ish, otherwise returns the non-nullable value. Like assertions in C, aborting the entire program if the expectation fails. Where desired, the --noAssert compiler option can be used to disable assertions in production.

  • Simple trace function which prints message and 5 optional f64 arguments to a console. n is count of used arguments.

    # Usage examples

  • Instantiates a new instance of T using the specified constructor arguments.

  • Changes the type of a value to another one. Useful for casting class instances to their pointer values and vice-versa.

  • Obtains the computed unique id of a class type. Usually only relevant when allocating objects or dealing with RTTI externally.

  • Returns the name of a given type.

# WebAssembly

# Math

The following generic built-ins compile to WebAssembly instructions directly.

  • Performs the sign-agnostic count leading zero bits operation on a 32-bit or 64-bit integer. All zero bits are considered leading if the value is zero.
    T Instruction
    i8, u8, i16, u16, i32, u32, bool i32.clz
    i64, u64 i64.clz
  • Performs the sign-agnostic count trailing zero bits operation on a 32-bit or 64-bit integer. All zero bits are considered trailing if the value is zero.
    T Instruction
    i8, u8, i16, u16, i32, u32, bool i32.ctz
    i64, u64 i64.ctz
  • Performs the sign-agnostic count number of one bits operation on a 32-bit or 64-bit integer.
    T Instruction
    i8, u8, i16, u16, i32, u32 i32.popcnt
    i64, u64 i64.popcnt
    bool none
  • Performs the sign-agnostic rotate left operation on a 32-bit or 64-bit integer.
    T Instruction
    i32, u32 i32.rotl
    i64, u64 i64.rotl
    i8, u8, i16, u16 emulated
    bool none
  • Performs the sign-agnostic rotate right operation on a 32-bit or 64-bit integer.
    T Instruction
    i32, u32 i32.rotr
    i64, u64 i64.rotr
    i8, u8, i16, u16 emulated
    bool none
  • Computes the absolute value of an integer or float.
    T Instruction
    f32 f32.abs
    f64 f64.abs
    i8, i16, i32, i64 emulated
    u8, u16, u32, u64, bool none
  • Determines the maximum of two integers or floats. If either operand is NaN, returns NaN.
    T Instruction
    f32 f32.max
    f64 f64.max
    i8, u8, i16, u16, i32, u32, i64, u64, bool emulated
  • Determines the minimum of two integers or floats. If either operand is NaN, returns NaN.
    T Instruction
    f32 f32.min
    f64 f64.min
    i8, u8, i16, u16, i32, u32, i64, u64, bool emulated
  • Performs the ceiling operation on a 32-bit or 64-bit float.
    T Instruction
    f32 f32.ceil
    f64 f64.ceil
    i8, u8, i16, u16, i32, u32, i64, u64, bool none
  • Performs the floor operation on a 32-bit or 64-bit float.
    T Instruction
    f32 f32.floor
    f64 f64.floor
    i8, u8, i16, u16, i32, u32, i64, u64, bool none
  • Composes a 32-bit or 64-bit float from the magnitude of x and the sign of y.
    T Instruction
    f32 f32.copysign
    f64 f64.copysign
  • Rounds to the nearest integer half to even of a 32-bit or 64-bit float.
    T Instruction
    f32 f32.nearest
    f64 f64.nearest
    i8, u8, i16, u16, i32, u32, i64, u64, bool none
  • Reinterprets the bits of the specified value as type T.
    TTo Instruction
    i32, u32 i32.reinterpret_f32
    i64, u64 i64.reinterpret_f64
    f32 f32.reinterpret_i32
    f64 f64.reinterpret_i64
  • Calculates the square root of a 32-bit or 64-bit float.
    T Instruction
    f32 f32.sqrt
    f64 f64.sqrt
  • Rounds to the nearest integer towards zero of a 32-bit or 64-bit float.
    T Instruction
    f32 f32.trunc
    f64 f64.trunc
    i8, u8, i16, u16, i32, u32, i64, u64, bool none

# Memory

Similarly, the following built-ins emit WebAssembly instructions accessing or otherwise modifying memory.

NOTE

The immOffset and immAlign arguments, if provided, must be compile time constant values. See more details in rationale (opens new window).

  • Loads a value of the specified type from memory. Equivalent to dereferencing a pointer in other languages.
    T Instruction If contextual type is i64
    i8 i32.load8_s i64.load8_s
    u8 i32.load8_u i64.load8_u
    i16 i32.load16_s i64.load16_s
    u16 i32.load16_u i64.load16_u
    i32 i32.load i64.load32_s
    u32 i32.load i64.load32_u
    i64, u64 i64.load n/a
    f32 f32.load n/a
    f64 f64.load n/a
    <ref> i32/i64.load n/a
  • Stores a value of the specified type to memory. Equivalent to dereferencing a pointer in other languages and assigning a value.
    T Instruction If value is i64
    i8, u8 i32.store8 i64.store8
    i16, u16 i32.store16 i64.store16
    i32, u32 i32.store i64.store32
    i64, u64 i64.store n/a
    f32 f32.store n/a
    f64 f64.store n/a
    <ref> i32/i64.store n/a
  • Returns the current size of the memory in units of pages. One page is 64kb.

  • Grows linear memory by a given unsigned delta of pages. One page is 64kb. Returns the previous size of the memory in units of pages or -1 on failure.

    WARNING

    Calling memory.grow where a memory manager is present might break it.

  • Copies n bytes from src to dst . Regions may overlap. Emits the respective instruction if bulk-memory is enabled, otherwise ships a polyfill.

  • Fills n bytes at dst with the given byte value. Emits the respective instruction if bulk-memory is enabled, otherwise ships a polyfill.

  • Repeats a sequence of bytes given as src with srcLength count times into destination dst.

  • Compares the first n bytes of left and right and returns a value that indicates their relationship:

    • Negative value if the first differing byte in lhs is less than the corresponding byte in rhs.
    • Positive value if the first differing byte in lhs is greater than the corresponding byte in rhs.
    • Zero​ if all n bytes of lhs and rhs are equal.
  • Gets a pointer to a zeroed static chunk of memory of the given size. Alignment defaults to 16. Arguments must be compile-time constants.

  • Gets a pointer to a pre-initialized static chunk of memory. Alignment defaults to the size of T. Arguments must be compile-time constants.

# Control flow

  • Selects one of two pre-evaluated values depending on the condition. Differs from an if/else in that both arms are always executed and the final value is picked based on the condition afterwards. Performs better than an if/else only if the condition is random (means: branch prediction is not going to perform well) and both alternatives are cheap. It is also worth to note that Binaryen will do relevant optimizations like switching to a select automatically, so simply using a ternary ? : may be preferable.

  • Emits an unreachable instruction that results in a runtime error (trap) when executed. Both a statement and an expression of any type. Beware that trapping in managed code will most likely lead to memory leaks or even break the program because it ends execution prematurely.

# Atomics πŸ¦„

The following instructions represent the WebAssembly threads and atomics (opens new window) specification. Must be enabled with --enable threads.

  • Atomically loads an integer value from memory and returns it.

  • Atomically stores an integer value to memory.

  • Atomically adds an integer value in memory.

  • Atomically subtracts an integer value in memory.

  • Atomically performs a bitwise AND operation on an integer value in memory.

  • Atomically performs a bitwise OR operation on an integer value in memory.

  • Atomically performs a bitwise XOR operation on an integer value in memory.

  • Atomically exchanges an integer value in memory.

  • Atomically compares and exchanges an integer value in memory if the condition is met.

  • Performs a wait operation on an address in memory, suspending this agent if the integer condition is met. Return values are

    Value Description
    0 OK - Woken by another agent.
    1 NOT_EQUAL - Loaded value did not match the expected value.
    2 TIMED_OUT - Not woken before the timeout expired.
  • Performs a notify operation on an address in memory waking up suspended agents.

  • Performs a fence operation, preserving synchronization guarantees of higher level languages.

# SIMD πŸ¦„

Likewise, these represent the WebAssembly SIMD (opens new window) specification. Must be enabled with --enable simd.

  • Initializes a 128-bit vector from sixteen 8-bit integer values. Arguments must be compile-time constants.

    See Constructing constant vectors for additional type-specific options.

  • Creates a vector with identical lanes.
    T Instruction
    i8, u8 i8x16.splat
    i16, u16 i16x8.splat
    i32, u32 i32x4.splat
    i64, u64 i64x2.splat
    f32 f32x4.splat
    f64 f64x2.splat
  • Extracts one lane as a scalar.
    T Instruction
    i8 i8x16.extract_lane_s
    u8 i8x16.extract_lane_u
    i16 i16x8.extract_lane_s
    u16 i16x8.extract_lane_u
    i32, u32 i32x4.extract_lane
    i64, u64 i64x2.extract_lane
    f32 f32x4.extract_lane
    f64 f64x2.extract_lane
  • Replaces one lane.
    T Instruction
    i8, u8 i8x16.replace_lane
    i16, u16 i16x8.replace_lane
    i32, u32 i32x4.replace_lane
    i64, u64 i64x2.replace_lane
    f32 f32x4.replace_lane
    f64 f64x2.replace_lane
  • Selects lanes from either vector according to the specified lane indexes.
    T Instruction
    i8, u8 i8x16.shuffle
    i16, u16 i8x16.shuffle emulating i16x8.shuffle
    i32, u32 i8x16.shuffle emulating i32x4.shuffle
    i64, u64 i8x16.shuffle emulating i64x2.shuffle
    f32 i8x16.shuffle emulating f32x4.shuffle
    f64 i8x16.shuffle emulating f64x2.shuffle
  • Selects 8-bit lanes from the first vector according to the indexes [0-15] specified by the 8-bit lanes of the second vector.

  • Loads a vector from memory.

  • Creates a vector by loading the lanes of the specified integer type and extending each to the next larger type.
    TFrom Instruction
    i8 v128.load8x8_s
    u8 v128.load8x8_u
    i16 v128.load16x4_s
    u16 v128.load16x4_u
    i32 v128.load32x2_s
    u32 v128.load32x2_u
  • Creates a vector by loading a value of the specified type into the lowest bits and initializing all other bits of the vector to zero.
    TFrom Instruction
    i32, u32, f32 v128.load32_zero
    i64, u64, f64 v128.load64_zero
  • Loads a single lane from memory into the specified lane of the given vector. Other lanes are bypassed as is.
    T Instruction
    i8, u8 v128.load8_lane
    i16, u16 v128.load16_lane
    i32, u32, f32 v128.load32_lane
    i64, u64, f64 v128.load64_lane
  • Stores the single lane at the specified index of the given vector to memory.
    T Instruction
    i8, u8 v128.store8_lane
    i16, u16 v128.store16_lane
    i32, u32, f32 v128.store32_lane
    i64, u64, f64 v128.store64_lane
  • Creates a vector with identical lanes by loading the splatted value.
    T Instruction
    i8, u8 v128.load8_splat
    i16, u16 v128.load16_splat
    i32, u32, f32 v128.load32_splat
    i64, u64, f64 v128.load64_splat
  • Stores a vector to memory.

  • Adds each lane.
    T Instruction
    i8, u8 i8x16.add
    i16, u16 i16x8.add
    i32, u32 i32x4.add
    i64, u64 i64x2.add
    f32 f32x4.add
    f64 f64x2.add
  • Subtracts each lane.
    T Instruction
    i8, u8 i8x16.sub
    i16, u16 i16x8.sub
    i32, u32 i32x4.sub
    i64, u64 i64x2.sub
    f32 f32x4.sub
    f64 f64x2.sub
  • Multiplies each lane.
    T Instruction
    i16, u16 i16x8.mul
    i32, u32 i32x4.mul
    i64, u64 i64x2.mul
    f32 f32x4.mul
    f64 f64x2.mul
  • Divides each floating point lane.
    T Instruction
    f32 f32x4.div
    f64 f64x2.div
  • Negates each lane.
    T Instruction
    i8, u8 i8x16.neg
    i16, u16 i16x8.neg
    i32, u32 i32x4.neg
    i64, u64 i64x2.neg
    f32 f32x4.neg
    f64 f64x2.neg
  • Adds each signed small integer lane using saturation.
    T Instruction
    i8 i8x16.add_sat_s
    u8 i8x16.add_sat_u
    i16 i16x8.add_sat_s
    u16 i16x8.add_sat_u
  • Subtracts each signed small integer lane using saturation.
    T Instruction
    i8 i8x16.sub_sat_s
    u8 i8x16.sub_sat_u
    i16 i16x8.sub_sat_s
    u16 i16x8.sub_sat_u
  • Performs a bitwise left shift by a scalar on each integer lane.
    T Instruction
    i8, u8 i8x16.shl
    i16, u16 i16x8.shl
    i32, u32 i32x4.shl
    i64, u64 i64x2.shl
  • Performs a bitwise right shift by a scalar on each integer lane.
    T Instruction
    i8 i8x16.shr_s
    u8 i8x16.shr_u
    i16 i16x8.shr_s
    u16 i16x8.shr_u
    i32 i32x4.shr_s
    u32 i32x4.shr_u
    i64 i64x2.shr_s
    u64 i64x2.shr_u
  • Performs the bitwise a & b operation on each lane.

  • Performs the bitwise a | b operation on each lane.

  • Performs the bitwise a ^ b operation on each lane.

  • Performs the bitwise a & !b operation on each lane.

  • Performs the bitwise !a operation on each lane.

  • Selects bits of either vector according to the specified mask. Selects from v1 if the bit in mask is 1, otherwise from v2.

  • Reduces a vector to a scalar indicating whether any lane is considered true.

  • Reduces a vector to a scalar indicating whether all lanes are considered true.
    T Instruction
    i8, u8 i8x16.all_true
    i16, u16 i16x8.all_true
    i32, u32 i32x4.all_true
    i64, u64 i64x2.all_true
  • Extracts the high bit of each integer lane (except 64-bit) and produces a scalar mask with all bits concatenated.
    T Instruction
    i8, u8 i8x16.bitmask
    i16, u16 i16x8.bitmask
    i32, u32 i32x4.bitmask
    i64, u64 i64x2.bitmask
  • Counts the number of bits set to one within each lane.
    T Instruction
    i8, u8 i8x16.popcnt
  • Computes the minimum of each lane.
    T Instruction
    i8 i8x16.min_s
    u8 i8x16.min_u
    i16 i16x8.min_s
    u16 i16x8.min_u
    i32 i32x4.min_s
    u32 i32x4.min_u
    f32 f32x4.min
    f64 f64x2.min
  • Computes the maximum of each lane.
    T Instruction
    i8 i8x16.max_s
    u8 i8x16.max_u
    i16 i16x8.max_s
    u16 i16x8.max_u
    i32 i32x4.max_s
    u32 i32x4.max_u
    f32 f32x4.max
    f64 f64x2.max
  • Computes the psuedo-minimum of each lane.
    T Instruction
    f32 f32x4.pmin
    f64 f64x2.pmin
  • Computes the pseudo-maximum of each lane.
    T Instruction
    f32 f32x4.pmax
    f64 f64x2.pmax
  • Computes the dot product of two 16-bit integer lanes each, yielding lanes one size wider than the input.
    T Instruction
    i16 i32x4.dot_i16x8_s
  • Computes the rounding average of each unsigned small integer lane.
    T Instruction
    u8 i8x16.avgr_u
    u16 i16x8.avgr_u
  • Computes the absolute value of each lane (except 64-bit integers).
    T Instruction
    i8, u8 i8x16.abs
    i16, u16 i16x8.abs
    i32, u32 i32x4.abs
    i64, u64 i64x2.abs
    f32 f32x4.abs
    f64 f64x2.abs
  • Computes the square root of each floating point lane.
    T Instruction
    f32 f32x4.sqrt
    f64 f64x2.sqrt
  • Performs the ceiling operation on each lane.
    T Instruction
    f32 f32x4.ceil
    f64 f64x2.ceil
  • Performs the floor operation on each lane.
    T Instruction
    f32 f32x4.floor
    f64 f64x2.floor
  • Rounds to the nearest integer towards zero of each lane.
    T Instruction
    f32 f32x4.trunc
    f64 f64x2.trunc
  • Rounds to the nearest integer half to even of each lane.
    T Instruction
    f32 f32x4.nearest
    f64 f64x2.nearest
  • Computes which lanes are equal.
    T Instruction
    i8, u8 i8x16.eq
    i16, u16 i16x8.eq
    i32, u32 i32x4.eq
    i64, u64 i64x2.eq
    f32 f32x4.eq
    f64 f64x2.eq
  • Computes which lanes are not equal.
    T Instruction
    i8, u8 i8x16.ne
    i16, u16 i16x8.ne
    i32, u32 i32x4.ne
    i64, u64 i64x2.ne
    f32 f32x4.ne
    f64 f64x2.ne
  • Computes which lanes of the first vector are less than those of the second.
    T Instruction
    i8 i8x16.lt_s
    u8 i8x16.lt_u
    i16 i16x8.lt_s
    u16 i16x8.lt_u
    i32 i32x4.lt_s
    u32 i32x4.lt_u
    i64 i64x2.lt_s
    f32 f32x4.lt
    f64 f64x2.lt
  • Computes which lanes of the first vector are less than or equal those of the second.
    T Instruction
    i8 i8x16.le_s
    u8 i8x16.le_u
    i16 i16x8.le_s
    u16 i16x8.le_u
    i32 i32x4.le_s
    u32 i32x4.le_u
    i64 i64x2.le_s
    f32 f32x4.le
    f64 f64x2.le
  • Computes which lanes of the first vector are greater than those of the second.
    T Instruction
    i8 i8x16.gt_s
    u8 i8x16.gt_u
    i16 i16x8.gt_s
    u16 i16x8.gt_u
    i32 i32x4.gt_s
    u32 i32x4.gt_u
    i64 i64x2.gt_s
    f32 f32x4.gt
    f64 f64x2.gt
  • Computes which lanes of the first vector are greater than or equal those of the second.
    T Instruction
    i8 i8x16.ge_s
    u8 i8x16.ge_u
    i16 i16x8.ge_s
    u16 i16x8.ge_u
    i32 i32x4.ge_s
    u32 i32x4.ge_u
    i64 i64x2.ge_s
    f32 f32x4.ge
    f64 f64x2.ge
  • Converts each lane of a vector from integer to single-precision floating point.
    TFrom Instruction
    i32 f32x4.convert_i32x4_s
    u32 f32x4.convert_i32x4_u
  • Converts the low lanes of a vector from integer to double-precision floating point.
    TFrom Instruction
    i32 f64x2.convert_low_i32x4_s
    u32 f64x2.convert_low_i32x4_u
  • Truncates each lane of a vector from single-precision floating point to integer with saturation. Takes the target type.
    TTo Instruction
    i32 i32x4.trunc_sat_f32x4_s
    u32 i32x4.trunc_sat_f32x4_u
  • Truncates each lane of a vector from double-precision floating point to integer with saturation. Takes the target type.
    TTo Instruction
    i32 i32x4.trunc_sat_f64x2_s_zero
    u32 i32x4.trunc_sat_f64x2_u_zero
  • Narrows wider integer lanes to their respective narrower lanes.
    TFrom Instruction
    i16 i8x16.narrow_i16x8_s
    u16 i8x16.narrow_i16x8_u
    i32 i16x8.narrow_i32x4_s
    u32 i16x8.narrow_i32x4_u
  • Extends the low half of narrower integer lanes to their respective wider lanes.
    TFrom Instruction
    i8 i16x8.extend_low_i8x16_s
    u8 i16x8.extend_low_i8x16_u
    i16 i32x4.extend_low_i16x8_s
    u16 i32x4.extend_low_i16x8_u
    i32 i64x2.extend_low_i32x4_s
    u32 i64x2.extend_low_i32x4_u
  • Extends the high half of narrower integer lanes to their respective wider lanes.
    TFrom Instruction
    i8 i16x8.extend_high_i8x16_s
    u8 i16x8.extend_high_i8x16_u
    i16 i32x4.extend_high_i16x8_s
    u16 i32x4.extend_high_i16x8_u
    i32 i64x2.extend_high_i32x4_s
    u32 i64x2.extend_high_i32x4_u
  • Adds lanes pairwise producing twice wider extended results.
    TFrom Instruction
    i16 i16x8.extadd_pairwise_i8x16_s
    u16 i16x8.extadd_pairwise_i8x16_u
    i32 i32x4.extadd_pairwise_i16x8_s
    u32 i32x4.extadd_pairwise_i16x8_u
  • Demotes each float lane to lower precision. The higher lanes of the result are initialized to zero.
    T Instruction
    f64 f32x4.demote_f64x2_zero
  • Promotes the lower float lanes to higher precision.
    T Instruction
    f32 f64x2.promote_low_f32x4
  • Performs the line-wise saturating rounding multiplication in Q15 format ((a[i] * b[i] + (1 << (Q - 1))) >> Q where Q=15).
    T Instruction
    i16 i16x8.q15mulr_sat_s
  • Performs the lane-wise integer extended multiplication of the lower lanes producing a twice wider result than the inputs.
    T Instruction
    i8 i16x8.extmul_low_i8x16_s
    u8 i16x8.extmul_low_i8x16_u
    i16 i32x4.extmul_low_i16x8_s
    u16 i32x4.extmul_low_i16x8_u
    i32 i64x2.extmul_low_i32x4_s
    u32 i64x2.extmul_low_i32x4_u
  • Performs the lane-wise integer extended multiplication of the higher lanes producing a twice wider result than the inputs.
    T Instruction
    i8 i16x8.extmul_high_i8x16_s
    u8 i16x8.extmul_high_i8x16_u
    i16 i32x4.extmul_high_i16x8_s
    u16 i32x4.extmul_high_i16x8_u
    i32 i64x2.extmul_high_i32x4_s
    u32 i64x2.extmul_high_i32x4_u

# Constructing constant vectors

  • Initializes a 128-bit vector from sixteen 8-bit integer values.

  • Initializes a 128-bit vector from eight 16-bit integer values.

  • Initializes a 128-bit vector from four 32-bit integer values.

  • Initializes a 128-bit vector from two 64-bit integer values.

  • Initializes a 128-bit vector from four 32-bit float values.

  • Initializes a 128-bit vector from two 64-bit float values.

# Relaxed SIMD πŸ¦„

The following instructions represent the WebAssembly Relaxed SIMD (opens new window) specification. Must be enabled with --enable relaxed-simd.

  • Selects 8-bit lanes from a using indices in s. Indices in the range [0-15] select the i-th element of a.

    Unlike v128.swizzle, the result of an out of bounds index is implementation-defined, depending on hardware capabilities: Either 0 or a[s[i]%16].

  • Truncates each lane of a vector from 32-bit floating point to a 32-bit signed or unsigned integer as indicated by T.
    T Instruction
    i32 i32x4.relaxed_trunc_f32x4_s
    u32 i32x4.relaxed_trunc_f32x4_u

    Unlike v128.trunc_sat, the result of lanes out of bounds of the target type is implementation defined, depending on hardware capabilities:

    • If the input lane contains NaN, the result is either 0 or the respective maximum integer value.
    • If the input lane contains a value otherwise out of bounds of the target type, the result is either the saturatated result or maximum integer value.
  • Truncates each lane of a vector from 64-bit floating point to a 32-bit signed or unsigned integer as indicated by T. Unused higher integer lanes of the result are initialized to zero.
    T Instruction
    i32 i32x4.relaxed_trunc_f64x2_s_zero
    u32 i32x4.relaxed_trunc_f64x2_u_zero

    Unlike v128.trunc_sat_zero, the result of lanes out of bounds of the target type is implementation defined, depending on hardware capabilities:

    • If the input lane contains NaN, the result is either 0 or the respective maximum integer value.
    • If the input lane contains a value otherwise out of bounds of the target type, the result is either the saturatated result or maximum integer value.
  • Performs the fused multiply-add operation (a * b + c) on 32- or 64-bit floating point lanes as indicated by T.
    T Instruction
    f32 f32x4.relaxed_madd
    f64 f64x2.relaxed_madd

    The result is implementation defined, depending on hardware capabilities:

    • Either a * b is rounded once and the final result rounded again, or
    • The expression is evaluated with higher precision and only rounded once
  • Performs the fused negative multiply-add operation (-(a * b) + c) on 32- or 64-bit floating point lanes as indicated by T.
    T Instruction
    f32 f32x4.relaxed_nmadd
    f64 f64x2.relaxed_nmadd

    The result is implementation defined, depending on hardware capabilities:

    • Either a * b is rounded once and the final result rounded again, or
    • The expression is evaluated with higher precision and only rounded once
  • Selects 8-, 16-, 32- or 64-bit integer lanes as indicated by T from a or b based on masks in m.
    T Instruction
    i8, u8 i8x16.relaxed_laneselect
    i16, u16 i16x8.relaxed_laneselect
    i32, u32 i32x4.relaxed_laneselect
    i64, u64 i64x2.relaxed_laneselect

    Behaves like v128.bitselect if masks in m do have all bits either set (result is a[i]) or unset (result is b[i]). Otherwise the result is implementation-defined, depending on hardware capabilities: If the most significant bit of m is set, the result is either bitselect(a[i], b[i], mask) or a[i], otherwise the result is b[i].

  • Computes the minimum of each 32- or 64-bit floating point lane as indicated by T.
    T Instruction
    f32 f32x4.relaxed_min
    f64 f64x2.relaxed_min

    Unlike v128.min, the result is implementation-defined if either value is NaN or both are -0.0 and +0.0, depending on hardware capabilities: Either a[i] or b[i].

  • Computes the maximum of each 32- or 64-bit floating point lane as indicated by T.
    T Instruction
    f32 f32x4.relaxed_max
    f64 f64x2.relaxed_max

    Unlike v128.max, the result is implementation-defined if either value is NaN or both are -0.0 and +0.0, depending on hardware capabilities: Either a[i] or b[i].

  • Performs the lane-wise rounding multiplication in Q15 format ((a[i] * b[i] + (1 << (Q - 1))) >> Q where Q=15).
    T Instruction
    i16 i16x8.relaxed_q15mulr_s

    Unlike v128.q15mulr_sat, the result is implementation-defined if both inputs are the minimum signed value: Either the minimum or maximum signed value.

  • Computes the dot product of two 8-bit integer lanes each, yielding lanes one size wider than the input.
    T Instruction
    i16 i16x8.relaxed_dot_i8x16_i7x16_s

    Unlike v128.dot, if the most significant bit of b[i] is set, whether b[i] is interpreted as signed or unsigned by the intermediate multiplication is implementation-defined.

  • Computes the dot product of two 8-bit integer lanes each, yielding lanes two sizes wider than the input with the lanes of c accumulated into the result.
    T Instruction
    i32 i32x4.relaxed_dot_i8x16_i7x16_add_s

    Unlike v128.dot, if the most significant bit of b[i] is set, whether b[i] is interpreted as signed or unsigned by the intermediate multiplication is implementation-defined.

# Inline instructions

In addition to using the generic builtins above, most WebAssembly instructions can be written directly in AssemblyScript code. For example, the following is equivalent: