CPUs often support specialized vector types and vector operations (a.k.a.media instructions). Vector types are a fixed array of floating or integer types, and vector operations operate simultaneously on them.
SpecializedVector types provide access to them.
TheVectorBaseType must be aStatic Array. TheVectorElementType is the unqualified element type of the static array. The dimension of the static array is the number of elements in the vector.
Vector types and operations are introduced by importingcore.simd:
import core.simd;The types defined will all follow the naming convention:
typeNNwheretype is the vector element type andNN is the number of those elements in the vector type. The type names will not be keywords.
Vector types have the property:
| Property | Description |
|---|---|
| .array | Returns static array representation |
Vectors support the following properties based on the vector element type. The value produced is that of a vector of the same type with each element set to the value corresponding to the property value for the element type.
| Property | Description |
|---|---|
| .min | minimum value |
| .max | maximum value |
| Property | Description |
|---|---|
| .epsilon | smallest increment to the value 1 |
| .infinity | infinity value |
| .max | largest representable value that is not infinity |
| .min_normal | smallest representable value that is not 0 |
| .nan | NaN value |
Vector types of the same size (number_of_elements * size_of_element) can be implicitly converted among each other, this is done as a reinterpret cast (a type paint). Vector types can be cast to theirVectorBaseType.
Integers and floating point values can be implicitly converted to their vector equivalents:
int4 v = 7;v = 3 + v;// add 3 to each element in vThey cannot be accessed directly, but can be when converted to an array type:
int4 v;(cast(int*)&v)[3] = 2;// set 3rd element of the 4 int vector(cast(int[4])v)[3] = 2;// set 3rd element of the 4 int vectorv.array[3] = 2;// set 3rd element of the 4 int vectorv.ptr[3] = 2;// set 3rd element of the 4 int vector
If vector extensions are implemented, theversion identifierD_SIMD is set.
Whether a type exists or not can be tested at compile time with anIsExpression:
staticif (is(typeNN)) ... yes, itis supported ...else ... nope, use workaround ...
Whether a particular operation on a type is supported can be tested at compile time with:
float4 a,b;staticif (__traits(compiles, a+b)) ... yes, addis supportedfor float4 ...else ... nope, use workaround ...
For runtime testing to see if certain vector instructions are available, see the functions incore.cpuid.
A typical workaround for unsupported vector operations would be to use array operations instead:
float4 a,b;staticif (__traits(compiles, a/b)) c = a / b;else c[] = a[] / b[];
The following describes the specific implementation of the vector types for the X86 and X86_64 architectures.
The vector extensions are currently implemented for the OS X 32 bit target, and all 64 bit targets.
core.simd defines the following types:
| Type Name | Description | gcc Equivalent |
|---|---|---|
| void16 | 16 bytes of untyped data | no equivalent |
| byte16 | 16bytes | signed char __attribute__((vector_size(16))) |
| ubyte16 | 16ubytes | unsigned char __attribute__((vector_size(16))) |
| short8 | 8shorts | short __attribute__((vector_size(16))) |
| ushort8 | 8ushorts | ushort __attribute__((vector_size(16))) |
| int4 | 4ints | int __attribute__((vector_size(16))) |
| uint4 | 4uints | unsigned __attribute__((vector_size(16))) |
| long2 | 2longs | long __attribute__((vector_size(16))) |
| ulong2 | 2ulongs | unsigned long __attribute__((vector_size(16))) |
| float4 | 4floats | float __attribute__((vector_size(16))) |
| double2 | 2doubles | double __attribute__((vector_size(16))) |
| void32 | 32 bytes of untyped data | no equivalent |
| byte32 | 32bytes | signed char __attribute__((vector_size(32))) |
| ubyte32 | 32ubytes | unsigned char __attribute__((vector_size(32))) |
| short16 | 16shorts | short __attribute__((vector_size(32))) |
| ushort16 | 16ushorts | ushort __attribute__((vector_size(32))) |
| int8 | 8ints | int __attribute__((vector_size(32))) |
| uint8 | 8uints | unsigned __attribute__((vector_size(32))) |
| long4 | 4longs | long __attribute__((vector_size(32))) |
| ulong4 | 4ulongs | unsigned long __attribute__((vector_size(32))) |
| float8 | 8floats | float __attribute__((vector_size(32))) |
| double4 | 4doubles | double __attribute__((vector_size(32))) |
| Operator | void16 | byte16 | ubyte16 | short8 | ushort8 | int4 | uint4 | long2 | ulong2 | float4 | double2 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| = | × | × | × | × | × | × | × | × | × | × | × |
| + | – | × | × | × | × | × | × | × | × | × | × |
| - | – | × | × | × | × | × | × | × | × | × | × |
| * | – | – | – | × | × | – | – | – | – | × | × |
| / | – | – | – | – | – | – | – | – | – | × | × |
| & | – | × | × | × | × | × | × | × | × | – | – |
| | | – | × | × | × | × | × | × | × | × | – | – |
| ^ | – | × | × | × | × | × | × | × | × | – | – |
| += | – | × | × | × | × | × | × | × | × | × | × |
| -= | – | × | × | × | × | × | × | × | × | × | × |
| *= | – | – | – | × | × | – | – | – | – | × | × |
| /= | – | – | – | – | – | – | – | – | – | × | × |
| &= | – | × | × | × | × | × | × | × | × | – | – |
| |= | – | × | × | × | × | × | × | × | × | – | – |
| ^= | – | × | × | × | × | × | × | × | × | – | – |
| == | – | × | × | × | × | × | × | × | × | × | × |
| != | – | × | × | × | × | × | × | × | × | × | × |
| < | – | × | × | × | × | × | × | × | × | × | × |
| <= | – | × | × | × | × | × | × | × | × | × | × |
| >= | – | × | × | × | × | × | × | × | × | × | × |
| > | – | × | × | × | × | × | × | × | × | × | × |
| unary~ | – | × | × | × | × | × | × | × | × | – | – |
| unary+ | – | × | × | × | × | × | × | × | × | × | × |
| unary- | – | × | × | × | × | × | × | × | × | × | × |
| Operator | void32 | byte32 | ubyte32 | short16 | ushort16 | int8 | uint8 | long4 | ulong4 | float8 | double4 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| = | × | × | × | × | × | × | × | × | × | × | × |
| + | – | × | × | × | × | × | × | × | × | × | × |
| - | – | × | × | × | × | × | × | × | × | × | × |
| * | – | – | – | – | – | – | – | – | – | × | × |
| / | – | – | – | – | – | – | – | – | – | × | × |
| & | – | × | × | × | × | × | × | × | × | – | – |
| | | – | × | × | × | × | × | × | × | × | – | – |
| ^ | – | × | × | × | × | × | × | × | × | – | – |
| += | – | × | × | × | × | × | × | × | × | × | × |
| -= | – | × | × | × | × | × | × | × | × | × | × |
| *= | – | – | – | – | – | – | – | – | – | × | × |
| /= | – | – | – | – | – | – | – | – | – | × | × |
| &= | – | × | × | × | × | × | × | × | × | – | – |
| |= | – | × | × | × | × | × | × | × | × | – | – |
| ^= | – | × | × | × | × | × | × | × | × | – | – |
| == | – | × | × | × | × | × | × | × | × | × | × |
| != | – | × | × | × | × | × | × | × | × | × | × |
| < | – | × | × | × | × | × | × | × | × | × | × |
| <= | – | × | × | × | × | × | × | × | × | × | × |
| >= | – | × | × | × | × | × | × | × | × | × | × |
| > | – | × | × | × | × | × | × | × | × | × | × |
| unary~ | – | × | × | × | × | × | × | × | × | – | – |
| unary+ | – | × | × | × | × | × | × | × | × | × | × |
| unary- | – | × | × | × | × | × | × | × | × | × | × |
Operators not listed are not supported at all.
Seecore.simd for the supported intrinsics.