http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52474
--- Comment #3 from wwieser at gmx dot de 2012-03-14 01:19:59 UTC --- OK, so talking to the debian maintainer, the GCC version I'm using is based upon patches available here: http://distribute.atmel.no/tools/opensource/avr-gcc/ Here is a test case. I'm unsure how to make a test case for code to be executed on a microcontroller because I need to make some assumptions about the board so that the result can be shown to the user somehow (flash a LED, whatever...) So, here is a test case which I believe to reproduce the bug. Please test this against the newest version of avr-gcc. Compile with: bash# avr-gcc -W -Wall -O2 -mmcu=atxmega192a3 f1.c f2.c -o f.elf We need 2 files (f1.c, f2.c) to prevent optimization of the whole computation. So, it is important to keep both files separate! ----------------<File: f1.c >------------------------------------ typedef signed short int int16; typedef signed long int int32; extern void ERROR(int16 v); extern void SUCCESS(int16 v); extern int16 QUERY(void); int main() { int16 slope = 1025 + QUERY(); int16 v = (int16)( (int32)(-800) * slope / 512L ) ; // This will compute: v=31166 instead of the correct v=-1601. if(v>0) ERROR(v); else SUCCESS(v); return(0); } ------------------------------------------------------------------- ----------------<File: f2.c >------------------------------------ typedef signed short int int16; void ERROR(int16 v) { // fill in code that signals ERROR to the user or debugger breakpoint. } void SUCCESS(int16 v) { // fill in code that signals SUCCESS to the user or debug breakpoint. } int16 QUERY(void) { return(0); } --------------------------------------------------------------