Image for post
Image for post

Using SIMD Vector Types in Swift

Bruno Rocha
Sep 29, 2019 · 4 min read

SIMD Vector Types is a feature added in Swift 5 that exposes Apple’s <simd/simd.h> module to Swift, allowing you to calculate multiple results with a single instruction.

What’s SIMD?

SIMD stands for Single Instruction, Multiple Data, a hardware instruction that can be used once to perform multiple operations at once. There’s nothing you can do with SIMD that you can’t do without it, but in the world of graphical applications where handling multiple values in the shape of vectors and matrices is important, usage of SIMD types can provide big performance boosts over calculating values normally.

Let’s assume that we are handling the position of a character in a game that currently sits in the vector x: 2 | y: 4, and an explosion needs to apply some knockback to it.

If we assume that the knockback multiplies the character’s position by two, we’ll need to do two operations to end up with x: 4| y: 8:

While there’s nothing particular wrong about doing two operations to achieve this, we can do better: a SIMD vector allows us to store multiple pieces of data in a single register by splitting our 64 bits of memory into smaller sections. In the case of our character, we can use a SIMD2 vector to store two different 32 bit values in a single 64 bit storage:

If we treat the knockback’s power as another SIMD2, we can multiply the vectors together to get the character's new position in one go:

This allows us to perform a single hardware instruction that will end up containing the result of multiple operators — in this case, a single instruction will allow us to get a new vector that contains the new values for both the x and y axes. Data-parallel problems like this have shown to gain between 2–10x better performance when using SIMD types, making them an invaluable tool when building high performance algorithms in the graphical applications world.

For example, a more common problem that benefits from SIMD is changing the brightness of an image — instead of individually bumping the R, G and B channels of each pixel for each of the millions of pixels in a modern screen, we can treat the pixels’ channels as a SIMD3 vector to reduce the number of instructions by around 3x.

SIMD in Swift

Starting from Swift 5, SIMD Vector types that range from 2 to 64 lanes are available for use. This is how the previous character example can be written in Swift using SIMD2<Int32>:

Vectors can be operated on through masked arithmetic operators, and individual values of the vectors can be accessed with the x,y,z,w properties, depending on how big the lanes are. If the SIMD has more than four lanes, you can use the lowHalf and highHalf to access smaller halves of the lanes until you can access individual values.

(Note that because only masked operators are supported, Swift will not protect you from overflowing values when using SIMD vectors.)

Besides arithmetic operators, SIMD types also supports comparison operators:

In addition to comparing the entire vector itself, SIMD types in Swift also support pointwise operators for comparisons. If instead of comparing everything we’re looking to get the individual result for each comparison in the vector, we can prefix a dot . to the operator to get the pointwise version of it. For equality for example, we can use the .== pointwise operator to get a vector of booleans that indicates the result of each individual equality.

Conclusion

Although the monstrous performance benefits can be only seen in the world of graphical applications, many problems can benefit from SIMD by being vectorized. Even when working with consumer applications, being careful about memory and CPU usage can bring big benefits to the user experience while teaching you more about software engineering.

Follow me on my Twitter (@rockthebruno), and let me know of any suggestions and corrections you want to share.

References and Good reads

The Swift Source Code
SIMD Proposal
SIMD Lecture from Princeton

Originally published at https://swiftrocks.com.

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Bruno Rocha

Written by

iOS Developer at Spotify | Follow me on Twitter at @rockbruno_ for more iOS tricks! Writer of https://swiftrocks.com

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Bruno Rocha

Written by

iOS Developer at Spotify | Follow me on Twitter at @rockbruno_ for more iOS tricks! Writer of https://swiftrocks.com

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store