Skip to content

poor performance for Mask::from_array #184

Closed
@programmerjake

Description

@programmerjake

The existing code copies/converts one bool at a time, unvectorized by llvm.

Suggested fix:
https://rust-lang.zulipchat.com/#narrow/stream/257879-project-portable-simd/topic/is.20it.20mature.20to.20switch.20to.20portable-simd.3F/near/261100448

https://rust.godbolt.org/z/GdTx63axx

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

extern "platform-intrinsic" {
    fn simd_ne<T, U>(a: T, b: T) -> U;
    fn simd_cast<T, U>(v: T) -> U;
}

#[repr(simd)]
pub struct Simd<T, const N: usize>(pub [T; N]);

pub fn from_array_new(v: [bool; 16]) -> Simd<i32, 16> {
    unsafe {
        let v: [u8; 16] = std::mem::transmute(v);
        let bools: Simd<i8, 16> = simd_ne(Simd(v), Simd([0u8; 16]));
        simd_cast(bools)
    }
}

pub fn from_array_current(v: [bool; 16]) -> Simd<i32, 16> {
    let mut retval = Simd([0; 16]);
    for i in 0..16 {
        retval.0[i] = if v[i] { -1 } else { 0 };
    }
    retval
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: BugI-slowImpact: Slowww

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions