SIMD Vector Operations in Swift 2

Hi, my first post here.


In the video "What's new in Swift" at https://developer.apple.com/videos/wwdc/2015/?id=106, one of the points listed on the slide of new features (but not discussed) was SIMD vector operations.


I looked in the pre-release "The Swift Language" iBook and couldn't find mention of SIMD, and anything concerning Vectors appeared to be to do with Advanced Operators (as in the previous versions of "The Swift Language"). Does anyone have any information about what was meant? Is the Accelerate framework being made obsolete? Will we even have to worry about this, or is it an internal compiler optimisation?


Thanks,

Geordie

The Xcode 7 beta release notes say:


SIMD Support:Clang extended vectors are imported and usable in Swift, enabling many graphics and other low-level numeric APIs (e.g. simd.h) to be usable in Swift.



You can now "import simd" to get access to the same functionality that C/C++/ObjC programs get via #include <simd/simd.h>


The Swift syntax seems to be similar to the C++ syntax, except that some functions and initializers have named arguments:


import simd
let vec1 = float4(1.0, 2.0, 3.0, 4.0)
let length1 = length(vec1)
let vec2 = float4(1.0, 1.0, -1.0, -1.0)
let dotProduct = dot(vec1, vec2)
let elementwiseMultiplication = vec1*vec2
let matrix1 = float4x4([[0,1,1,1], [-1,2,0,3], [-3,0,-4,5],[-1,-1,2,2]])
let matrix2 = float4x4(diagonal:[1,2,3,4])
let matrix3 = matrix1 + matrix2
let matrix4 = matrix3.transpose



I haven't found any Swift documentation yet (aside from option-clicking a symbol to get the popup description or command-clicking to get Xcode's pseudo-header thing), but through trial and error (and Xcode's autocompletion) it's pretty easy to figure out how the C++ version maps into Swift.



Also, it looks like it's not done yet. while simd.vector and simd.matrix work fine, simd.logic doesn't seem to have been fully converted to Swift yet. (I didn't check all the other ones.) And not all the functions in simd.vector and simd.matrix have descriptions yet, either.

I get two compile time errors when trying to import simd:

  1. "__swift_FORCE_LOAD_$_swiftDarwin", referenced from:
  2. clang: error: linker command failed with exit code 1 (use -v to see invocation)


Undefined symbols for architecture x86_64:

"__swift_FORCE_LOAD_$_swiftDarwin", referenced from:

__swift_FORCE_LOAD_$_swiftDarwin_$_simd in libswiftsimd.a(simd.o)

(maybe you meant: __swift_FORCE_LOAD_$_swiftDarwin_$_simd)

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)


Xcode 7, OS X 10.10.3

It looks like you need to "import Accelerate" in addition to "import simd" in order to get it to link against the right libraries.


I'd only tried it in a Playground before, where "import simd" by itself was sufficient.

Ah, thanks, also it seems to be enough to import Darwin (in addition to simd) to make it work.

SIMD Vector Operations in Swift 2
 
 
Q