Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insufficiently aligned x86_64 SIMD stores #151

Open
finnbear opened this issue Jan 27, 2025 · 1 comment
Open

Insufficiently aligned x86_64 SIMD stores #151

finnbear opened this issue Jan 27, 2025 · 1 comment

Comments

@finnbear
Copy link
Collaborator

let mut data = std::mem::MaybeUninit::<[f32; 4]>::uninit();
_mm_store_ps(data.as_mut_ptr() as *mut f32, res);

The docs of _mm_store_ps say:

If the pointer is not aligned to a 128-bit boundary (16 bytes) a general protection fault will be triggered (fatal program crash).

[f32; 4] has the same alignment as f32, or only 4 bytes.

This code appears to be relying on Rust function stack memory layout properties that are not guaranteed.

Possible solutions:

  • Create a wrapper type with #[repr(C, align(16))]
  • Switch to unaligned SIMD stores, e.g. _mm_storeu_ps

This applies to other x86_64 SIMD stores, not just on f32x4.

@svenstaro
Copy link
Owner

svenstaro commented Jan 27, 2025

Good you bring this up. Frankly, we probably shouldn't be using explicit SIMD intrinsics in the first place. There are plenty of libraries that would make this a lot easier and incidentally even allow us to automatically targets other architectures implicitly. In addition, they all run on stable:

I don't think we should fix the current SIMD implementation at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants