How to use Vector Extensions in an opBinary
user1234
user1234 at 12.de
Sun Apr 17 13:02:42 UTC 2022
On Sunday, 17 April 2022 at 11:16:25 UTC, HuskyNator wrote:
> I recently found out there is [support for vector
> extensions](https://dlang.org/spec/simd.html)
> But I have found I don't really understand how to use it, not
> even mentioning the more complex stuff. I couldn't find any
> good examples either.
>
> I'm trying to figure out how to implement the following
> opBinary using vector extensions.
> ```dlang
> alias MatType = typeof(this);
>
> union{
> T[rows*columns] vec;
> T[rows][columns] mat;
> }
>
> MatType opBinary(string op, T)(const T right) const {
> MatType result = this;
> mixin("result.vec[] = this.vec[] " ~ op ~ " right;");
> return result;
> }
> ```
> Or alternatively, as I'm not sure which is more
> efficient/faster:
> ```dlang
> MatType opBinary(string op, T)(const T right) const {
> MatType result;
> static foreach(i; 0..rows*columns)
> mixin("result.vec[i] = this.vec[i] " ~ op ~ " right;");
> return result;
> }
> ```
>
> But going off the documentation, I have no idea how to use
> vector extensions to achieve something like this.
>
> As a small disclaimer; I don't know to what extent the compiler
> already automates these kind of operations, and mostly want to
> use this as a learning experience.
>
> Kind regards, HN
I'd experiment with CompilerExplorer. For example
[this](https://godbolt.org/z/a51r8GEv4) shows that the codegen is
identical for both opBinary version for "+".
About time spent to compile, `static foreach` is known not to
scale well.
More information about the Digitalmars-d-learn
mailing list