# Generic SIMD function produces non-SIMD code, unless you explicitly constraint it

**URL:** <https://forums.swift.org/t/generic-simd-function-produces-non-simd-code-unless-you-explicitly-constraint-it/75796>\
**Category:** Compiler\
**Created:** [November 3, 2024, 6:22pm UTC](https://forums.swift.org/t/generic-simd-function-produces-non-simd-code-unless-you-explicitly-constraint-it/75796 "2024-11-03T18:22:21Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [November 4, 2024, 12:43am UTC](https://forums.swift.org/t/generic-simd-function-produces-non-simd-code-unless-you-explicitly-constraint-it/75796/9 "2024-11-04T00:43:49Z")

</div>

A while back I asked [how to do something similar](https://forums.swift.org/t/can-simd-types-compute-the-high-part-of-a-product/55740), and @scanon recommended writing a `widen` function like this:

```swift
private func widen(_ x: SIMD8<UInt8>) -> SIMD8<UInt16> {
    SIMD8<UInt16>(truncatingIfNeeded: x)
}

```

Then you can interleave vectors like this:

```swift
(widen(a) &<< 8) | widen(b)

```

I believe this also produces efficient machine instructions.

---

_[View the full topic](https://forums.swift.org/t/generic-simd-function-produces-non-simd-code-unless-you-explicitly-constraint-it/75796)._
