POV-Ray : Newsgroups : povray.off-topic : Unexpected : Re: Unexpected Server Time
28 Jul 2024 22:31:43 EDT (-0400)
  Re: Unexpected  
From: Warp
Date: 19 Aug 2013 15:01:16
Message: <52126b7c@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.