SIMD - Scope of Implementation?

In the SIMD implementation, is there an equivalent to _mm256_movemask_epi8()?

You can use it directly via the module (but note that as an underscored module, this is not formally source stable, though in practice it is unlikely to change). Because Intel has funky types (or lack thereof) for their intrinsics, it's helpful to write a little bit of wrapper code to make it more pleasant to work with:


func movemask(_ v: SIMD32<Int8>) -> UInt32 {
    _mm256_movemask_epi8(unsafeBitCast(v, to: __m256i.self))

With that wrapper in place, we can use it easily:

let v = SIMD32<Int8>.random(in: .min ... .max)
print(String(movemask(v), radix: 2))

Compile with avx2 enabled (this is ugly, sorry. Better support for enabling target CPU features is something we need to work on):

Sandbox % swiftc jawn.swift -O -Xcc -Xclang -Xcc -target-feature -Xcc -Xclang -Xcc +avx2
Sandbox % ./jawn
SIMD32<Int8>(-18, -95, 57, 112, 70, -39, 55, -33, 16, -57, -118, 42, 106, -101, 93, 28, -68, 72, -102, -18, 36, -46, 96, -6, 87, 123, -26, 117, 68, 46, 76, 122)

Note that for the most common simple uses of movemask (is any element set / clear, etc) you can also use the any / all functions and avoid all of this:

let v = SIMD32<Int8>.random(in: .min ... .max)
if any(v .< 0) { print("v contains a negative number.") }

Thank you @scanon for such a quick, thorough and informative answer.

Also, thank you for your work on SE-0229. Great stuff!

Alas, the documentation for the simd framework is lacking. How could someone, like me, be helpful on that front?

The simd module on Apple platforms (i.e. what you get when you #include <simd/simd.h> or import simd) is a part of Apple's SDK (not part of the Swift project), so for improvements there the answer is "report a bug".

The standard library types and operations (SE-0229 and SE-0251) are part of the Swift project, so you can just open a PR with documentation improvements for those (please tag me and @nnnnnnnn as reviewers). Feel free to open incomplete PRs and we can discuss the improvements in the PR itself.

That said, I was the original author of <simd/simd.h> and now I work on Swift, so there's a certain amount of overlap here, and if you message me any documentation bugs you file I can help make sure they get to the right people.

Terms of Service

Privacy Policy

Cookie Policy