Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil) . Junte-se a nós e ajude a fazer o trabalho!
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.
SIMD (pronounced "sim-dee") is short for Single Instruction/Multiple Data which is one classification of computer architectures. SIMD operations perform the same computation on multiple data points resulting in data level parallelism and thus performance gains, for example for 3D graphics and video processing, physics simulations or cryptography, and other domains.
This page and sub pages are the SIMD API reference documentation. See also SIMD types for an article that describes SIMD in JavaScript more generally.
Description
The JavaScript SIMD API consists of several new types and operations. Browsers provide highly optimized implementations of this API depending on the underlying hardware of the user. Currently, SIMD is especially modeled for ARMv7 platforms with NEON and x86 platforms with SSE.
The SIMD API types are installed on a SIMD
module. Unlike the other global objects, SIMD
is not a constructor. You can not use it with a new
operator or invoke the SIMD
object as a function. All properties and methods of SIMD
are static (as is the case with the Math
object).
Overview
A SIMD value has multiple lanes. For a vector of length 4, the lanes are named x
, y
, z
, and w
. Now, instead of having to perform 4 separate operations on each of these lanes, SIMD allows you to perform the operation on all 4 lanes simultaneously. This requires fewer operations, which leads to performance improvements and better energy efficiency compared to scalar operations (SISD). Note that SIMD operations cannot be used to process multiple data in different ways. In the following figure, there is only a single instruction (addition) and thus it could be operated with SIMD:
Figures 1 and 2: SISD and SIMD compared.
Simple addition arithmetic
The JavaScript code for a simple SIMD operation like in figure 2 looks like this:
var a = SIMD.Float32x4(1, 2, 3, 4); var b = SIMD.Float32x4(5, 6, 7, 8); var c = SIMD.Float32x4.add(a,b); // Float32x4[6,8,10,12]
Data types
All SIMD data types are immutable. You can not alter them directly. Instead, you perform operations that create new immutable SIMD data types.
The following figure shows the different SIMD data types in a 128-bit SIMD register. The current SIMD JavaScript API has 12 different types with lane lengths of either 2, 4, 8 or 16.
Figure 3: Lanes per type in a 128-bit SIMD register.
SIMD boolean types
SIMD.Bool8x16
- 128-bits divided into 16 lanes storing boolean values.
SIMD.Bool16x8
- 128-bits divided into 8 lanes storing boolean values.
SIMD.Bool32x4
- 128-bits divided into 4 lanes storing boolean values.
SIMD.Bool64x2
- 128-bits divided into 2 lanes storing boolean values.
SIMD signed integer types
SIMD.Int8x16
- 128-bits divided into 16 lanes storing 8-bit signed integer values.
SIMD.Int16x8
- 128-bits divided into 8 lanes storing 16-bit signed integer values.
SIMD.Int32x4
- 128-bits divided into 4 lanes storing 32-bit signed integer values.
SIMD unsigned integer types
SIMD.Uint8x16
- 128-bits divided into 16 lanes storing 8-bit unsigned integer values.
SIMD.Uint16x8
- 128-bits divided into 8 lanes storing 16-bit unsigned integer values.
SIMD.Uint32x4
- 128-bits divided into 4 lanes storing 32-bit unsigned integer values.
SIMD floating-point types
SIMD.Float32x4
- 128-bits divided into 4 lanes storing single precision floating point values.
SIMD.Float64x2
- 128-bits divided into 2 lanes storing double precision floating point values.
Constructor functions
In addition to the simple creator functions (e.g. SIMD.Int32x4(1,2,3,4)
), the SIMD API provides the following constructor functions:
SIMD.%type%.splat()
- Creates SIMD data type with all lanes set to a given value.
You can also convert from one SIMD data type to another.
Note: SIMD types don't work with new
, as SIMD values are no "boxed" objects (comparable to String(s)
vs. new String(s)
, which creates a String object).
var v = new SIMD.Float32x4(0,1,2,3); // TypeError: SIMD.Float32x4 is not a constructor
Instead, you just write:
var v = SIMD.Float32x4(0,1,2,3);
Operations
To actually do something with SIMD types, SIMD operations are needed that work on SIMD data types.
Note: Not all SIMD operations are available on all SIMD types, see the individual reference pages for details and availability.
Checking SIMD types
SIMD.%type%.check()
- Returns a new instance if the parameter is a valid SIMD data type and the same as
%type%
. Throws aTypeError
otherwise.
Accessing and mutating lanes
SIMD.%type%.extractLane()
- Returns the value of the given lane.
SIMD.%type%.replaceLane()
- Returns a new instance with the given lane value replaced.
Loading from and storing into typed arrays
SIMD.%type%.load()
SIMD.%type%.load1()
SIMD.%type%.load2()
SIMD.%type%.load3()
- Returns a new instance with the lane values loaded from a typed array.
SIMD.%type%.store()
SIMD.%type%.store1()
SIMD.%type%.store2()
SIMD.%type%.store3()
- Store a SIMD data type into a typed array.
Arithmetic operations
SIMD.%FloatType%.abs()
- Returns a new instance with the absolute lane values.
SIMD.%type%.add()
- Returns a new instance with the lane values added (
a + b
). SIMD.%type%.addSaturate()
- Returns a new instance with the lane values added (
a + b
) and saturating behavior on overflow. SIMD.%FloatType%.div()
- Returns a new instance with the lane values divided (
a / b
). SIMD.%type%.mul()
- Returns a new instance with the lane values multiplied (
a * b
). SIMD.%type%.neg()
- Returns a new instance with the negated lane values.
SIMD.%FloatType%.reciprocalApproximation()
- Returns a new instance with an approximation of the reciprocal lane values.
SIMD.%FloatType%.reciprocalSqrtApproximation()
- Returns a new instance with an approximation of the reciprocal square root lane values.
SIMD.%type%.sub()
- Returns a new instance with the lane values subtracted (
a - b
). SIMD.%type%.subSaturate()
- Returns a new instance with the lane values subtracted (
a - b
) and saturating behavior on overflow. SIMD.%FloatType%.sqrt()
- Returns a new instance with the square root of the lane values.
Shuffling and swizzling
SIMD.%type%.shuffle()
- Returns a new instance with the lane values shuffled.
SIMD.%type%.swizzle()
- Returns a new instance with the lane values swizzled.
Min and max values
SIMD.%FloatType%.max()
- Returns a new instance with the maximum of the lane values.
SIMD.%FloatType%.maxNum()
- Returns a new instance with the maximum of the lane values, preferring numbers over
NaN
. SIMD.%FloatType%.min()
- Returns a new instance with the minimum of the lane values.
SIMD.%FloatType%.minNum()
- Returns a new instance with the minimum of the lane values, preferring numbers over
NaN
.
Selections
SIMD.%type%.select()
- Returns a new instance with the lane values being a mix of the lanes depending on the selector mask.
Comparisons
SIMD.%type%.equal()
- Returns a selection mask depending on
a == b
. SIMD.%type%.notEqual()
- Returns a selection mask depending on
a != b
. SIMD.%type%.lessThan()
- Returns a selection mask depending on
a < b
. SIMD.%type%.lessThanOrEqual()
- Returns selection mask depending on
a <= b
. SIMD.%type%.greaterThan()
- Returns a selection mask depending on
a > b
. SIMD.%type%.greaterThanOrEqual()
- Returns a selection mask depending on
a >= b
.
Bitwise logical operations
SIMD.%type%.and()
- Returns a new instance with the logical AND of the lane values (
a & b
). SIMD.%type%.or()
- Returns a new instance with the logical OR of the lane values (
a | b
). SIMD.%type%.xor()
- Returns a new instance with the logical XOR of the lane values (
a ^ b
). SIMD.%type%.not()
- Returns a new instance with the logical NOT of the lane values (
~a
).
Bitwise shift operations
SIMD.%IntegerType%.shiftLeftByScalar()
- Returns a new instance with the lane values shifted left by a given bit count (
a << bits
). SIMD.%IntegerType%.shiftRightByScalar()
- Returns a new instance with the lane values shifted right. Behavior depends on whether the underlying type is signed or unsigned.
Boolean operations
SIMD.%BooleanType%.allTrue()
- Checks if all lanes hold a
true
value. SIMD.%BooleanType%.anyTrue()
- Checks if any of the lanes hold a
true
value.
Data conversions
SIMD.%type%.fromFloat32x4()
- Creates a new SIMD data type with a float conversion from a Float32x4.
SIMD.%type%.fromFloat32x4Bits()
- Creates a new SIMD data type with a bit-wise copy from a Float32x4.
SIMD.%type%.fromFloat64x2Bits()
- Creates a new SIMD data type with a bit-wise copy from a Float64x2.
SIMD.%type%.fromInt32x4()
- Creates a new SIMD data type with an integer conversion from an In32x4.
SIMD.%type%.fromInt32x4Bits()
- Creates a new SIMD data type with a bit-wise copy from an Int32x4.
SIMD.%type%.fromInt16x8Bits()
- Creates a new SIMD data type with a bit-wise copy from an Int16x8.
SIMD.%type%.fromInt8x16Bits()
- Creates a new SIMD data type with a bit-wise copy from an Int8x16.
SIMD.%type%.fromUint32x4()
- Creates a new SIMD data type with an integer conversion from a Uin32x4.
SIMD.%type%.fromUint32x4Bits()
- Creates a new SIMD data type with a bit-wise copy from a Uint32x4.
SIMD.%type%.fromUint16x8Bits()
- Creates a new SIMD data type with a bit-wise copy from a Uint16x8.
SIMD.%type%.fromUint8x16Bits()
- Creates a new SIMD data type with a bit-wise copy from a Uint8x16.
SIMD prototype
The following methods and properties are installed on the SIMD.%type%.prototype
.
SIMD.%type%.prototype.constructor
- Specifies the function that creates a SIMD object's prototype.
SIMD.%type%.prototype.toLocaleString()
- Returns a localized string representing the SIMD type and its elements. Overrides the
Object.prototype.toLocaleString()
method. SIMD.%type%.prototype.toString()
- Returns a string representing the SIMD type and its elements. Overrides the
Object.prototype.toString()
method. SIMD.%type%.prototype.valueOf()
- Returns the primitive value of a SIMD object.
SIMD.%type%.prototype.toSource()
- Returns a string representing the source code of the object. Overrides the
Object.prototype.toSource()
method.
Polyfill
A Polyfill implementation based on typed arrays, is available at the ecmascript_simd GitHub repository.
Specifications
Specification | Status | Comment |
---|---|---|
SIMD The definition of 'SIMD' in that specification. |
Draft | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Float32x4 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Float64x2 |
No support | Nightly build | No support | No support | No support | No support |
SIMD.Int8x16 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Int16x8 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Int32x4 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Uint8x16 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Uint16x8 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Uint32x4 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool8x16 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool16x8 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool32x4 |
No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool64x2 |
No support | Nightly build | No support | No support | No support | No support |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | Edge | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Float32x4 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Float64x2 |
No support | No support | Nightly build | No support | No support | No support | No support |
SIMD.Int8x16 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Int16x8 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Int32x4 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Uint8x16 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Uint16x8 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Uint32x4 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool8x16 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool16x8 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool32x4 |
No support | No support | Nightly build | Nightly build | No support | No support | No support |
SIMD.Bool64x2 |
No support | No support | Nightly build | No support | No support | No support | No support |
Status notes
- "Preview in: Windows Insider Preview Build 10240+ by enabling in about:flags" in Microsoft Edge. Details: Full Edge API
- "Intent to implement" in Blink/Chromium
See also
- Glossary: SIMD
- SIMD types
- Data types and data structures
- JavaScript typed arrays
- SIMD Programming in JavaScript, talk by Peter Jensen, Intel.
- Mandelbrot animation using SIMD, demo by Peter Jensen, Intel.
- The state of SIMD.js performance in Firefox, blog post by Benjamin Bouvier, Mozilla.