Tried with: avr-gcc (GCC) 3.4.6 (Gentoo 3.4.6-r2 p1.5, ssp-3.4.6-1.0, pie-8.7.10) avr-gcc (GCC) 4.2.4 (Gentoo 4.2.4 p1.0) avr-gcc (Gentoo 4.3.2-r2 p1.5, pie-10.1.5) 4.3.2
Both -Os and -O0 behave the same, the -mmcu=atmega1281 and -mmcu=atmega128 too (however the later was not tested on all 6 combinations) When the complex statement is replaced with the commented out equivalent, the result is right. It fails on stated magic numbers - the code is basically a test if a write to external memory crosses the 64KB chip boundary or not. Complete source code: #define F_CPU 16000000UL #include <avr/io.h> uint8_t _eeprom_cross16bit( uint32_t a, uint16_t c ) { /* uint32_t s = a & 0x10000; uint32_t e = (a + c -1) & 0x10000; return s != e; */ return (a & 010000) != ( (a + c -1) & 010000 ); } // main code int main(void) { // init LEDS PORTC = 0; DDRC = 0xFF; // comparison result: if (_eeprom_cross16bit( 4075 , 43 )) { // wrong PORTC = 0x55; } else { // good PORTC = 0x0F; } while(1); return 0; } -- Summary: [avr] Complex equations evaluate wrong Product: gcc Version: 3.4.6 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: daniel at rozsnyo dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38767