# SIMD - Scope of Implementation?

**URL:** <https://forums.swift.org/t/simd-scope-of-implementation/29896>\
**Category:** Using Swift\
**Tags:** simd\
**Created:** [October 17, 2019, 2:29pm UTC](https://forums.swift.org/t/simd-scope-of-implementation/29896 "2019-10-17T14:29:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [October 17, 2019, 2:29pm UTC](https://forums.swift.org/t/simd-scope-of-implementation/29896/1 "2019-10-17T14:29:18Z")

</div>

In the SIMD implementation, is there an equivalent to \_mm256\_movemask\_epi8()?

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 17, 2019, 3:06pm UTC](https://forums.swift.org/t/simd-scope-of-implementation/29896/2 "2019-10-17T15:06:58Z")

</div>

You can use it directly via the `_Builtin_intrinsics.intel` 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:

```swift
import _Builtin_intrinsics.intel

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

```

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

```swift
let v = SIMD32<Int8>.random(in: .min ... .max)
print(v)
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):

```swift
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)
100101011010010011010100011

```

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:

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

```

---

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [October 18, 2019, 6:27am UTC](https://forums.swift.org/t/simd-scope-of-implementation/29896/3 "2019-10-18T06:27:32Z")

</div>

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

Also, thank you for your work on [SE-0229](https://github.com/apple/swift-evolution/blob/master/proposals/0229-simd.md). Great stuff!

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

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 18, 2019, 1:01pm UTC](https://forums.swift.org/t/simd-scope-of-implementation/29896/4 "2019-10-18T13:01:45Z")

</div>

> [@mattrips](#):
>
> 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.
