rust-lang / packed_simd

Portable Packed SIMD Vectors for Rust standard library
https://rust-lang.github.io/packed_simd/packed_simd_2/
Apache License 2.0
589 stars 74 forks source link

Underlying slice access to avoid intermediate buffers #346

Closed therealbnut closed 2 years ago

therealbnut commented 2 years ago

Hi,

I've noticed the aligned slice copy methods essentially internally cast the structure to a slice. I'd find it very useful for processing file data in a Simd structure to have read/write access to this slice, to avoid an intermediate copy.

I currently have to do this:

let mut buffer = [0u8; 64];
...
file.read(&mut buffer)?;
let data = u8x64::from_slice_aligned(&buffer);
...

I'd like to do this:

let mut data = u8x64::splat(0);
...
file.read(data.as_slice_mut())?;
...

Is there any reason you couldn't do something like this (although maybe without the unsafe code by using knowledge of the underlying data structures):

impl Simd<S> {
    fn as_slice(&self) -> &S {
        unsafe { &*(self as *const Self as *const S) }
    }
    fn as_slice_mut(&mut self) -> &mut S {
        unsafe { &mut *(self as *mut Self as *mut S) }
    }
}
Lokathor commented 2 years ago

If you use bytemuck with the nightly_portable_simd feature then you can do this fairly easily.

In general, special transmutation related things are not as likely with the portable simd api so that there's not duplication with the eventual APIs from the safe-transmute project.

therealbnut commented 2 years ago

Thanks @Lokathor, I can also use code similar to what I posted. I was thinking this might be something very useful to integrate into the main package as it seems fairly core.

burrbull commented 2 years ago

main package as it seems fairly core

https://github.com/rust-lang/portable-simd/issues/170

therealbnut commented 2 years ago

@burrbull thanks, I’ll look into that and raise there if needed.

Sorry, I vaguely remembered plans for something more official, but couldn’t find it when googling and searching crates.io.

Lokathor commented 2 years ago

Oh, sorry, I didn't even realize that this is the packed_simd repo I just assumed it was the portable-simd repo XD