Home

Awesome

Swift SIMD Type Cheat Sheet

In Swift, you can handle vectors, matrices, and quaternions, ​​using the standard library SIMD vector types and the Accelerate simd library. They are related as shown in the figure below.

This cheat sheet shows an overview and sample code for each type.

Image

SIMD Vector Types

The SIMD Vector Types were added in Swift 5 Standard Library. They consist of SIMD2<Scalar>, SIMD3<Scalar>, SIMD4<Scalar>, SIMD8<Scalar>, SIMD16<Scalar>, SIMD32<Scalar>, and SIMD64<Scalar>. The Scalar is a type conforming to the SIMDScalar protocol, such as Double, Float, Float16, Int, Int8, Int16, Int32, Int64, UInt, UInt8, UInt16, UInt32, and UInt64. The SIMD protocol defines properties, methods, and operators for SIMD vectors.

The cheat sheet shows the definition of each type and protocol, as well as sample code.

Sample Code:

References:

Image

Accelerate simd library - Vectors

It provides Signed Integer Vectors, Unsigned Integer Vectors, and Floating-Point Vectors. Since simd types are typealias of Standard Library SIMD types, you can take advantage of SIMD type functions.

The cheat sheet shows the types and functions which can be used on the types, as well as sample code.

Sample Code:

References:

Image Image

Accelerate simd Library - Matrices

It provides Single-Precision Floating-Point Matrices and Double-Precision Floating-Point Matrices. The Matrices are up to 4 columns x 4 rows. (column major naming convention)

The cheat sheet shows the types and functions which can be used on the types, as well as sample code.

Sample Code:

References:

Image Image

Accelerate simd Library - Quaternions

It provides Single-Precision Quaternion Type simd_quatf and Double-Precision Quaternion Type simd_quatd.

Quaternions

Sample Code:

References:

Image Image

License: CC0