help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: About g++ optimization


From: Andre Poenitz
Subject: Re: About g++ optimization
Date: Tue, 4 Apr 2006 22:22:51 +0200

rmansfield@gmail.com <rmansfield@gmail.com> wrote:
> Change your more recent test case to i--, then look at the assembly.
> There will be a cmpl $-1

Urm, yes. You are right. Problem between desk and chair.

Last try (promised...)

 int foo(int n)         int bar(int n)               int baz(int n)   
 {                      {                            {
   int s = 0;             int s = 0;                   int s = 0;
   int i;                 int i;                       int i;
   for (i = n; i--; )     for (i = 0; i != n; ++i)     for (i = n; --i >= 0; )
     s += i;                s += i;                      s += i;
   return s;              return s;                    return s;
 }                      }                            }                          
 
                           
gives with -O3:

 foo:                     bar:                    baz:
   pushl  %ebp              pushl  %ebp             pushl  %ebp
   xorl  %eax, %eax         xorl  %eax, %eax        xorl  %eax, %eax
   movl  %esp, %ebp         movl  %esp, %ebp        movl  %esp, %ebp
   movl  8(%ebp), %edx      xorl  %edx, %edx        movl  8(%ebp), %edx
   decl  %edx               movl  8(%ebp), %ecx     decl  %edx
   cmpl  $-1, %edx          cmpl  %ecx, %eax        js  .L60
   je  .L8                  je  .L19                .p2align 4,,7
   .p2align 4,,7            .p2align 4,,7         .L61:
 .L9:                     .L20:                     addl  %edx, %eax
   addl  %edx, %eax         addl  %edx, %eax        decl  %edx
   decl  %edx               incl  %edx              jns  .L61
   cmpl  $-1, %edx          cmpl  %ecx, %edx      .L60:
   jne  .L9                 jne  .L20               popl  %ebp
 .L8:                     .L19:                     ret
   popl  %ebp               popl  %ebp          
   ret                      ret                


So it looks as though the 'canonical' solution ('bar') uses one
more register (ecx) than my original micro-optimized version ('foo').

Of course, I am not sure anymore what the difference of the cmpl
against a register or a constant will translate to in parctice.

In any case, using 'baz' it seems indeed to be possible to skip the
'cmpl' entirely (_and_ save the register). The only unfortunate ascpect
here is that it won't work with unsigned counters as C++'s
std::container::size_types.

Andre'


reply via email to

[Prev in Thread] Current Thread [Next in Thread]