Tool/software:
Hi,
When using bitfields with uint64_t, I came across a problem where changing one field affected another field. It happened only when the value assigned was negative and optimization was enabled. The minimal testcase is provided below:
Compiler version:TI v22.6.1.LTS
///home/mwrze/ti/ccs1040/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/bin/cl2000 -c --float_support=fpu32 --abi=eabi -mt --diag_wrap=off --keep_asm --gen_func_subsections=on --issue_remarks -g --c99 --fp_mode=relaxed -I/home/mwrze/ti/ccs1040/ccs/tools/compiler/ti-cgt-c2000_22.6.1.LTS/include -O1 main.c -fe main.o
#include "stdint.h"
struct
{
uint32_t a:16;
uint32_t b:16;
}foo;
float getA(){return -10.f;}
int16_t getB(){return 10;}
int main(void)
{
foo.b = getB();
foo.a = (int16_t)getA();
while (1) {}
}
I expect that 'a' should be -10 and 'b' should be 10, both as int16_t. However, in the given case, the result of 'a' was correct (-10), but 'b' was equal to 0xffff. I noticed that the error occurs when casting float to int16_t (and the result was written to the bitfield as 32-bit variable instead 16-bit).