core.simd and dynamic arrays

John C. example at example.com
Sun Jan 26 12:45:11 UTC 2025


Hello everyone. I am complete newbie in D and programming at all 
and I can't understand why dynamic arrays can't be used within 
following D code:
```d
import std.random : uniform01;
import core.simd;

void main() {
     align(32) float[] a = new float[128];
     align(32) float[] b = new float[128];
     align(32) float[] c = new float[128];

     /* filling input arrays with random numbers in [0, 1) range */
     for (size_t i = 0; i < c.length; ++i) {
         a[i], b[i] = uniform01(), uniform01();
     }

     for (size_t i = 0; i < c.length; i += 8) {
         /* seems that segfault reason hides below */
         auto va = *cast(float8 *)(&a[i]);
         auto vb = *cast(float8 *)(&b[i]);
         auto vc = va * vb;
         *cast(float8 *)(&c[i]) = vc;
     }
}
```

I have tested same code (but used instead static arrays of size 
8) and it worked correctly. For bigger static arrays code above 
even outperformed one-by-one element iterative version.
I'm using LDC compiler 1.36.0 on x86_64 Linux system with "-w -O3 
-mattr=+avx" compiler flags.


More information about the Digitalmars-d-learn mailing list