Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

SIMD.%type%.select()

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The static SIMD.%type%.select() method creates a new integer SIMD data type with the lane values being a selection match from a selector mask.

Syntax

SIMD.Float32x4.select(mask, trueValue, falseValue)
SIMD.Float64x2.select(mask, trueValue, falseValue)

SIMD.Int8x16.select(mask, trueValue, falseValue)
SIMD.Int16x8.select(mask, trueValue, falseValue)
SIMD.Int32x4.select(mask, trueValue, falseValue)

SIMD.Uint8x16.select(mask, trueValue, falseValue)
SIMD.Uint16x8.select(mask, trueValue, falseValue)
SIMD.Uint32x4.select(mask, trueValue, falseValue)

Parameters

mask
An int32x4, int16x8 or int8x16 that is used as the selector mask.
trueValue
If the selector mask lane is true, pick the corresponding lane value from here.
falseValue
If the selector mask lane is false, pick the corresponding lane value from here.

Return value

A new SIMD data type.

Description

The SIMD.%type%.select() method selects lanes from a selection mask. Masking (or "branching") lanes is useful as you can't operate on a fraction of data in SIMD data types. However, with masks and the select function, you can branch and merge vectors to assemble the result vector you need.

Examples

Custom selection mask

This example uses the SIMD.Bool32x4 type to create a custom selection mask. With this mask, you are able to select the first and the last lane from the first Float32x4 data type. Thus, the select function selects the first and last lane from vector a and the second and third lane from vector b (or the sum vector in the second select).

var a = SIMD.Float32x4(1, 2, 3, 4);
var b = SIMD.Float32x4(5, 6, 7, 8);

var mask = SIMD.Bool32x4(true, false, false, true);

SIMD.Float32x4.select(mask, a, b);
// Float32x4[1, 6, 7, 4]

var sum = SIMD.Float32x4.add(a,b);
SIMD.Float32x4.select(mask, a, sum);
// Float32x4[1, 8, 10, 4]

Operations returning a selection mask

All SIMD comparison operations return a selection mask from which you have to select to actually get the result of the comparison:

var a = SIMD.Float32x4(0, 12, 3, 4);
var b = SIMD.Float32x4(0, 6, 7, 50);

var mask = SIMD.Float32x4.lessThan(a,b);
// Bool32x4[false, false, true, true]

var result = SIMD.Float32x4.select(mask, a, b);
// Float32x4[0, 6, 3, 4]

Specifications

Specification Status Comment
SIMD
The definition of 'SIMDConstructor.select' in that specification.
Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support No support Nightly build No support No support No support
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support Nightly build No support No support No support

See also

文档标签和贡献者

 此页面的贡献者: eduardoboucas, fscholz
 最后编辑者: eduardoboucas,