# 全局变量
¥Globals
以下全局常量和函数与标准库的类一起存在。
¥The following global constants and functions are present alongside the standard library's classes.
# 常量
¥Constants
const NaN: auto // f32 or f64
不是 32 位或 64 位浮点数的数字,具体取决于上下文。编译为常量。
¥Not a number as a 32-bit or 64-bit float depending on context. Compiles to a constant.
const Infinity: auto // f32 or f64
正无穷大为 32 位或 64 位浮点数,具体取决于上下文。编译为常量。
¥Positive infinity as a 32-bit or 64-bit float depending on context. Compiles to a constant.
# 函数
¥Functions
function isNaN<T>(value: T): bool
测试 32 位或 64 位浮点数是否为
NaN
。¥Tests if a 32-bit or 64-bit float is
NaN
.function isFinite<T>(value: T): bool
测试 32 位或 64 位浮点是否是有限的,即不是
NaN
或 +/-Infinity
。¥Tests if a 32-bit or 64-bit float is finite, that is not
NaN
or +/-Infinity
.function parseInt(str: string, radix?: i32): f64
将表示整数的字符串解析为 f64 数字 如果输入无效,则返回
NaN
。¥Parses a string representing an integer to an f64 number Returns
NaN
on invalid inputs.parseInt
的特定类型变体可单独购买:¥Type-specific variants of
parseInt
are available separately:F32.parseInt
解析为 32 位浮点数。¥
F32.parseInt
to parse to a 32-bit float.I8.parseInt
解析为有符号 8 位整数,如果无符号则解析为U8.parseInt
。¥
I8.parseInt
to parse to a signed 8-bit integer, respectivelyU8.parseInt
if unsigned.I16.parseInt
解析为有符号 16 位整数,如果无符号则解析为U16.parseInt
。¥
I16.parseInt
to parse to a signed 16-bit integer, respectivelyU16.parseInt
if unsigned.I32.parseInt
解析为有符号 32 位整数,如果无符号则解析为U32.parseInt
。¥
I32.parseInt
to parse to a signed 32-bit integer, respectivelyU32.parseInt
if unsigned.I64.parseInt
解析为有符号 64 位整数,如果无符号则解析为U64.parseInt
。¥
I64.parseInt
to parse to a signed 64-bit integer, respectivelyU64.parseInt
if unsigned.
function parseFloat(str: string): f64
将字符串解析为 64 位浮点数。如果输入无效,则返回
NaN
。¥Parses a string to a 64-bit float. Returns
NaN
on invalid inputs.
# 内置函数
¥Builtins
以下内置函数提供对 WebAssembly 和编译器功能的直接访问。它们构成了标准库的底层基础,同时也可供每个人在需要直接利用 WebAssembly 或编译器时使用。
¥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
通过使用以下特殊类型检查,尤其是在通用上下文中,可以静态消除未采用的分支,从而生成专门处理一种类型的具体 WebAssembly 函数。
¥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.
function isInteger<T>(value?: T): bool
测试指定的类型或表达式是否为整数类型而不是引用。编译为常量。
¥Tests if the specified type or expression is of an integer type and not a reference. Compiles to a constant.
function isSigned<T>(value?: T): bool
测试指定的类型或表达式是否可以表示负数。编译为常量。
¥Tests if the specified type or expression can represent negative numbers. Compiles to a constant.
function isFloat<T>(value?: T): bool
测试指定的类型或表达式是否为浮点类型。编译为常量。
¥Tests if the specified type or expression is of a float type. Compiles to a constant.
function isVector<T>(value?: T): bool
测试指定的类型或表达式是否属于 SIMD 向量类型。编译为常量。
¥Tests if the specified type or expression is of a SIMD vector type. Compiles to a constant.
function isReference<T>(value?: T): bool
测试指定的类型或表达式是否属于引用类型。编译为常量。
¥Tests if the specified type or expression is of a reference type. Compiles to a constant.
function isString<T>(value?: T): bool
测试指定的类型或表达式是否可以用作字符串。编译为常量。
¥Tests if the specified type or expression can be used as a string. Compiles to a constant.
function isArray<T>(value?: T): bool
测试指定的类型或表达式是否可以用作数组。编译为常量。
¥Tests if the specified type or expression can be used as an array. Compiles to a constant.
function isFunction<T>(value?: T): bool
测试指定的类型或表达式是否属于函数类型。编译为常量。
¥Tests if the specified type or expression is of a function type. Compiles to a constant.
function isNullable<T>(value?: T): bool
测试指定的类型或表达式是否属于可为空的引用类型。编译为常量。
¥Tests if the specified type or expression is of a nullable reference type. Compiles to a constant.
function isDefined(expression: auto): bool
测试指定的表达式是否解析为定义的元素。编译为常量。
¥Tests if the specified expression resolves to a defined element. Compiles to a constant.
function isConstant(expression: auto): bool
测试指定表达式的计算结果是否为常量值。编译为常量。
¥Tests if the specified expression evaluates to a constant value. Compiles to a constant.
function isManaged<T>(expression: auto): bool
测试指定的类型或表达式是否属于托管类型。编译为常量。通常仅在实现自定义类集合类时相关。
¥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
function add<T>(a: T, b: T): T {
return a + b // addition if numeric, string concatenation if a string
}
function add<T>(a: T, b: T): T {
if (isString<T>()) { // eliminated if T is not a string
return parseInt(a) + parseInt(b)
} else { // eliminated if T is a string
return a + b
}
}
提示
如果你在可预见的将来不打算使用底层 WebAssembly,请稍后再回来查看以下段落。
¥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
function bswap<T>(value: T): T
反转指定整数的字节顺序。
¥Reverses the byte order of the specified integer.
function sizeof<T>(): usize
确定相应基本类型的字节大小。方法:如果
T
是类类型,则返回usize
(指针类型)的大小。要获取内存中类的大小,请改用offsetof<T>()
。编译为常量。¥Determines the byte size of the respective basic type. Means: If
T
is a class type, the size ofusize
, the pointer type, is returned. To obtain the size of a class in memory, useoffsetof<T>()
instead. Compiles to a constant.function offsetof<T>(fieldName?: string): usize
确定给定类类型中指定字段的偏移量。如果省略
fieldName
,则返回可以解释为类的大小或对齐之前下一个字段所在位置的偏移量。编译为常量。fieldName
参数必须是编译时常量string
,因为运行时不再有有关字段名称的信息。因此,在计算返回的常量时必须知道字段的名称。¥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. ThefieldName
argument must be a compile-time constantstring
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.function alignof<T>(): usize
确定指定底层基本类型的对齐方式(log2);即,如果
T
是类类型,则返回usize
的对齐方式。编译为常量。¥Determines the alignment (log2) of the specified underlying basic type; i.e. If
T
is a class type, the alignment ofusize
is returned. Compiles to a constant.function assert<T>(isTrueish: T, message?: string): T
如果指定的值不为 true,则捕获,否则返回不可为 null 的值。就像 C 中的断言一样,如果期望失败则中止整个程序。如果需要,可以使用
--noAssert
编译器选项来禁用生产中的断言。¥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.function trace(message: string, n?: i32, a0?: f64, a1?: f64, a2?: f64, a3?: f64, a4?: f64): void
简单的跟踪函数,将
message
和 5 个可选的f64
参数打印到控制台。n
是已使用参数的计数。¥Simple trace function which prints
message
and 5 optionalf64
arguments to a console.n
is count of used arguments.# 使用示例
¥Usage examples
trace("foo"); trace("one arg:", 1, 5.0); trace("three args:", 3, 1.0, <f64>2, 3);
function instantiate<T>(...args: auto[]): T
使用指定的构造函数参数实例化
T
的新实例。¥Instantiates a new instance of
T
using the specified constructor arguments.function changetype<T>(value: auto): T
将一个值的类型更改为另一种值的类型。对于将类实例转换为其指针值非常有用,反之亦然。
¥Changes the type of a value to another one. Useful for casting class instances to their pointer values and vice-versa.
function idof<T>(): u32
获取类类型的计算的唯一 ID。通常仅在分配对象或外部处理 RTTI 时相关。
¥Obtains the computed unique id of a class type. Usually only relevant when allocating objects or dealing with RTTI externally.
function nameof<T>(value?: T): string
返回给定类型的名称。
¥Returns the name of a given type.
# WebAssembly
# 数学
¥Math
以下通用内置函数直接编译为 WebAssembly 指令。
¥The following generic built-ins compile to WebAssembly instructions directly.
function clz<T>(value: T): T
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 操作说明 i8、u8、i16、u16、i32、u32、布尔 i32.clz i64、u64 i64.clz function ctz<T>(value: T): T
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 操作说明 i8、u8、i16、u16、i32、u32、布尔 i32.ctz i64、u64 i64.ctz function popcnt<T>(value: T): T
Performs the sign-agnostic count number of one bits operation on a 32-bit or 64-bit integer.
T 操作说明 i8、u8、i16、u16、i32、u32 i32.popcnt i64、u64 i64.popcnt 布尔值 没有任何 function rotl<T>(value: T, shift: T): T
Performs the sign-agnostic rotate left operation on a 32-bit or 64-bit integer.
T 操作说明 i32、u32 i32.rotl i64、u64 i64.rotl i8、u8、i16、u16 效仿的 布尔值 没有任何 function rotr<T>(value: T, shift: T): T
Performs the sign-agnostic rotate right operation on a 32-bit or 64-bit integer.
T 操作说明 i32、u32 i32.rotr i64、u64 i64.rotr i8、u8、i16、u16 效仿的 布尔值 没有任何 function abs<T>(value: T): T
Computes the absolute value of an integer or float.
T 操作说明 F32 f32.abs f64 f64.abs i8、i16、i32、i64 效仿的 u8、u16、u32、u64、布尔 没有任何 function max<T>(left: T, right: T): T
Determines the maximum of two integers or floats. If either operand is
NaN
, returnsNaN
.T 操作说明 F32 f32 max f64 f64.max i8、u8、i16、u16、i32、u32、i64、u64、布尔 效仿的 function min<T>(left: T, right: T): T
Determines the minimum of two integers or floats. If either operand is
NaN
, returnsNaN
.T 操作说明 F32 f32.min f64 f64.min i8、u8、i16、u16、i32、u32、i64、u64、布尔 效仿的 function ceil<T>(value: T): T
Performs the ceiling operation on a 32-bit or 64-bit float.
T 操作说明 F32 f32.ceil f64 f64.ceil i8、u8、i16、u16、i32、u32、i64、u64、布尔 没有任何 function floor<T>(value: T): T
Performs the floor operation on a 32-bit or 64-bit float.
T 操作说明 F32 f32.floor f64 f64.floor i8、u8、i16、u16、i32、u32、i64、u64、布尔 没有任何 function copysign<T>(x: T , y: T): T
Composes a 32-bit or 64-bit float from the magnitude of
x
and the sign ofy
.T 操作说明 F32 f32.copysign f64 f64.copysign function nearest<T>(value: T): T
Rounds to the nearest integer half to even of a 32-bit or 64-bit float.
T 操作说明 F32 f32.nearest f64 f64.nearest i8、u8、i16、u16、i32、u32、i64、u64、布尔 没有任何 function reinterpret<TTo>(value: auto): TTo
Reinterprets the bits of the specified value as type
T
.时间到 操作说明 i32、u32 i32.reinterpret_f32 i64、u64 i64.reinterpret_f64 F32 f32.reinterpret_i32 f64 f64.reinterpret_i64 function sqrt<T>(value: T): T
Calculates the square root of a 32-bit or 64-bit float.
T 操作说明 F32 f32.sqrt f64 f64.sqrt function trunc<T>(value: T): T
Rounds to the nearest integer towards zero of a 32-bit or 64-bit float.
T 操作说明 F32 f32.trunc f64 f64.trunc i8、u8、i16、u16、i32、u32、i64、u64、布尔 没有任何
# 记忆
¥Memory
类似地,以下内置函数触发访问或以其他方式修改内存的 WebAssembly 指令。
¥Similarly, the following built-ins emit WebAssembly instructions accessing or otherwise modifying memory.
注意
immOffset
和 immAlign
参数(如果提供)必须是编译时常量值。更多详情请参见 rationale (opens new window)。
¥The immOffset
and immAlign
arguments, if provided, must be compile time constant values. See more details in rationale (opens new window).
function load<T>(ptr: usize, immOffset?: usize, immAlign?: usize): T
Loads a value of the specified type from memory. Equivalent to dereferencing a pointer in other languages.
T 操作说明 如果上下文类型是 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 不适用 F32 f32.load 不适用 f64 f64.load 不适用 <参考> i32/i64.load 不适用 function store<T>(ptr: usize, value: auto, immOffset?: usize, immAlign?: usize): void
Stores a value of the specified type to memory. Equivalent to dereferencing a pointer in other languages and assigning a value.
T 操作说明 如果值为 i64 i8、u8 i32.store8 i64.store8 i16、u16 i32.store16 i64.store16 i32、u32 i32.store i64.store32 i64、u64 i64.store 不适用 F32 f32.store 不适用 f64 f64.store 不适用 <参考> i32/i64.store 不适用 function memory.size(): i32
返回当前内存大小(以页为单位)。一页有 64kb。
¥Returns the current size of the memory in units of pages. One page is 64kb.
function memory.grow(value: i32): i32
按给定的无符号页增量增加线性内存。一页有 64kb。返回先前的内存大小(以页为单位)或失败时返回
-1
。¥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
memory.grow
where a memory manager is present might break it. :::呼唤function memory.copy(dst: usize, src: usize, n: usize): void
将
n
字节从src
复制到dst
。 区域可能重叠。如果启用了大容量内存,则触发相应的指令,否则会发送一个 polyfill。¥Copies
n
bytes fromsrc
todst
. Regions may overlap. Emits the respective instruction if bulk-memory is enabled, otherwise ships a polyfill.function memory.fill(dst: usize, value: u8, n: usize): void
用给定的字节
value
填充dst
处的n
字节。如果启用了大容量内存,则触发相应的指令,否则会发送一个 polyfill。¥Fills
n
bytes atdst
with the given bytevalue
. Emits the respective instruction if bulk-memory is enabled, otherwise ships a polyfill.function memory.repeat(dst: usize, src: usize, srcLength: usize, count: usize): void
将指定为
src
和srcLength
的字节序列重复count
次到目标dst
。¥Repeats a sequence of bytes given as
src
withsrcLength
count
times into destinationdst
.function memory.compare(lhs: usize, rhs: usize, n: usize): i32
比较
left
和right
的前n
字节并返回一个指示它们关系的值:¥Compares the first
n
bytes ofleft
andright
and returns a value that indicates their relationship:如果
lhs
中的第一个不同字节小于rhs
中的相应字节,则为负值。¥Negative value if the first differing byte in
lhs
is less than the corresponding byte inrhs
.如果
lhs
中的第一个不同字节大于rhs
中的相应字节,则为正值。¥Positive value if the first differing byte in
lhs
is greater than the corresponding byte inrhs
.如果
lhs
和rhs
的所有n
字节都相等,则为零。¥Zero if all
n
bytes oflhs
andrhs
are equal.
function memory.data(size: i32, align?: i32): usize
获取指向给定大小的归零静态内存块的指针。对齐方式默认为
16
。参数必须是编译时常量。¥Gets a pointer to a zeroed static chunk of memory of the given size. Alignment defaults to
16
. Arguments must be compile-time constants.function memory.data<T>(values: T[], align?: i32): usize
获取指向预初始化静态内存块的指针。对齐方式默认为
T
的大小。参数必须是编译时常量。¥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
function select<T>(ifTrue: T, ifFalse: T, condition: bool): T
根据条件选择两个预评估值之一。与
if/else
的不同之处在于,始终执行两个臂,并根据之后的条件选择最终值。仅当条件是随机的(意味着:分支预测不会表现良好)并且两种替代方案都很便宜时,性能才比if/else
更好。还值得注意的是,Binaryen 会进行相关优化,例如自动切换到select
,因此简单地使用三元? :
可能会更好。¥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 anif/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 aselect
automatically, so simply using a ternary? :
may be preferable.function unreachable(): auto
触发无法访问的指令,该指令在执行时会导致运行时错误(陷阱)。任何类型的语句和表达式。请注意,托管代码中的陷阱很可能会导致内存泄漏,甚至会破坏程序,因为它会过早结束执行。
¥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 🦄
以下说明代表 WebAssembly 线程和原子 (opens new window) 规范。必须使用 --enable threads
启用。
¥The following instructions represent the WebAssembly threads and atomics (opens new window) specification. Must be enabled with --enable threads
.
function atomic.load<T>(ptr: usize, immOffset?: usize): T
以原子方式从内存加载整数值并返回它。
¥Atomically loads an integer value from memory and returns it.
function atomic.store<T>(ptr: usize, value: auto, immOffset?: usize): void
以原子方式将整数值存储到内存中。
¥Atomically stores an integer value to memory.
function atomic.add<T>(ptr: usize, value: T, immOffset?: usize): T
在内存中原子地添加一个整数值。
¥Atomically adds an integer value in memory.
function atomic.sub<T>(ptr: usize, value: T, immOffset?: usize): T
原子地减去内存中的整数值。
¥Atomically subtracts an integer value in memory.
function atomic.and<T>(ptr: usize, value: T, immOffset?: usize): T
以原子方式对内存中的整数值执行按位 AND 运算。
¥Atomically performs a bitwise AND operation on an integer value in memory.
function atomic.or<T>(ptr: usize, value: T, immOffset?: usize): T
以原子方式对内存中的整数值执行按位或运算。
¥Atomically performs a bitwise OR operation on an integer value in memory.
function atomic.xor<T>(ptr: usize, value: T, immOffset?: usize): T
以原子方式对内存中的整数值执行按位异或运算。
¥Atomically performs a bitwise XOR operation on an integer value in memory.
function atomic.xchg<T>(ptr: usize, value: T, immOffset?: usize): T
以原子方式交换内存中的整数值。
¥Atomically exchanges an integer value in memory.
function atomic.cmpxchg<T>(ptr: usize, expected: T, replacement: T, immOffset?: usize): T
如果满足条件,则自动比较并交换内存中的整数值。
¥Atomically compares and exchanges an integer value in memory if the condition is met.
function atomic.wait<T>(ptr: usize, expected: T, timeout: i64): AtomicWaitResult
对内存中的地址执行等待操作,如果满足整数条件则挂起此代理。返回值为
¥Performs a wait operation on an address in memory, suspending this agent if the integer condition is met. Return values are
值 描述 0 OK - 被另一个特工吵醒了。 1 NOT_EQUAL - 加载的值与预期值不匹配。 2 TIMED_OUT - 超时之前未唤醒。 function atomic.notify(ptr: usize, count: i32): i32
对内存中的地址执行通知操作,唤醒挂起的代理。
¥Performs a notify operation on an address in memory waking up suspended agents.
function atomic.fence(): void
执行栅栏操作,保留高级语言的同步保证。
¥Performs a fence operation, preserving synchronization guarantees of higher level languages.
# SIMD 🦄
同样,这些代表 WebAssembly SIMD (opens new window) 规范。必须使用 --enable simd
启用。
¥Likewise, these represent the WebAssembly SIMD (opens new window) specification. Must be enabled with --enable simd
.
function v128(a: i8, ... , p: i8): v128
从 16 个 8 位整数值初始化一个 128 位向量。参数必须是编译时常量。
¥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.
function v128.splat<T>(x: T): v128
Creates a vector with identical lanes.
T 操作说明 i8、u8 i8x16.splat i16、u16 i16x8.splat i32、u32 i32x4.splat i64、u64 i64x2.splat F32 f32x4.splat f64 f64x2.splat function v128.extract_lane<T>(x: v128, idx: u8): T
Extracts one lane as a scalar.
T 操作说明 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 function v128.replace_lane<T>(x: v128, idx: u8, value: T): v128
Replaces one lane.
T 操作说明 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 function v128.shuffle<T>(a: v128, b: v128, ...lanes: u8[]): v128
Selects lanes from either vector according to the specified lane indexes.
T 操作说明 i8、u8 i8x16.shuffle i16、u16 i8x16.shuffle 模拟 i16x8.shuffle i32、u32 i8x16.shuffle 模拟 i32x4.shuffle i64、u64 i8x16.shuffle 模拟 i64x2.shuffle F32 i8x16.shuffle 模拟 f32x4.shuffle f64 i8x16.shuffle 模拟 f64x2.shuffle function v128.swizzle(a: v128, s: v128): v128
根据第二个向量的 8 位通道指定的索引 [0-15] 从第一个向量中选择 8 位通道。
¥Selects 8-bit lanes from the first vector according to the indexes [0-15] specified by the 8-bit lanes of the second vector.
function v128.load(ptr: usize, immOffset?: usize, immAlign?: usize): v128
从内存加载向量。
¥Loads a vector from memory.
function v128.load_ext<TFrom>(ptr: usize, immOffset?: usize, immAlign?: usize): v128
Creates a vector by loading the lanes of the specified integer type and extending each to the next larger type.
时间从 操作说明 i8 v128.load8x8_s u8 v128.load8x8_u i16 v128.load16x4_s u16 v128.load16x4_u i32 v128.load32x2_s u32 v128.load32x2_u function v128.load_zero<TFrom>(ptr: usize, immOffset?: usize, immAlign?: usize): v128
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.
时间从 操作说明 i32、u32、f32 v128.load32_zero i64、u64、f64 v128.load64_zero function v128.load_lane<T>( ptr: usize, vec: v128, idx: u8, immOffset?: usize, immAlign?: usize ): v128
Loads a single lane from memory into the specified lane of the given vector. Other lanes are bypassed as is.
T 操作说明 i8、u8 v128.load8_lane i16、u16 v128.load16_lane i32、u32、f32 v128.load32_lane i64、u64、f64 v128.load64_lane function v128.store_lane<T>( ptr: usize, vec: v128, idx: u8, immOffset?: usize, immAlign?: usize ): v128
Stores the single lane at the specified index of the given vector to memory.
T 操作说明 i8、u8 v128.store8_lane i16、u16 v128.store16_lane i32、u32、f32 v128.store32_lane i64、u64、f64 v128.store64_lane function v128.load_splat<T>(ptr: usize, immOffset?: usize, immAlign?: usize): v128
Creates a vector with identical lanes by loading the splatted value.
T 操作说明 i8、u8 v128.load8_splat i16、u16 v128.load16_splat i32、u32、f32 v128.load32_splat i64、u64、f64 v128.load64_splat function v128.store(ptr: usize, value: v128, immOffset?: usize, immAlign?: usize): void
将向量存储到内存中。
¥Stores a vector to memory.
function v128.add<T>(a: v128, b: v128): v128
Adds each lane.
T 操作说明 i8、u8 i8x16.add i16、u16 i16x8.add i32、u32 i32x4.add i64、u64 i64x2.add F32 f32x4.add f64 f64x2.add function v128.sub<T>(a: v128, b: v128): v128
Subtracts each lane.
T 操作说明 i8、u8 i8x16.sub i16、u16 i16x8.sub i32、u32 i32x4.sub i64、u64 i64x2.sub F32 f32x4.sub f64 f64x2.sub function v128.mul<T>(a: v128, b: v128): v128
Multiplies each lane.
T 操作说明 i16、u16 i16x8.mul i32、u32 i32x4.mul i64、u64 i64x2.mul F32 f32x4.mul f64 f64x2.mul function v128.div<T>(a: v128, b: v128): v128
Divides each floating point lane.
T 操作说明 F32 f32x4.div f64 f64x2.div function v128.neg<T>(a: v128): v128
Negates each lane.
T 操作说明 i8、u8 i8x16.neg i16、u16 i16x8.neg i32、u32 i32x4.neg i64、u64 i64x2.neg F32 f32x4.neg f64 f64x2.neg function v128.add_sat<T>(a: v128, b: v128): v128
Adds each signed small integer lane using saturation.
T 操作说明 i8 i8x16.add_sat_s u8 i8x16.add_sat_u i16 i16x8.add_sat_s u16 i16x8.add_sat_u function v128.sub_sat<T>(a: v128, b: v128): v128
Subtracts each signed small integer lane using saturation.
T 操作说明 i8 i8x16.sub_sat_s u8 i8x16.sub_sat_u i16 i16x8.sub_sat_s u16 i16x8.sub_sat_u function v128.shl<T>(a: v128, b: i32): v128
Performs a bitwise left shift by a scalar on each integer lane.
T 操作说明 i8、u8 i8x16.shl i16、u16 i16x8.shl i32、u32 i32x4.shl i64、u64 i64x2.shl function v128.shr<T>(a: v128, b: i32): v128
Performs a bitwise right shift by a scalar on each integer lane.
T 操作说明 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 function v128.and(a: v128, b: v128): v128
对每个通道执行按位
a & b
操作。¥Performs the bitwise
a & b
operation on each lane.function v128.or(a: v128, b: v128): v128
对每个通道执行按位
a | b
操作。¥Performs the bitwise
a | b
operation on each lane.function v128.xor(a: v128, b: v128): v128
对每个通道执行按位
a ^ b
操作。¥Performs the bitwise
a ^ b
operation on each lane.function v128.andnot(a: v128, b: v128): v128
对每个通道执行按位
a & !b
操作。¥Performs the bitwise
a & !b
operation on each lane.function v128.not(a: v128): v128
对每个通道执行按位
!a
操作。¥Performs the bitwise
!a
operation on each lane.function v128.bitselect(v1: v128, v2: v128, mask: v128): v128
根据指定的掩码选择任一向量的位。如果
mask
中的位是1
,则从v1
中选择,否则从v2
中选择。¥Selects bits of either vector according to the specified mask. Selects from
v1
if the bit inmask
is1
, otherwise fromv2
.function v128.any_true(a: v128): bool
将向量简化为标量,指示是否有任何车道被视为
true
。¥Reduces a vector to a scalar indicating whether any lane is considered
true
.function v128.all_true<T>(a: v128): bool
Reduces a vector to a scalar indicating whether all lanes are considered
true
.T 操作说明 i8、u8 i8x16.all_true i16、u16 i16x8.all_true i32、u32 i32x4.all_true i64、u64 i64x2.all_true function v128.bitmask<T>(a: v128): bool
Extracts the high bit of each integer lane (except 64-bit) and produces a scalar mask with all bits concatenated.
T 操作说明 i8、u8 i8x16.bitmask i16、u16 i16x8.bitmask i32、u32 i32x4.bitmask i64、u64 i64x2.bitmask function v128.popcnt<T>(a: v128): v128
Counts the number of bits set to one within each lane.
T 操作说明 i8、u8 i8x16.popcnt function v128.min<T>(a: v128, b: v128): v128
Computes the minimum of each lane.
T 操作说明 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 function v128.max<T>(a: v128, b: v128): v128
Computes the maximum of each lane.
T 操作说明 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 function v128.pmin<T>(a: v128, b: v128): v128
Computes the psuedo-minimum of each lane.
T 操作说明 F32 f32x4.pmin f64 f64x2.pmin function v128.pmax<T>(a: v128, b: v128): v128
Computes the pseudo-maximum of each lane.
T 操作说明 F32 f32x4.pmax f64 f64x2.pmax function v128.dot<T>(a: v128, b: v128): v128
Computes the dot product of two 16-bit integer lanes each, yielding lanes one size wider than the input.
T 操作说明 i16 i32x4.dot_i16x8_s function v128.avgr<T>(a: v128, b: v128): v128)
Computes the rounding average of each unsigned small integer lane.
T 操作说明 u8 i8x16.avgr_u u16 i16x8.avgr_u function v128.abs<T>(a: v128): v128
Computes the absolute value of each lane (except 64-bit integers).
T 操作说明 i8、u8 i8x16.abs i16、u16 i16x8.abs i32、u32 i32x4.abs i64、u64 i64x2.abs F32 f32x4.abs f64 f64x2.abs function v128.sqrt<T>(a: v128): v128
Computes the square root of each floating point lane.
T 操作说明 F32 f32x4.sqrt f64 f64x2.sqrt function v128.ceil<T>(a: v128): v128
Performs the ceiling operation on each lane.
T 操作说明 F32 f32x4.ceil f64 f64x2.ceil function v128.floor<T>(a: v128): v128
Performs the floor operation on each lane.
T 操作说明 F32 f32x4.floor f64 f64x2.floor function v128.trunc<T>(a: v128): v128
Rounds to the nearest integer towards zero of each lane.
T 操作说明 F32 f32x4.trunc f64 f64x2.trunc function v128.nearest<T>(a: v128): v128
Rounds to the nearest integer half to even of each lane.
T 操作说明 F32 f32x4.nearest f64 f64x2.nearest function v128.eq<T>(a: v128, b: v128): v128
Computes which lanes are equal.
T 操作说明 i8、u8 i8x16.eq i16、u16 i16x8.eq i32、u32 i32x4.eq i64、u64 i64x2.eq F32 f32x4.eq f64 f64x2.eq function v128.ne<T>(a: v128, b: v128): v128
Computes which lanes are not equal.
T 操作说明 i8、u8 i8x16.ne i16、u16 i16x8.ne i32、u32 i32x4.ne i64、u64 i64x2.ne F32 f32x4.ne f64 f64x2.ne function v128.lt<T>(a: v128, b: v128): v128
Computes which lanes of the first vector are less than those of the second.
T 操作说明 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 function v128.le<T>(a: v128, b: v128): v128
Computes which lanes of the first vector are less than or equal those of the second.
T 操作说明 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 function v128.gt<T>(a: v128, b: v128): v128
Computes which lanes of the first vector are greater than those of the second.
T 操作说明 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 function v128.ge<T>(a: v128, b: v128): v128
Computes which lanes of the first vector are greater than or equal those of the second.
T 操作说明 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 function v128.convert<TFrom>(a: v128): v128
Converts each lane of a vector from integer to single-precision floating point.
时间从 操作说明 i32 f32x4.convert_i32x4_s u32 f32x4.convert_i32x4_u function v128.convert_low<TFrom>(a: v128): v128
Converts the low lanes of a vector from integer to double-precision floating point.
时间从 操作说明 i32 f64x2.convert_low_i32x4_s u32 f64x2.convert_low_i32x4_u function v128.trunc_sat<TTo>(a: v128): v128
Truncates each lane of a vector from single-precision floating point to integer with saturation. Takes the target type.
时间到 操作说明 i32 i32x4.trunc_sat_f32x4_s u32 i32x4.trunc_sat_f32x4_u function v128.trunc_sat_zero<TTo>(a: v128): v128
Truncates each lane of a vector from double-precision floating point to integer with saturation. Takes the target type.
时间到 操作说明 i32 i32x4.trunc_sat_f64x2_s_zero u32 i32x4.trunc_sat_f64x2_u_zero function v128.narrow<TFrom>(a: v128, b: v128): v128
Narrows wider integer lanes to their respective narrower lanes.
时间从 操作说明 i16 i8x16.narrow_i16x8_s u16 i8x16.narrow_i16x8_u i32 i16x8.narrow_i32x4_s u32 i16x8.narrow_i32x4_u function v128.extend_low<TFrom>(a: v128): v128
Extends the low half of narrower integer lanes to their respective wider lanes.
时间从 操作说明 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 function v128.extend_high<TFrom>(a: v128): v128
Extends the high half of narrower integer lanes to their respective wider lanes.
时间从 操作说明 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 function v128.extadd_pairwise<TFrom>(a: v128): v128
Adds lanes pairwise producing twice wider extended results.
时间从 操作说明 i16 i16x8.extadd_pairwise_i8x16_s u16 i16x8.extadd_pairwise_i8x16_u i32 i32x4.extadd_pairwise_i16x8_s u32 i32x4.extadd_pairwise_i16x8_u function v128.demote_zero<T>(a: v128): v128
Demotes each float lane to lower precision. The higher lanes of the result are initialized to zero.
T 操作说明 f64 f32x4.demote_f64x2_zero function v128.promote_low<T>(a: v128): v128
Promotes the lower float lanes to higher precision.
T 操作说明 F32 f64x2.promote_low_f32x4 function v128.q15mulr_sat<T>(a: v128, b: v128): v128
Performs the line-wise saturating rounding multiplication in Q15 format ((a[i] * b[i] + (1 << (Q - 1))) >> Q where Q=15).
T 操作说明 i16 i16x8.q15mulr_sat_s function v128.extmul_low<T>(a: v128, b: v128): v128
Performs the lane-wise integer extended multiplication of the lower lanes producing a twice wider result than the inputs.
T 操作说明 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 function v128.extmul_high<T>(a: v128, b: v128): v128
Performs the lane-wise integer extended multiplication of the higher lanes producing a twice wider result than the inputs.
T 操作说明 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
function i8x16(a: i8, ... , p: i8): v128
从 16 个 8 位整数值初始化一个 128 位向量。
¥Initializes a 128-bit vector from sixteen 8-bit integer values.
function i16x8(a: i16, ..., h: i16): v128
从八个 16 位整数值初始化一个 128 位向量。
¥Initializes a 128-bit vector from eight 16-bit integer values.
function i32x4(a: i32, b: i32, c: i32, d: i32): v128
从四个 32 位整数值初始化一个 128 位向量。
¥Initializes a 128-bit vector from four 32-bit integer values.
function i64x2(a: i64, b: i64): v128
从两个 64 位整数值初始化 128 位向量。
¥Initializes a 128-bit vector from two 64-bit integer values.
function f32x4(a: f32, b: f32, c: f32, d: f32): v128
从四个 32 位浮点值初始化一个 128 位向量。
¥Initializes a 128-bit vector from four 32-bit float values.
function f64x2(a: f64, b: f64): v128
从两个 64 位浮点值初始化 128 位向量。
¥Initializes a 128-bit vector from two 64-bit float values.
# 宽松的 SIMD 🦄
¥Relaxed SIMD 🦄
以下说明代表 WebAssembly 宽松 SIMD (opens new window) 规范。必须使用 --enable relaxed-simd
启用。
¥The following instructions represent the WebAssembly Relaxed SIMD (opens new window) specification. Must be enabled with --enable relaxed-simd
.
function v128.relaxed_swizzle(a: v128, s: v128): v128
使用
s
中的索引从a
选择 8 位通道。[0-15]范围内的索引选择a
的第 i 个元素。¥Selects 8-bit lanes from
a
using indices ins
. Indices in the range [0-15] select the i-th element ofa
.与
v128.swizzle
不同,越界索引的结果是实现定义的,具体取决于硬件功能:0
或a[s[i]%16]
。¥Unlike
v128.swizzle
, the result of an out of bounds index is implementation-defined, depending on hardware capabilities: Either0
ora[s[i]%16]
.function v128.relaxed_trunc<T>(a: v128): v128
Truncates each lane of a vector from 32-bit floating point to a 32-bit signed or unsigned integer as indicated by T.
T 操作说明 i32 i32x4.relaxed_trunc_f32x4_s u32 i32x4.relaxed_trunc_f32x4_u 与
v128.trunc_sat
不同,通道超出目标类型边界的结果是实现定义的,具体取决于硬件功能:¥Unlike
v128.trunc_sat
, the result of lanes out of bounds of the target type is implementation defined, depending on hardware capabilities:如果输入通道包含
NaN
,则结果是0
或相应的最大整数值。¥If the input lane contains
NaN
, the result is either0
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.
function v128.relaxed_trunc_zero<T>(a: v128): v128
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 操作说明 i32 i32x4.relaxed_trunc_f64x2_s_zero u32 i32x4.relaxed_trunc_f64x2_u_zero 与
v128.trunc_sat_zero
不同,通道超出目标类型边界的结果是实现定义的,具体取决于硬件功能:¥Unlike
v128.trunc_sat_zero
, the result of lanes out of bounds of the target type is implementation defined, depending on hardware capabilities:如果输入通道包含
NaN
,则结果是0
或相应的最大整数值。¥If the input lane contains
NaN
, the result is either0
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.
function v128.relaxed_madd<T>(a: v128, b: v128, c: v128): v128
Performs the fused multiply-add operation (a * b + c) on 32- or 64-bit floating point lanes as indicated by T.
T 操作说明 F32 f32x4.relaxed_madd f64 f64x2.relaxed_madd 结果是根据硬件功能定义的实现:
¥The result is implementation defined, depending on hardware capabilities:
a * b
四舍五入一次,最终结果再次四舍五入,或者¥Either
a * b
is rounded once and the final result rounded again, or该表达式以更高的精度进行计算,并且仅舍入一次
¥The expression is evaluated with higher precision and only rounded once
function v128.relaxed_nmadd<T>(a: v128, b: v128, c: v128): v128
Performs the fused negative multiply-add operation (-(a * b) + c) on 32- or 64-bit floating point lanes as indicated by T.
T 操作说明 F32 f32x4.relaxed_nmadd f64 f64x2.relaxed_nmadd 结果是根据硬件功能定义的实现:
¥The result is implementation defined, depending on hardware capabilities:
a * b
四舍五入一次,最终结果再次四舍五入,或者¥Either
a * b
is rounded once and the final result rounded again, or该表达式以更高的精度进行计算,并且仅舍入一次
¥The expression is evaluated with higher precision and only rounded once
function v128.relaxed_laneselect<T>(a: v128, b: v128, m: v128): v128
Selects 8-, 16-, 32- or 64-bit integer lanes as indicated by T from a or b based on masks in m.
T 操作说明 i8、u8 i8x16.relaxed_laneselect i16、u16 i16x8.relaxed_laneselect i32、u32 i32x4.relaxed_laneselect i64、u64 i64x2.relaxed_laneselect 如果
m
中的掩码确实设置了所有位(结果为a[i]
)或未设置(结果为b[i]
),则行为类似于v128.bitselect
。否则,结果是实现定义的,具体取决于硬件功能:如果设置了m
的最高有效位,则结果为bitselect(a[i], b[i], mask)
或a[i]
,否则结果为b[i]
。¥Behaves like
v128.bitselect
if masks inm
do have all bits either set (result isa[i]
) or unset (result isb[i]
). Otherwise the result is implementation-defined, depending on hardware capabilities: If the most significant bit ofm
is set, the result is eitherbitselect(a[i], b[i], mask)
ora[i]
, otherwise the result isb[i]
.function v128.relaxed_min<T>(a: v128, b: v128): v128
Computes the minimum of each 32- or 64-bit floating point lane as indicated by T.
T 操作说明 F32 f32x4.relaxed_min f64 f64x2.relaxed_min 与
v128.min
不同,如果任一值为NaN
或两者均为-0.0
和+0.0
,则结果是实现定义的,具体取决于硬件功能:a[i]
或b[i]
。¥Unlike
v128.min
, the result is implementation-defined if either value isNaN
or both are-0.0
and+0.0
, depending on hardware capabilities: Eithera[i]
orb[i]
.function v128.relaxed_max<T>(a: v128, b: v128): v128
Computes the maximum of each 32- or 64-bit floating point lane as indicated by T.
T 操作说明 F32 f32x4.relaxed_max f64 f64x2.relaxed_max 与
v128.max
不同,如果任一值为NaN
或两者均为-0.0
和+0.0
,则结果是实现定义的,具体取决于硬件功能:a[i]
或b[i]
。¥Unlike
v128.max
, the result is implementation-defined if either value isNaN
or both are-0.0
and+0.0
, depending on hardware capabilities: Eithera[i]
orb[i]
.function v128.relaxed_q15mulr<T>(a: v128, b: v128): v128
Performs the lane-wise rounding multiplication in Q15 format ((a[i] * b[i] + (1 << (Q - 1))) >> Q where Q=15).
T 操作说明 i16 i16x8.relaxed_q15mulr_s 与
v128.q15mulr_sat
不同,如果两个输入都是最小有符号值,则结果是实现定义的:最小或最大有符号值。¥Unlike
v128.q15mulr_sat
, the result is implementation-defined if both inputs are the minimum signed value: Either the minimum or maximum signed value.function v128.relaxed_dot<T>(a: v128, b: v128): v128
Computes the dot product of two 8-bit integer lanes each, yielding lanes one size wider than the input.
T 操作说明 i16 i16x8.relaxed_dot_i8x16_i7x16_s 与
v128.dot
不同,如果设置了b[i]
的最高有效位,则b[i]
是否被中间乘法解释为有符号或无符号是实现定义的。¥Unlike
v128.dot
, if the most significant bit ofb[i]
is set, whetherb[i]
is interpreted as signed or unsigned by the intermediate multiplication is implementation-defined.function v128.relaxed_dot_add<T>(a: v128, b: v128, c: v128): v128
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 操作说明 i32 i32x4.relaxed_dot_i8x16_i7x16_add_s 与
v128.dot
不同,如果设置了b[i]
的最高有效位,则b[i]
是否被中间乘法解释为有符号或无符号是实现定义的。¥Unlike
v128.dot
, if the most significant bit ofb[i]
is set, whetherb[i]
is interpreted as signed or unsigned by the intermediate multiplication is implementation-defined.
# 内联指令
¥Inline instructions
除了使用上面的通用内置函数之外,大多数 WebAssembly 指令都可以直接用 AssemblyScript 代码编写。例如,以下内容是等效的:
¥In addition to using the generic builtins above, most WebAssembly instructions can be written directly in AssemblyScript code. For example, the following is equivalent:
// generic builtin
v128.splat<i32>(42);
// inline instruction
i32x4.splat(42);