simd icon indicating copy to clipboard operation
simd copied to clipboard

Structures with arbitrary size

Open flip111 opened this issue 8 years ago • 3 comments

Would it be possible to define a structure where the right SIMD instruction is chosen automatically (question 1)? Where the possible SIMD instructions are detected on compile time (question 2)?

Let's say i have an older CPU so no AVX2, hopefully the compiler can hint this just like gcc. Then i can have a maximum of 128 bits vectors.

For example i have a 1300 bit vector which i want to AND with another 1300 vector. Which would take 10x 128 bit vectors and 1x 20 bit vector (round up to 32 bits). If the size of the vector is known at compile time could rust generate the right instructors for it, with only me specifying the 1300 bit size? If 1300 is the wrong example, think of 1312 or 1344.

Runtime would also be interesting (question 3), but i think actually less interesting than compile time because you would have to branch to normal instructions (for 32 bits and so) and avx instructions based on size which will make you loose performance.

flip111 avatar Mar 02 '17 16:03 flip111

Internally, this library uses cfg(target_feature = "...") to define certain things depending on compile options. You can see a simple example here: https://github.com/rust-lang-nursery/simd/blob/master/src/x86/mod.rs --- You can use cfg(target_feature = "...") in your code too.

Runtime would also be interesting (question 3), but i think actually less interesting than compile time because you would have to branch to normal instructions (for 32 bits and so) and avx instructions based on size which will make you loose performance.

I imagine you would amortize the branch (possibly by detecting it once at startup and setting a global function pointer). Regardless, this library doesn't support that anyway. But hopefully some day. :-)

BurntSushi avatar Mar 02 '17 16:03 BurntSushi

Looks good :) What about adding support for arbitrary size vectors though? So that chosing SIMD instructions or not is abstracted away?

flip111 avatar Mar 03 '17 11:03 flip111

@flip111 I'm not sure that's in scope for this crate. I'd recommend building something yourself in a separate crate and see where it takes us.

BurntSushi avatar Mar 03 '17 11:03 BurntSushi