[Top][All Lists]

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

Re: [avr-gcc-list] Avr-gcc Produces Incorrect Code with -Os

From: Dave N6NZ
Subject: Re: [avr-gcc-list] Avr-gcc Produces Incorrect Code with -Os
Date: Thu, 15 May 2008 08:51:08 -0700
User-agent: Thunderbird 1.5 (X11/20051201)

The result of the atan2 does not impact the bit value written to PORTA, so the compiler is free to re-order it. That's what optimizers do.


Thomas D. Dean wrote:
I have a code segment which 1. sets a bit in PORTA.
  2.  calls atan2.
  3.  clears the same bit in PORTA.

The compiler produces code that 1. sets the bit in PORTA.
  2.  clears the same bit in PORT.
  3.  calls atan2.

With -O0, the code is correct.

# uname -a
FreeBSD dv6000.tddhome 7.0-STABLE FreeBSD 7.0-STABLE #0: Sun May  4 07:58:25 
PDT 2008     address@hidden:/usr/src/sys/GENERIC  i386

# avr-gcc --version
avr-gcc (GCC) 4.2.2
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

The code fragment,

  line3[20] = ' ';
  line4[20] = ' ';
  TRACE_ON(TRACE4);  // portb |= _bv(04)
atan_rad = atan2(cos_rad,sin_rad); TRACE_OFF(TRACE4); // portb &= ~_bv(04)

# gmake ATMEGA16=1 TRACE=1 8_BIT=1 clean all
rm -f *.o *.elf *.s19 *.b *.a *.map *.hex
avr-gcc  -mmcu=atmega16  -Wall -Wmissing-prototypes  -Os -DTRACE -DLCD_8_BIT   
-I.    -c -o main.o main.c
avr-gcc  -mmcu=atmega16  -Wall -Wmissing-prototypes  -Os -DTRACE -DLCD_8_BIT   
-I.    -c -o timer.o ../common/timer.c
avr-gcc  -mmcu=atmega16  -Wall -Wmissing-prototypes  -Os -DTRACE -DLCD_8_BIT   
-I.    -c -o lcd-8-bit.o ../common/lcd-8-bit.c
avr-gcc -o main.elf main.o timer.o lcd-8-bit.o -mmcu=atmega16  
-L../../../asus-avr-1.0/config/stk500 -L../../../asus-avr-1.0/lib  
-L/usr/local/avr/lib/ -lasus -lc -lm -lprintf_flt -lscanf_flt
avr-objcopy -j .text -j .data -O ihex main.elf main.hex

#  avr-objdump -D main.elf | grep 'call.*atan2' -B12 -A10
     398:       0e 94 71 03     call    0x6e2   ; 0x6e2 <dtostrf>
     39c:       80 e2           ldi     r24, 0x20       ; 32
     39e:       f1 01           movw    r30, r2
     3a0:       84 8b           std     Z+20, r24       ; 0x14
     3a2:       f2 01           movw    r30, r4
     3a4:       84 8b           std     Z+20, r24       ; 0x14
     3a6:       dc 9a           sbi     0x1b, 4 ; 27
     3a8:       dc 98           cbi     0x1b, 4 ; 27
     3aa:       a5 01           movw    r20, r10
     3ac:       94 01           movw    r18, r8
     3ae:       c7 01           movw    r24, r14
     3b0:       b6 01           movw    r22, r12
     3b2:       0e 94 0f 06     call    0xc1e   ; 0xc1e <atan2>
     3b6:       cf 54           subi    r28, 0x4F       ; 79
     3b8:       df 4f           sbci    r29, 0xFF       ; 255
     3ba:       08 81           ld      r16, Y
     3bc:       19 81           ldd     r17, Y+1        ; 0x01
     3be:       c1 5b           subi    r28, 0xB1       ; 177
     3c0:       d0 40           sbci    r29, 0x00       ; 0
     3c2:       23 e0           ldi     r18, 0x03       ; 3
     3c4:       46 e0           ldi     r20, 0x06       ; 6
     3c6:       0e 94 71 03     call    0x6e2   ; 0x6e2 <dtostrf>
     3ca:       b1 01           movw    r22, r2

AVR-GCC-list mailing list

reply via email to

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