# Usage of CPU intrinsics in SPM projects

**URL:** <https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887>\
**Category:** Using Swift\
**Tags:** packagemanager\
**Created:** [October 3, 2020, 9:27pm UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887 "2020-10-03T21:27:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbartelmess](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tbartelmess/32/12974_2.png) [@tbartelmess](https://forums.swift.org/u/tbartelmess)\
**Post date:** [October 3, 2020, 9:27pm UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887/1 "2020-10-03T21:27:17Z")

</div>

Hi everyone 👋

I have a [Swift Package](https://github.com/tbartelmess/swift-crc32c) that, when available, is using Intel SSE 4.2 instructions to accelerate the calculation of [CRC32C](https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm) checksums.

The parts for this are implemented in C using the Intel intrinsics in Clang.

Currently, clang requires the `-msse4.2` flag to be passed as a compile option.

Is there a way in Swift Package Manager to pass the `-msse4.2` flag as a compile option that doesn't involve `unsafeFlags`?

If not any suggestions other than restoring to inline assembly to use architecture-specific instructions.

Would it be worth considering to add support for intrinsics for ARM and Intel as "safe" compile options in Swift Package manager?

---

<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 4, 2020, 12:08am UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887/2 "2020-10-04T00:08:00Z")

</div>

Instead of passing `-msse4.2` (which is really too coarse-grained in general), put ` __attribute__ ((target("sse4.2")))` on your `intel_crc` function.

We still need a good solution for this problem in _Swift_ code, but for C code target attributes are really a better option anyway and don't require an unsafe flag.

---

<div class="post-metadata">

**Author:** ![tbartelmess](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tbartelmess/32/12974_2.png) [@tbartelmess](https://forums.swift.org/u/tbartelmess)\
**Post date:** [October 4, 2020, 12:13am UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887/3 "2020-10-04T00:13:44Z")

</div>

@scanon Thank you, I didn't consider annotating the function, which in hindsight is very obvious 😳

> [@scanon](#):
>
> We still need a good solution for this problem in _Swift_ code, but for C code target attributes are really a better option anyway and don't require an unsafe flag.

I think it's not currently possible to use Intel Intrinsic functions from Swift directly (maybe I am wrong).

A few weeks ago I've tried using `_mm_crc32_u8` directly from Swift and it resulted in a compiler crash. (See [Compiler crash when using intel intrinsics](https://forums.swift.org/t/compiler-crash-when-using-intel-intrinsics/39774)).

Thanks a lot,  
Thomas

---

<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 4, 2020, 12:28am UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887/4 "2020-10-04T00:28:05Z")

</div>

It _is_ possible, but the frontend isn't well set-up to deal with it yet, so it'll crash instead of issuing a diagnostic that you need to pass `-msse4.2` (and passing that flag is a pain in Swift):

```swift
-Xcc -Xclang -Xcc -target-feature -Xcc -Xclang -Xcc +sse4.2

```

Needless to say, this is not a good option. What you have is good for now, and we should provide something like function attributes in Swift in the future.

---

<div class="post-metadata">

**Author:** ![tbartelmess](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tbartelmess/32/12974_2.png) [@tbartelmess](https://forums.swift.org/u/tbartelmess)\
**Post date:** [October 4, 2020, 12:31am UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887/5 "2020-10-04T00:31:11Z")

</div>

@scanon Good to know! I wasn't aware of that it needs to be passed as a `+sse4.2` to the compiler 😄.

Thank you.

---

<div class="post-metadata">

**Author:** ![RandomHashTags](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/randomhashtags/32/24259_2.png) [@RandomHashTags](https://forums.swift.org/u/RandomHashTags)\
**Post date:** [December 17, 2024, 2:18am UTC](https://forums.swift.org/t/usage-of-cpu-intrinsics-in-spm-projects/40887/6 "2024-12-17T02:18:59Z")

</div>

Has there been any progress on this? I find the native `SIMD` value types very useful, but lacking some important operations and key functionality for high-performance real-world usage.

I am exploring the idea of creating a library that enables this behavior, expanding SIMD (see [`StackStrings`](https://github.com/RandomHashTags/destiny/blob/73511bc606ed42aefb5c32d267f21ed5898e0184/Sources/DestinyUtilities/StackStrings.swift)), and offering more targeted bit manipulation. It would be very useful if there already was a way to use intrinsics in Swift natively today without doing a bunch of hoopla.
