2

In an auto-vectorized array initialization,

alignas(64)
const float a[16]={
    b[i+0],b[i+1],b[i+2],b[i+3], // normal initialization

    // self-referencing for duplicated data
    a[0],a[1],a[2],a[3], 
    a[0],a[1],a[2],a[3],
    a[0],a[1],a[2],a[3]
};

runs faster than this:

alignas(64)
const float a[16]={
    b[i+0],b[i+1],b[i+2],b[i+3], // normal initialization
    b[i+0],b[i+1],b[i+2],b[i+3], // normal initialization
    b[i+0],b[i+1],b[i+2],b[i+3], // normal initialization
    b[i+0],b[i+1],b[i+2],b[i+3], // normal initialization
};

Is there any caveat for using the first version? Can it cause any kind of bug for any compiler?

When I try same for transposed version (where columns are duplicated instead), compiler generates same cpu instructions for both versions. Is this a side-effect of compiler's initialization order of elements or is it about ability of CPU architecture (i.e. not having an efficient instruction for that)?

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
  • It's guaranteed to work since C++20. Before C++20, it was under specified, but probably intended to work. – eerorika Apr 07 '22 at 13:27
  • @eerorika What C++20 change do you consider relevant here? – user17732522 Apr 07 '22 at 13:28
  • @user17732522 Added wording from the designated initialisation proposal: `The initializations of the elements of the aggregate are evaluated in the element order. That is, all value computations and side effects associated with a given element are sequenced before those of any element that follows it in order.` – eerorika Apr 07 '22 at 13:29
  • The other (duplicate answer) says it is "not well defined" but at least it has "defined"? So is it safe to assume element order always goes from 0 to n-1? – huseyin tugrul buyukisik Apr 07 '22 at 13:32
  • @huseyintugrulbuyukisik Yes, that's the guaranteed element order of arrays. – eerorika Apr 07 '22 at 13:33
  • @user17732522 In general, an up to date answer would be posted on the target, and one could also leave a comment on the older answer pointing out that it's wrong. You could also ask the answerer to consider updating their answer (although an answer that *was* correct is perfectly fine to have around.) If this question can be made sufficiently different to the target, you can edit it, and add a link to the other question, explaining why it's different, but that doesn't seem viable in this particular case. – cigien Apr 07 '22 at 13:34
  • @eerorika I see. I guess I assumed that was the intend of the old evaluation rules as well. – user17732522 Apr 07 '22 at 13:34
  • @user17732522 I added a better duplicate. – eerorika Apr 07 '22 at 13:35
  • For the performance part, clearly a missed optimization if code ends up compiling to give the same results but slower. – Peter Cordes Apr 07 '22 at 13:36
  • @eerorika Yeah, that's a better target, with more modern, and better answers. I've closed the original target as a duplicate of that as well. Thanks for finding that. – cigien Apr 07 '22 at 13:37

0 Answers0