Lokathor / wide

A crate to help you go wide. By which I mean use SIMD stuff.
https://docs.rs/wide
zlib License
279 stars 23 forks source link

explorability: suggest a `new(v1, v2, v3, v4)` function for better visibility #114

Closed m4b closed 2 years ago

m4b commented 2 years ago

It took me a somewhat embarrassing amount of time to realize that e.g., constructing a f32x4 wide type from a set of points can use the From impl on [f32; 4], e.g., I could not figure out how to implement this:

use nalgebra as na;

fn main() {
    let points = (0..8)
        .map(|i| {
            let i = i as f32;
            na::Vector3::new(i, i * 2.0, i * 3.0)
        })
        .collect::<Vec<_>>();

    let mut wpoints = vec![];

    println!("{:?}", points);

    for ps in points.chunks(4) {
        let x = simba::simd::WideF32x4::from([ps[0].x, ps[1].x, ps[2].x, ps[3].x]);
        let y = simba::simd::WideF32x4::from([ps[0].y, ps[1].y, ps[2].y, ps[3].y]);
        let z = simba::simd::WideF32x4::from([ps[0].z, ps[1].z, ps[2].z, ps[3].z]);
        wpoints.push(na::Vector3::new(x, y, z));
    }
    println!("{:#?}", wpoints);
}

I was looking at the associated methods but found no constructor, or similar function for f32x4.

Whereas compared to simba::simd::f32x4 there is a new method, which is easily visible and seen when exploring documentation (https://docs.rs/simba/latest/simba/simd/struct.Simd.html#method.new-1):

         let x = simba::simd::f32x4::new(ps[0].x, ps[1].x, ps[2].x, ps[3].x);
         let y = simba::simd::f32x4::new(ps[0].y, ps[1].y, ps[2].y, ps[3].y);
         let z = simba::simd::f32x4::new(ps[0].z, ps[1].z, ps[2].z, ps[3].z);

I only accidentally saw the from impl for wide::f32x4 when browsing simba::simd::WF32x4 and noticed it somehow had a From<[f32;4] so I knew it must be possible.

Anyway, I'm suggesting something like nice convenience function:

pub fn new(v0: f32, v1: f32, v2: f32, v3: f32) -> Self {
  Self::from([v0, v1, v2, v3])
}

so that when users are browsing documentation it's easily visible to see how to construct such a wide vector, (it will appear with methods at top).

Anyway, thanks for the library!

Lokathor commented 2 years ago

I'd love to have it, do you want to do the PR?

m4b commented 2 years ago

This was more of a drive by issue on my part, as I had this minor ergonomic papercut when playing around with some wide types, and figured at least filing my experience was least I could do, so I'm not sure I'd have the time at the moment to contribute a full PR, however.

Lokathor commented 2 years ago

no worries i can probably get it soon

Lokathor commented 2 years ago

Fixed in 0.7.4

m4b commented 2 years ago

wow this is super fast turnaround, thanks! Looks great in documentation now, right front and center, thanks for this, I suspect it'll help out a lot of people learning your awesome crate :) https://docs.rs/wide/latest/wide/struct.f32x8.html#method.new