avr-gcc-list
[Top][All Lists]
Advanced

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

[avr-gcc-list] odd optimization differences of shift between C and C++


From: William \"Chops\" Westfield
Subject: [avr-gcc-list] odd optimization differences of shift between C and C++
Date: Sun, 7 Nov 2010 20:57:56 -0800

I'm told that I should post questions here rather than on avr-freaks.
Here's one...
(some additional discussion at http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=98888 ) 4.3.4 behaves the same, and
there are NO right shifts of SIGNED variables involved...


Is there something I'm not understanding about C++ ? I have a trivial program:

Code:
extern void foo(unsigned char c);
extern void bar(int c);

extern unsigned char c;

int main()
{
    foo(c>>4);
    bar(c>>4);
}



If I compile with avr-gcc I get the expected optimization using SWAP:
Code:
gcc --version
i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5493)

 avr-gcc -Os -g -mmcu=atmega8 -c -o shift.o shift.c
BillW-MacOSX-2<1155> avr-objdump -S shift.o

00000000 <main>:

extern unsigned char c;

int main()
{
    foo(c>>4);
   0:   80 91 00 00    lds   r24, 0x0000
   4:   82 95          swap   r24
   6:   8f 70          andi   r24, 0x0F   ; 15
   8:   00 d0          rcall   .+0         ; 0xa <main+0xa>
    bar(c>>4);
   a:   80 91 00 00    lds   r24, 0x0000
   e:   82 95          swap   r24
  10:   8f 70          andi   r24, 0x0F   ; 15
  12:   90 e0          ldi   r25, 0x00   ; 0
  14:   00 d0          rcall   .+0         ; 0x16 <main+0x16>
}
  16:   08 95          ret


If I compile the SAME FILE using g++, it apparently does the promotion to int much earlier, and generates much bulkier and slower code:

g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5493)

 avr-g++ -Os -g -mmcu=atmega8 -c -o shift.o shift.c

avr-objdump -S shift.o

00000000 <main>:

extern unsigned char c;

int main()
{
    foo(c>>4);
   0:   80 91 00 00    lds   r24, 0x0000
   4:   82 95          swap   r24
   6:   8f 70          andi   r24, 0x0F   ; 15
   8:   00 d0          rcall   .+0         ; 0xa <main+0xa>
    bar(c>>4);
   a:   80 91 00 00    lds   r24, 0x0000
   e:   90 e0          ldi   r25, 0x00   ; 0
  10:   24 e0          ldi   r18, 0x04   ; 4
  12:   96 95          lsr   r25
  14:   87 95          ror   r24
  16:   2a 95          dec   r18
  18:   01 f4          brne   .+0         ; 0x1a <main+0x1a>
  1a:   00 d0          rcall   .+0         ; 0x1c <main+0x1c>
}
  1c:   80 e0          ldi   r24, 0x00   ; 0
  1e:   90 e0          ldi   r25, 0x00   ; 0
  20:   08 95          ret


Is this expected? Is it bug-worthy ?


reply via email to

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