8 bit signed overflowing problem
Posted: Fri Sep 11, 2015 8:43 am
I have discovered a problem with some signed 8 bit code. As far as I can tell a code example in http://www.teigfam.net/oyvind/home/tech ... g_surprise (disclaimer: no money with this or any other of my links) fails:
It also fails in xTIMEcomposer. Or is this correct in any way? Why is it compiled like this and what is the "solution"?
Is the XMOS GCC-based?
It works as expected in Go, see my blog note or http://play.golang.org/p/lH2SzZQEHG
Code: Select all
unsigned int Cnt1 = 0; // 16 bits (kind of system clock)unsigned int Cnt2 = 0; // 16 bits (observer)signed char Timer_Secs = 0; // 8 bits with signsigned char FutureTimeout_1Minute = 60; // 8 bits with signwhile (Cnt1 < 602) { // ## if ((Timer_Secs - FutureTimeout_1Minute) == 0) { FutureTimeout_1Minute += 60; // Overflow/underflow allowed, // so wraps and changes sign Cnt2++; } else {} // No code Timer_Secs++; Cnt1++;} // ## // Cnt2 is 35 if > 0 above #define AFTER_F(a,b) ((a-b)>0)// Cnt2 is 35 if >= 0 above #define AFTER_EQ_F(a,b) ((a-b)>=0)// Cnt2 is 10 if == 0 above #define AFTER_ON_F(a,b) ((a-b)==0) // 10 is expected
Is the XMOS GCC-based?
It works as expected in Go, see my blog note or http://play.golang.org/p/lH2SzZQEHG