Language mapping for JavaScript

April 25, 2016 · View on GitHub

SIMD.js defines primitive SIMD types with a [[SIMDElements]] field containing one of the types from the portable SIMD specification. The corresponding SIMD type descriptors have an [[Interpretation]] field referencing one of the SIMD interpretations in this spec.

Constructor[[SIMDElements]] type[[Interpretation]]
SIMD.Float32x4v128f32x4
SIMD.Int32x4v128s32x4
SIMD.Int16x8v128s16x8
SIMD.Int8x16v128s8x16
SIMD.Uint32x4v128u32x4
SIMD.Uint16x8v128u16x8
SIMD.Uint8x16v128u8x16
SIMD.Bool32x4b32x4b32x4
SIMD.Bool16x8b16x4b16x8
SIMD.Bool8x16b8x16b8x16

SIMD.js does not currently provide mappings for any of the 64x2 types.

Abstract operations

SIMDCreate(descriptor, elements)

  1. Return the SIMD value specified by the record { [[SIMDTypeDescriptor]]: descriptor, [[SIMDElements]]: elements }.

SIMDUnaryOp(a, op, descriptor)

  1. If a.[[SIMDTypeDescriptor]] is not descriptor, throw a TypeError exception.
  2. Let res be descriptor.[[Interpretation]].op(a.[[SIMDElements]]).
  3. Return SIMDCreate(descriptor, res).

SIMDBinaryOp(a, b, op, descriptor, outputDescriptor)

  1. If a.[[SIMDTypeDescriptor]] is not descriptor or b.[[SIMDTypeDescriptor]] is not descriptor, throw a TypeError exception.
  2. If outputDescriptor is not provided, let outputDescriptor be descriptor.
  3. Let res be descriptor.[[Interpretation]].op(a.[[SIMDElements]], b.[[SIMDElements]]).
  4. Return SIMDCreate(outputDescriptor, res).

SIMDRelationalOp(a, b, op, descriptor)

  1. Let outputDescriptor be SIMDBoolType(descriptor).
  2. Return SIMDBinaryOp(a, b, op, descriptor, outputDescriptor).

SIMDScalarOp(a, bits, op, descriptor)

  1. If a.[[SIMDTypeDescriptor]] is not descriptor, throw a TypeError exception.
  2. Let scalar be ToUint8(bits).
  3. Let res be descriptor.[[Interpretation]].op(a.[[SIMDElements]], scalar).
  4. Return SIMDCreate(descriptor, res).

Properties of the SIMDConstructor constructors

Unary operations

The unary operations listed in the table below are defined as properties «SIMD»Constructor.«Operation»(a) where «SIMD» is one of the SIMD types and «Operation» is the name of the operation.

  1. Let result be SIMDUnaryOp(a, «Operation», «SIMD»Descriptor).
  2. ReturnIfAbrupt(result).
  3. Return result.
«Operation»SIMD types supported
neginteger and floating-point
sqrtfloating-point
reciprocalSqrtApproximationfloating-point
reciprocalApproximationfloating-point
absfloating-point
notinteger and boolean

Binary operations

The unary operations listed in the table below are defined as properties «SIMD»Constructor.«Operation»(a, b) where «SIMD» is one of the SIMD types and «Operation» is the name of the operation.

  1. Let result be SIMDBinaryOp(a, b, «Operation», «SIMD»Descriptor).
  2. ReturnIfAbrupt(result).
  3. Return result.
«Operation»SIMD types supported
addinteger and floating-point
subinteger and floating-point
mulinteger and floating-point
divfloating-point
maxfloating-point
minfloating-point
maxNumfloating-point
minNumfloating-point
andinteger and boolean
xorinteger and boolean
orinteger and boolean
addSaturate8x16 and 16x8 integers
subSaturate8x16 and 16x8 integers

Relational operations

The relational operations listed in the table below are defined as properties «SIMD»Constructor.«Operation»(a, b) where «SIMD» is one of the SIMD types and «Operation» is the name of the operation.

  1. Let result be SIMDRelationalOp(a, b, «Operation», «SIMD»Descriptor).
  2. ReturnIfAbrupt(result).
  3. Return result.
«Operation»SIMD types supported
lessThaninteger and floating-point
lessThanOrEqualinteger and floating-point
greaterThaninteger and floating-point
greaterThanOrEqualinteger and floating-point
equalinteger and floating-point
notEqualinteger and floating-point

«SIMD»Constructor.from«TIMD»Bits(a)

In this definition, «TIMD» is not «SIMD», and both «TIMD» and «SIMD» range over all integer and floating-point SIMD types. These types all have a v128 [[SIMDElements]] field which is simply reinterpreted as the new type.

  1. If a.[[SIMDTypeDescriptor]] is not «TIMD»Descriptor, throw a TypeError exception.
  2. Let res be a.[[SIMDElements]].
  3. Return SIMDCreate(«SIMD»Descriptor, res).

«SIMD»Constructor.from«TIMD»(a)

Conversion between floating-point and integer lanes is provided for the 32x4 integer types:

«SIMD»«TIMD»«Operation»Fallible
Float32x4Int32x4f32x4.fromSignedIntNo
Float32x4Uint32x4f32x4.fromUnsignedIntNo
Int32x4Float32x4s32x4.fromFloatYes
Uint32x4Float32x4u32x4.fromFloatYes

The integer-to-float conversions always succeed, but the float-to-integer conversions can fail:

  1. If a.[[SIMDTypeDescriptor]] is not «TIMD»Descriptor, throw a TypeError exception.
  2. If conversion is fallible:
    1. Let (res, fail) be «Operation»(a.[[SIMDElements]]).
  3. Otherwise:
    1. Let res be «Operation»(a.[[SIMDElements]]).
    2. Let fail be false.
  4. If fail is true, throw a RangeError exception.
  5. Return SIMDCreate(«SIMD»Descriptor, res).

«SIMD»Constructor.anyTrue(a)

This operation is only defined on boolean SIMD types.

  1. If a.[[SIMDTypeDescriptor]] is not «SIMD»Descriptor, throw a TypeError exception.
  2. Return «SIMD»Descriptor.[[Interpretation]].anyTrue(a.[[SIMDElements]]).

«SIMD»Constructor.shiftLeftByScalar(a, bits)

This operation is only defined on integer SIMD types.

  1. Let result be SIMDScalarOp(a, bits, shiftLeftByScalar, «SIMD»Descriptor).
  2. ReturnIfAbrupt(result).
  3. Return result.

«SIMD»Constructor.shuffle(a, b, ...lanes)

This operation exists only on integer and floating point SIMD types.

  1. If a.[[SIMDTypeDescriptor]] is not «SIMD»Descriptor, or if b.[[SIMDTypeDescriptor]] is not «SIMD»Descriptor, throw a TypeError exception.
  2. Let indices be a new List.
    1. For integer n from 0 to «SIMD»Descriptor.[[VectorLength]],
      1. Let lane be lanes[i], or 0 if lanes is not long enough.
      2. Let index be SIMDToLane(«SIMD»Descriptor.[[VectorLength]] * 2, lane).
      3. ReturnIfAbrupt(index).
      4. Append index to the end of indices.
  3. Let res be «SIMD»Descriptor.[[Interpretation]].shuffle(a, b, indices).
  4. Return SIMDCreate(«SIMD»Descriptor, res).