Part Number: TMS320F28377D
Other Parts Discussed in Thread: TEST2
Hi,
While browsing the community, I came across the thread linked below and had a few technical questions regarding the TI engineer's response.
In the original post, the author provided the following constant definition and code snippet:
#define CURRENT_SAMPLE_DEFAULT_OFFSET_MAX 39321 // 32768 * 1.2
volatile int32_t temp2;
temp2 = CURRENT_SAMPLE_DEFAULT_OFFSET_MAX; // temp2 = 2457
I read the TI engineer's explanation regarding this, but I am somewhat confused by it.
On a TMS320F28x MCU, even if we define the macro as a bare decimal literal and assign it to a 32-bit variable like above, the value 39321 is successfully and correctly stored in temp2. According to the C standard specifications, the type of an unsuffixed decimal integer literal is automatically determined as the first type in the sequence of int → long int → long long int that can fully represent its value. Since 39321 exceeds the maximum value of a signed 16-bit int (32767), it should inherently be treated as a 32-bit long int.
Even if the value 39321 were processed as a 16-bit type and truncation occurred, the result would still not be 2457. (Of course, if the value is assigned to a 12-bit register, 0x9999 would be truncated to 0x0999, which explains where the value 2457 comes from.)
If we perform compile-time arithmetic operations that overflow 16-bit limits without explicit casting, truncation due to overflow will occur as shown in the examples below. However, even in those cases, the TI compiler alerts the user during the build process with a specific warning message:
#define TEST1 (30000 * 30000)
#define TEST2 (0x4120 << 8)
volatile int32_t temp1, temp2;
temp1 = TEST1; // temp1 = -5888 (Truncated)
temp2 = TEST2; // temp2 = 8192 (Truncated)
[Compiler Warning]
"xxx.c", line nnn: warning #62-D: integer operation result is out of range
(While it is true that attaching suffixes like L or UL when defining constants is the correct method and good habit to prevent truncation phenomena like the example) Given this compiler behavior and the C standard rules for literal promotion, is the explanation provided in the original TI response entirely accurate? Or am I missing some C2000-specific compiler behavior here?
I would appreciate any further clarification on this matter.
Best regards,
Sang-il