It shows that for uint16_t type, conditional statements with expressions, not simple variables or constants, yield incorrect results.
This is a test for a problem I have in the Nil chSysTimerHandlerI and chSchGoSleepTimeoutS functions in Tickless mode. See [url]
viewtopic.php?f=12&t=1301&start=10#p15278[/url]
The following code is inserted into a demo as a thread. It requires SD2 be running.
Code: Select all
/*
* Test code thread.
*/
static THD_WORKING_AREA(waThread4, 128);
static THD_FUNCTION(Thread4, arg) {
(void)arg;
chRegSetThreadName("TestCode");
#define BITS_16 TRUE // Set to FALSE for 32 bit
chnWrite(&SD2, (const uint8_t *)"-----------------\r\n", 19);
chnWrite(&SD2, (const uint8_t *)"Testing C code if statement\r\n", 29);
#if BITS_16
uint16_t a = 100;
chnWrite(&SD2, (const uint8_t *)"uint16_t a = 100;\r\n", 19);
uint16_t b = 0;
chnWrite(&SD2, (const uint8_t *)"uint16_t b = 0;\r\n", 19);
#else
uint32_t a = 100;
chnWrite(&SD2, (const uint8_t *)"uint32_t a = 100;\r\n", 19);
uint32_t b = 0;
chnWrite(&SD2, (const uint8_t *)"uint32_t a = 0;\r\n", 19);
#endif
chnWrite(&SD2, (const uint8_t *)"-----------------\r\n", 19);
chnWrite(&SD2, (const uint8_t *)"a <= b ", 7);
if (a <= b)
chnWrite(&SD2, (const uint8_t *)" : TRUE\r\n", 9);
else
chnWrite(&SD2, (const uint8_t *)" : FALSE\r\n", 10);
chnWrite(&SD2, (const uint8_t *)"-----------------\r\n", 19);
chnWrite(&SD2, (const uint8_t *)"a <= (b - 1) ", 13);
if (a <= (b - 1))
chnWrite(&SD2, (const uint8_t *)" : TRUE\r\n", 9);
else
chnWrite(&SD2, (const uint8_t *)" : FALSE\r\n", 10);
while (true) {
chThdSleepMilliseconds(500);
}
}
// In main add
chThdCreateStatic(waThread4, sizeof(waThread4), NORMALPRIO+4, Thread4, NULL);
Now if we set
#define BITS_16 FALSE
We get
Code: Select all
-----------------
Testing C code if statement
uint32_t a = 100;
uint32_t a = 0;
-----------------
a <= b : FALSE
-----------------
a <= (b - 1) : TRUEWhich looks ok to me. In the second statement the left side is 100 and right side should be 4294967295.
But if we set
#define BITS_16 TRUE
We get
Code: Select all
-----------------
Testing C code if statement
uint16_t a = 100;
uint16_t b = 0;
-----------------
a <= b : FALSE
-----------------
a <= (b - 1) : FALSEAs far as I understand, that last result is incorrect. The right side should be 65535 which is much greater than 100.
It is my understanding that in C uint '-1' is defined as uint_MAX.
This was run on an STM32, complied with gcc-arm-none-eabi-4_7-2013q3 or gcc-arm-none-eabi-4_8-2014q1.
Could someone please review this code and possibly try on a different compiler.
Regards
saraben.