arduano / simdeez

easy simd
MIT License
332 stars 25 forks source link

simd_runtime_generate can't handle generic functions #38

Open Ralith opened 4 years ago

Ralith commented 4 years ago

Explicit generic parameters aren't parsed, and impl Trait arguments don't work due to the generic parameter the macro adds itself. This makes it difficult to abstract SIMD code.

I can't help but wonder if there's a way these macros could be replaced by a higher order function or similar...

Ralith commented 4 years ago

After some tinkering I'm inclined to judge that this can only really be solved sanely with a proc macro or much better support in rust for higher-kinded types.

verpeteren commented 1 year ago

@Ralith : can you share a small example of what you want to accomplish? Something that we can drop into the repo as src/main.rs would probably be enough to start tinkering and thinking about it.

Ralith commented 1 year ago

Simplified:

pub trait Height {
    fn from_f32(x: f32) -> Self;
}

impl Height for u16 {
    // Fixed-point fraction of max value
    fn from_f32(x: f32) -> u16 {
        (x * u16::max_value() as f32) as u16
    }
}

impl Height for f32 {
    /// Meters
    fn from_f32(x: f32) -> f32 {
        x * 1e4
    }
}

fn generate_heightmap<S: Simd, T: Height>(chunk: &Chunk, mem: &mut [MaybeUninit<T>]) {
    unsafe {
        for mem in mem.chunks_mut(S::VF32_WIDTH) {
            let result = S::set1_ps(0.0);
            for (i, out) in mem.iter_mut().enumerate() {
                out.write(T::from_f32(result[i]));
            }
        }
    }
}

At this point, if I revisit this code I'll probably just use std simd on nightly, though.