-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathusing-simd-vector-types-in-swift.html
300 lines (260 loc) · 15 KB
/
using-simd-vector-types-in-swift.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://use.fontawesome.com/afd448ce82.js"></script>
<!-- Meta Tag -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- SEO -->
<meta name="author" content="Bruno Rocha">
<meta name="keywords" content="Software, Engineering, Blog, Posts, iOS, Xcode, Swift, Articles, Tutorials, OBJ-C, Objective-C, Apple">
<meta name="description" content="SIMD Vector Types is a feature added in Swift 5 that allows you to calculate multiple results with a single instruction. Let's see how that works.">
<meta name="title" content="Using SIMD Vector Types in Swift">
<meta name="url" content="https://swiftrocks.com/using-simd-vector-types-in-swift">
<meta name="image" content="https://swiftrocks.com/images/thumbs/thumb.jpg?4">
<meta name="copyright" content="Bruno Rocha">
<meta name="robots" content="index,follow">
<meta property="og:title" content="Using SIMD Vector Types in Swift"/>
<meta property="og:image" content="https://swiftrocks.com/images/thumbs/thumb.jpg?4"/>
<meta property="og:description" content="SIMD Vector Types is a feature added in Swift 5 that allows you to calculate multiple results with a single instruction. Let's see how that works."/>
<meta property="og:type" content="website"/>
<meta property="og:url" content="https://swiftrocks.com/using-simd-vector-types-in-swift"/>
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://swiftrocks.com/images/thumbs/thumb.jpg?4"/>
<meta name="twitter:image:alt" content="Page Thumbnail"/>
<meta name="twitter:title" content="Using SIMD Vector Types in Swift"/>
<meta name="twitter:description" content="SIMD Vector Types is a feature added in Swift 5 that allows you to calculate multiple results with a single instruction. Let's see how that works."/>
<meta name="twitter:site" content="@rockbruno_"/>
<!-- Favicon -->
<link rel="icon" type="image/png" href="images/favicon/iconsmall2.png" sizes="32x32" />
<link rel="apple-touch-icon" href="images/favicon/iconsmall2.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+3:ital,wght@0,200..900;1,200..900&display=swap" rel="stylesheet">
<link rel="canonical" href="https://swiftrocks.com/using-simd-vector-types-in-swift"/>
<!-- Bootstrap CSS Plugins -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<!-- Prism CSS Stylesheet -->
<link rel="stylesheet" type="text/css" href="css/prism4.css">
<!-- Main CSS Stylesheet -->
<link rel="stylesheet" type="text/css" href="css/style48.css">
<link rel="stylesheet" type="text/css" href="css/sponsor4.css">
<!-- HTML5 shiv and Respond.js support IE8 or Older for HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://swiftrocks.com/using-simd-vector-types-in-swift"
},
"image": [
"https://swiftrocks.com/images/thumbs/thumb.jpg"
],
"datePublished": "2019-09-29T18:00:00+00:00",
"dateModified": "2020-04-12T14:00:00+02:00",
"author": {
"@type": "Person",
"name": "Bruno Rocha"
},
"publisher": {
"@type": "Organization",
"name": "SwiftRocks",
"logo": {
"@type": "ImageObject",
"url": "https://swiftrocks.com/images/thumbs/thumb.jpg"
}
},
"headline": "Using SIMD Vector Types in Swift",
"abstract": "SIMD Vector Types is a feature added in Swift 5 that allows you to calculate multiple results with a single instruction. Let's see how that works."
}
</script>
</head>
<body>
<div id="main">
<!-- Blog Header -->
<!-- Blog Post (Right Sidebar) Start -->
<div class="container">
<div class="col-xs-12">
<div class="page-body">
<div class="row">
<div><a href="https://swiftrocks.com">
<img id="logo" class="logo" alt="SwiftRocks" src="images/bg/logo2light.png">
</a>
<div class="menu-large">
<div class="menu-arrow-right"></div>
<div class="menu-header menu-header-large">
<div class="menu-item">
<a href="blog">blog</a>
</div>
<div class="menu-item">
<a href="about">about</a>
</div>
<div class="menu-item">
<a href="talks">talks</a>
</div>
<div class="menu-item">
<a href="projects">projects</a>
</div>
<div class="menu-item">
<a href="software-engineering-book-recommendations">book recs</a>
</div>
<div class="menu-item">
<a href="games">game recs</a>
</div>
<div class="menu-arrow-right-2"></div>
</div>
</div>
<div class="menu-small">
<div class="menu-arrow-right"></div>
<div class="menu-header menu-header-small-1">
<div class="menu-item">
<a href="blog">blog</a>
</div>
<div class="menu-item">
<a href="about">about</a>
</div>
<div class="menu-item">
<a href="talks">talks</a>
</div>
<div class="menu-item">
<a href="projects">projects</a>
</div>
<div class="menu-arrow-right-2"></div>
</div>
<div class="menu-arrow-right"></div>
<div class="menu-header menu-header-small-2">
<div class="menu-item">
<a href="software-engineering-book-recommendations">book recs</a>
</div>
<div class="menu-item">
<a href="games">game recs</a>
</div>
<div class="menu-arrow-right-2"></div>
</div>
</div>
</div>
<div class="content-page" id="WRITEIT_DYNAMIC_CONTENT">
<!--WRITEIT_POST_NAME=Using SIMD Vector Types in Swift-->
<!--WRITEIT_POST_HTML_NAME=using-simd-vector-types-in-swift-->
<!--WRITEIT_POST_SITEMAP_DATE_LAST_MOD=2020-04-12T14:00:00+02:00-->
<!--WRITEIT_POST_SITEMAP_DATE=2019-09-29T18:00:00+00:00-->
<!--Add here the additional properties that you want each page to possess.-->
<!--These properties can be used to change content in the template page or in the page itself as shown here.-->
<!--Properties must start with 'WRITEIT_POST'.-->
<!--Writeit provides and injects WRITEIT_POST_NAME and WRITEIT_POST_HTML_NAME by default.-->
<!--WRITEIT_POST_SHORT_DESCRIPTION=SIMD Vector Types is a feature added in Swift 5 that allows you to calculate multiple results with a single instruction. Let's see how that works.-->
<title>Using SIMD Vector Types in Swift</title>
<div class="blog-post">
<div class="post-title-index">
<h1>Using SIMD Vector Types in Swift</h1>
</div>
<div class="post-info">
<div class="post-info-text">
Published on 30 Sep 2019
</div>
</div>
<p><b>SIMD Vector Types</b> is a feature added in Swift 5 that exposes Apple's <code><simd/simd.h></code> module to Swift, allowing you to calculate multiple results with a single instruction.</p>
<h2>What's SIMD?</h2>
<div class="sponsor-article-ad-auto hidden"></div>
<p>SIMD stands for <b>Single Instruction, Multiple Data</b>, a hardware instruction that can be used once to perform multiple operations <b>at once.</b> 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.</p>
<p>Let's assume that we are handling the position of a character in a game that currently sits in the vector <code>x: 2 | y: 4</code>, and an explosion needs to apply some knockback to it.</p>
<p>If we assume that the knockback multiplies the character's position by two, we'll need to do two operations to end up with <code>x: 4| y: 8</code>:</p>
<pre><code>(64bit integers)
0000000000000000000000000000000000000000000000000000000000000010
times
0000000000000000000000000000000000000000000000000000000000000010
-- and
0000000000000000000000000000000000000000000000000000000000000100
times
0000000000000000000000000000000000000000000000000000000000000010</code></pre>
<p>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 <code>SIMD2</code> vector to store two different 32 bit values in a single 64 bit storage:</p>
<pre><code>32 bits for (x: 2), and 32 bits for (4)
00000000000000000000000000000010 | 00000000000000000000000000000100</code></pre>
<p>If we treat the knockback's power as another <code>SIMD2</code>, we can multiply the vectors together to get the character's new position in one go:</p>
<pre><code>00000000000000000000000000000010 | 00000000000000000000000000000100
times
00000000000000000000000000000010 | 00000000000000000000000000000010</code></pre>
<p>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 <b>new</b> 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.</p>
<p>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.</p>
<h2>SIMD in Swift</h2>
<p>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 <code>SIMD2<Int32></code>:</p>
<pre><code>let character = SIMD2<Int32>(arrayLiteral: 2, 4)
let knockback = SIMD2<Int32>(arrayLiteral: 2, 2)
let result: SIMD2<Int32> = character &* knockback // 4,8</code></pre>
<p>Vectors can be operated on through masked arithmetic operators, and individual values of the vectors can be accessed with the <code>x,y,z,w</code> properties, depending on how big the lanes are. If the SIMD has more than four lanes, you can use the <code>lowHalf</code> and <code>highHalf</code> to access smaller halves of the lanes until you can access individual values.</p>
<pre><code>var character = SIMD2<Int32>()
character.x = 2
character.y = 4
var fourLanes = SIMD4<Int16>()
fourLanes.x = 2
fourLanes.y = 4
fourLanes.z = 8
fourLanes.w = 16
var eightLanes = SIMD8<Int8>()
eightLanes.lowHalf.x = 1
eightLanes.highHalf.w = 1
character &+ character // 4, 8
character &- character // 0, 0
character &* character // 4, 16
character / character // 1, 1</code></pre>
<p>(Note that because only masked operators are supported, Swift will not protect you from overflowing values when using SIMD vectors.)</p>
<p>Besides arithmetic operators, SIMD types also supports comparison operators:</p>
<pre><code>character == knockback // false</code></pre>
<p>In addition to comparing the entire vector itself, SIMD types in Swift also support <b>pointwise operators</b> 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 <code>.</code> to the operator to get the pointwise version of it. For equality for example, we can use the <code>.==</code> pointwise operator to get a vector of booleans that indicates the result of each individual equality.</p>
<pre><code>character .== knockback // SIMDMask<SIMD2<Int32>>(false, true)</code></pre>
<h2>Conclusion</h2>
<div class="sponsor-article-ad-auto hidden"></div>
<p>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.</p>
<p>Follow me on my Twitter (<a href="https://twitter.com/rockbruno_">@rockbruno_</a>), and let me know of any suggestions and corrections you want to share.</p>
<h2>References and Good reads</h2>
<a href="https://github.com/apple/swift">The Swift Source Code</a>
<br>
<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0229-simd.md">SIMD Proposal</a>
<br>
<a href="https://www.coursera.org/lecture/comparch/simd-MtZ5W">SIMD Lecture from Princeton</a>
<br>
</div></div>
<div class="blog-post footer-main">
<div class="footer-logos">
<a href="https://swiftrocks.com/rss.xml"><i class="fa fa-rss"></i></a>
<a href="https://twitter.com/rockbruno_"><i class="fa fa-twitter"></i></a>
<a href="https://github.com/rockbruno"><i class="fa fa-github"></i></a>
</div>
<div class="footer-text">
© 2025 Bruno Rocha
</div>
<div class="footer-text">
<p><a href="https://swiftrocks.com">Home</a> / <a href="blog">See all posts</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Blog Post (Right Sidebar) End -->
</div>
</div>
</div>
<!-- All Javascript Plugins -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script type="text/javascript" src="js/prism4.js"></script>
<!-- Main Javascript File -->
<script type="text/javascript" src="js/scripts30.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-H8KZTWSQ1R"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-H8KZTWSQ1R');
</script>
</body>
</html>