dmd optimizer now converted to D!
Seb
seb at wilzba.ch
Thu Jul 5 14:05:42 UTC 2018
On Thursday, 5 July 2018 at 12:50:18 UTC, Ivan Kazmenko wrote:
> With D, I used mixins, and they were cumbersome. Now that we
> have static foreach, it's just this:
>
> for (int i = 0; i < 4 * n; i += 4)
> static foreach (k; 0..4)
> a[i + k] += i + k;
>
> This looks very nice to me, but still not ideal: a
> static-foreach argument cannot encapsulate a runtime variable,
> so we have to repeat "i + k" twice. This can get cumbersome
> for a more complex example. Is there any better way? To
> prevent introducing bugs when micro-optimizing, I'd like the
> loop body to remain as unchanged as it can be.
>
> Ivan Kazmenko.
FYI: you can introduce scopes with static foreach to declare new
variables:
for (int i = 0; i < 4 * n; i += 4)
{
static foreach (k; 0..4)
{{
auto idx = i + k
a[idx] += idx;
}}
}
However, LDC is pretty good at loop unrolling out of the box:
https://godbolt.org/g/4nSWzQ
(even though gdc is written there, it's "ldc" - known typo:
https://github.com/mattgodbolt/compiler-explorer/pull/988)
More information about the Digitalmars-d
mailing list