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

Underlying slice access to avoid intermediate buffers #346

Closed
therealbnut opened this issue Aug 18, 2022 · 5 comments
Closed

Underlying slice access to avoid intermediate buffers #346

therealbnut opened this issue Aug 18, 2022 · 5 comments

Comments

@therealbnut
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

main package as it seems fairly core

rust-lang/portable-simd#170

@therealbnut
Copy link
Author

@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
Copy link
Contributor

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

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

3 participants