|
![](/i/fill.gif) |
Orchid Win7 v1 <voi### [at] dev null> wrote:
> > That's a pretty epic fail.
> As I say, the impressive thing is that it's a correct (yet inefficient)
> implementation of negate().
Out of curiosity, I wanted to see what gcc would produce from that code
(when written in C++). In other words this:
//-----------------------------------------
int a(int input)
{
int value = 0;
for(int n = input; n != 0; ++n)
++value;
return value;
}
//-----------------------------------------
It produces this, in asm:
//-----------------------------------------
.LFB0:
.cfi_startproc
movl %edi, %edx
xorl %eax, %eax
negl %edx
testl %edi, %edi
cmovne %edx, %eax
ret
.cfi_endproc
//-----------------------------------------
If we "disassemble" that back to C++, it's basically equivalent to:
//-----------------------------------------
int a(int input)
{
return input == 0 ? 0 : -input;
}
//-----------------------------------------
For some reason gcc produces a needless test against zero, but
otherwise it's rather impressive that it got rid of the loop completely.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |