Proposal
Problem statement
The SIMD types in core::arch don't implement any arithmetic operators in core::ops, which makes them unwieldy and verbose to use
Motivating examples or use cases
Currently writing a simple bitwise complement of a vector type (e.g. __m256i from x86) requires considerable boilerplate
#[target_feature(enable = "avx2)]
pub fn foo(a: __m256i) -> __m256i {
_mm256_xor_si256(a, _mm256_set1_epi64x(-1))
}
Implementing some arithmetic ops for these SIMD types will make them much easier to use. This is a popular C compiler extension, supported by gcc, clang and icc.
Solution sketch
Implement the following traits (and the assign variants, if applicable) for all SIMD types that have a defined element size (one exception is __m{128,256,512}i in x86)
Add
Sub
Mul
Div
Rem
Neg
Index and IndexMut (should behave identical to indexing to the array got by transmuting) (this can be discussed, the C apis don't define this. Probably more complicated than transmuting due to ARMBE)
Along with these, integer SIMD types should also implement
BitOr
BitAnd
BitXor
Not
Shl and Shr (if element size is defined)
Fortunately, all of these can be implemented easily (and more importantly, independent of target features) by using core::intrinsics::simd
Alternatives
- Leave this for third-party crates (i.e. crates that offer a "safe" intrinsics API, e.g.
safe_arch)
- Let
portable_simd handle this
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Proposal
Problem statement
The SIMD types in
core::archdon't implement any arithmetic operators incore::ops, which makes them unwieldy and verbose to useMotivating examples or use cases
Currently writing a simple bitwise complement of a vector type (e.g.
__m256ifrom x86) requires considerable boilerplateImplementing some arithmetic ops for these SIMD types will make them much easier to use. This is a popular C compiler extension, supported by gcc, clang and icc.
Solution sketch
Implement the following traits (and the assign variants, if applicable) for all SIMD types that have a defined element size (one exception is
__m{128,256,512}iin x86)AddSubMulDivRemNegIndexandIndexMut(should behave identical to indexing to the array got by transmuting) (this can be discussed, the C apis don't define this. Probably more complicated than transmuting due to ARMBE)Along with these, integer SIMD types should also implement
BitOrBitAndBitXorNotShlandShr(if element size is defined)Fortunately, all of these can be implemented easily (and more importantly, independent of target features) by using
core::intrinsics::simdAlternatives
safe_arch)portable_simdhandle thisLinks and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution: