Reimplementing the bulk of std.meta iteratively

Dukc ajieskola at gmail.com
Tue Sep 29 14:42:42 UTC 2020


On Tuesday, 29 September 2020 at 12:54:53 UTC, Stefan Koch wrote:
> So we had this:
>
>> int mul(
>>   int x,
>>   int y
>> ) {
>>   int result = 0;
>>   for(int i = 0; i < x; i++)
>>   {
>>     result += y;
>>   }
>>   return result;
>> }
>
>
> And the code generated was
>
> 	imull	%esi, %edi // y *= x
> 	xorl	%eax, %eax // result = 0
> 	testl	%esi, %esi // if (y)
> 	cmovgl	%edi, %eax // if (x > 0) result = x;
>         retq               // return result;
>
> On first glance these instructions seem superfluous
> But we have to understand that the compiler had no way of 
> knowing that we actually wanted to generate a mere multilply.
>
> For example there is the loop entrance check.
> Why is it there?
>
> We can proof it's necessity quite easily.
> [snip]

I don't know assembly, but I can see an even simpler reason why 
the compiler can't generate a multiply instruction. It's `mul(-5, 
5)*`.




More information about the Digitalmars-d mailing list