# Improved SIMD in Rust

**URL:** <https://users.rust-lang.org/t/improved-simd-in-rust/2604>\
**Category:** uncategorized\
**Created:** [August 24, 2015, 5:39pm UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604 "2015-08-24T17:39:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![huon](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/huon/32/19_2.png) [@huon](https://users.rust-lang.org/u/huon)\
**Post date:** [August 24, 2015, 5:39pm UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/1 "2015-08-24T17:39:18Z")

</div>

An overview of my work on SIMD in Rust.

> **[SIMD in Rust](https://huonw.github.io/blog/2015/08/simd-in-rust/)**
>
> An overview of my work on improving SIMD in Rust.

---

<div class="post-metadata">

**Author:** ![droundy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/droundy/32/1129_2.png) [@droundy](https://users.rust-lang.org/u/droundy)\
**Post date:** [August 24, 2015, 7:01pm UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/2 "2015-08-24T19:01:07Z")

</div>

Good fun! I'd love to see the cross-platform layer made truly portable by means of a scalar implementation that is selected if there is no SIMD option. That would enable "worry-free" use of simd types in data structures, which sounds highly appealing to me for cases where they make a particularly good fit.

---

<div class="post-metadata">

**Author:** ![huon](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/huon/32/19_2.png) [@huon](https://users.rust-lang.org/u/huon)\
**Post date:** [August 24, 2015, 8:45pm UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/3 "2015-08-24T20:45:23Z")

</div>

Having a cross-platform library that calls the appropriate instruction on ARM/x86/... or fall-backing to a scalar version would be great. I don't think I want that to be part of `simd` (at least, not yet), but it should be possible to do in a zero-overhead way in a wrapper lib.

---

<div class="post-metadata">

**Author:** ![PeteVine](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@PeteVine](https://users.rust-lang.org/u/PeteVine)\
**Post date:** [November 6, 2015, 10:11am UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/4 "2015-11-06T10:11:08Z")

</div>

Which instruction sets are supported? (does it start at SSE2 or way back at MMX?)

---

<div class="post-metadata">

**Author:** ![huon](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/huon/32/19_2.png) [@huon](https://users.rust-lang.org/u/huon)\
**Post date:** [November 6, 2015, 11:16am UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/5 "2015-11-06T11:16:29Z")

</div>

At the moment there's no MMX intrinsics, but I'm fairly sure it is just a matter of adding them.

---

<div class="post-metadata">

**Author:** ![eefriedman](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@eefriedman](https://users.rust-lang.org/u/eefriedman)\
**Post date:** [November 6, 2015, 9:50pm UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/6 "2015-11-06T21:50:24Z")

</div>

MMX in particular is is bit complicated because we would need to expose the LLVM type `x86_mmx` to Rust code somehow. Also, emms is a complete mess: LLVM can't automatically insert emms calls, so you have to insert them yourself. But if you insert them yourself, you have to be very careful because the LLVM optimizer doesn't understand the relationship between MMX operations, x87 operations, and emms, and therefore can reorder instructions incorrectly. Overall, it's probably not worth the trouble.

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/gnzlbg/32/2107_2.png) [@gnzlbg](https://users.rust-lang.org/u/gnzlbg)\
**Post date:** [January 22, 2019, 5:06pm UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/7 "2019-01-22T17:06:37Z")

</div>

> [@eefriedman](#):
>
> Overall, it’s probably not worth the trouble.

Whoever you are @eefriedman, you were right. MMX support has been a road full of pain for all the reasons you mention.

> the LLVM optimizer doesn’t understand the relationship between MMX operations, x87 operations, and emms, and therefore can reorder instructions incorrectly.

For a hammer, one can use `asm!("emms" : : : "volatile")` to work around that. I just added [0] support for `_mm_empty()` to `core::arch` and there I use `llvm.x86.mmx.emms`. We'll see how that works out. In the mean time I've filled [Diagnostic: no EMMS instruction before call · Issue #57831 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/57831) to replicate ICC's warning about missing `emms` and [repr(simd) struct with one i64 element incorrectly emits x86\_mmx · Issue #57832 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/57832) to clean up the current implementaiton of `x86_mmx` in rustc.

[0] I've been wondering for a year why our tests were randomly failing and fixing themselves just to fail again and why these errors were pretty much impossible to reproduce in different machines and missing `emms` is probably a big source of issues.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:50am UTC](https://users.rust-lang.org/t/improved-simd-in-rust/2604/8 "2023-01-12T08:50:02Z")

</div>


