Skip to content

easier functions to work with slices with dynamic length #240

@vova616

Description

@vova616

I searched for an easy way to deal with sizes that cannot be always in the length you want so I made something like this and wondered why this isn't part of the library? or maybe I missed it?

The names/impl can change but thats the idea

pub trait SimdMap<T> where Self: Sized {
    #[inline(always)]
    fn simd_map<F>(slice: &mut [T], mut func: F) where F: FnMut(Self) -> Self;
}

impl SimdMap<u8> for u8x64 {
    #[inline(always)]
    fn simd_map<F>(mut slice: &mut [u8], mut func: F) where F: FnMut(Self) -> Self {
        while slice.len() >= Self::lanes() {
            func(Self::from_slice_unaligned(slice)).write_to_slice_unaligned(&mut slice);
            slice = &mut slice[Self::lanes()..];
        }
        let mut temp = [0u8; Self::lanes()];
        temp[..slice.len()].copy_from_slice(slice);
        func(Self::from_slice_unaligned(&temp)).write_to_slice_unaligned(&mut temp);
        slice.copy_from_slice(&temp[..slice.len()]);
    }
}

then I can use this on any size buffer with the types I want and I don't have to worry about too much stuff or size

pub fn xor(buff: &mut [u8]) {
    u8x64::simd_map(buff, |data| {
        data ^ 0x15
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions