C++ Logo

std-proposals

Advanced search

Re: [std-proposals] SIMD by just operating on 2 arrays

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 12 Apr 2023 13:53:44 -0400
On Wed, Apr 12, 2023 at 1:42 PM samuel ammonius via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Wed, Apr 12, 2023 at 2:11 PM Sebastian Wittmeier via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> what is it exactly you are mainly after?
>>
>> a) To express vector operations with implicit fallback to a loop
>>
>> b) To have a nice way to express SIMD code with simple mathematical
>> operations
>>
>> c) To create better interoperation between C-style arrays and SIMD code?
>>
>> Or strictly a combination of those?
>>
>
> It's mainly b) and c). My primary issue was that I thought templates and
> classes are overkill for something that is just a CPU instruction. However,
> everyone has brought up a ton of great issues so I don't think this idea
> would work anymore. But just as a brainstorm, what about a class that
> operates on an array like this:
>
> alignas(float) float a[4] = {0.0f, 1.0f, 2.0f, 3.0f};
>
> std::simd4 a_simd = std::simd4(&a);
>
> a_simd += 0.1f; // Adds 0.1 to all elements of "a"
>
> This way arrays can be operated on without breaking compatibility. Does
> this sound like a better idea?
>

Yes; and again, observe that you can write that *today* (if you replace
`std::` with `my::`), using operator overloading. The trick is figuring out
how to get it to use SIMD instructions on platforms where that's natively
possible.

In passing, note that `float`s are always aligned to the alignment of
`float`, so the `alignas(float)` is redundant. And what you've got here is
to `valarray` as `span` is to `array`, so you might consider naming it
`my::valspan` (except that that's a pretty icky name :)).

–Arthur

Received on 2023-04-12 17:53:57