Hello,
I am using S32 design studio for power architecture with MPC5744P and I have been using the conditional statements.
The shortened code looks like this:
// variable declaration for the sake of type insight
float dutyCycle;
vuint16_t a, b, c;
uint16_t inv = FALSE;
//assign a value to variable a
a = (pSub->VAL1.R * dutyCycle);
//assign a value to variable b
b = (-((int16_t)(pSub->VAL1.R * dutyCycle)));
//assign a value to variable c using a and b -> correct value
c = (inv) ? a : b;
//assign a value to variable c using statements used for assigning a and b
//-> incorrectly returns 0
c = (inv) ? (pSub->VAL1.R * dutyCycle) : (-((int16_t)(pSub->VAL1.R * dutyCycle)));
The c variable will be assigned correctly when using a and b variables, but will be 0 if the values are entered directly in the conditional expression.
Do you know what this could be about?
Hi,
could you please create some simple working example, which demonstrates your issue and post it here? From the source code you shared, it is difficult to reproduce you issue, because I have to check assembly code in debugger.
Could you please also write me, which version of S32 Design Studio you use (1.0 or 1.1)?
Regards,
Martin
I have been using the S32 Design Studio 1.1. This is a working example that is showing the misbehavior:
typedef short int16_t;
typedef unsigned short uint16_t;
typedef volatile unsigned short vuint16_t;
int main(void) {
float dutyCycle = 0.2f;
vuint16_t val1 = 8000;
vuint16_t a, b, c;
uint16_t inv = 0;
for(;;) {
//assign a value to variable a
a = (val1 * dutyCycle);
//assign a value to variable b
b = (-((int16_t)(val1 * dutyCycle)));
//assign a value to variable c using a and b -> correct value
c = (inv) ? a : b;
//assign a value to variable c using statements used for assigning a and b
//-> incorrectly returns 0
c = (inv) ? (val1 * dutyCycle) : (-((int16_t)(val1 * dutyCycle)));
/* Loop forever */
}
}
Hello,
I'd recommend not to rely on implicit integer conversion here.
Could you try to cast the operands explicitly:
(vuint16_t)
(-((int16_t)(val1 * dutyCycle)));
I have seen different results on different compilers (GHS vs. IAR vs. GCC,...).
hope it helps
Stan
Thank you, this does give the correct result, although I would like to be able to rely on implicit integer conversion.
Seems it wasn't a problem in all other cases.
Hi,
I consulted your use case with my colleague, who is compiler expert and it seems it is really compiler bug. So I reported it and I hope it will be fixed soon.
Regards,
Martin