This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

32-bit auto variables

Other Parts Discussed in Thread: MSP430F5419

Hi,

I'm using CCS 4.2.1.00004 to write code for an MSP430F5419.

I've recently spotted that in one of my functions a 32-bit auto variable (unsigned long) is being treated as if it were 16 bits i.e. when I do some maths that results in a large number the variable only contans the bottom 16-bits of the answer.

Is this expected behaviour are 32-bit auto variables not supported?

Regards,

Ben

  • No, it is expected that 32-bit values are computed with 32-bit math.  Anything else would be a bug.  Can you post a test case that demonstrates the problem?

  •  

     

     

     

     

    long 

     

    rtcGetTimeSinceMidnightSunday(void)

    {

     

     

    long retValue;

     

     

    struct  tm* brokenTime;

     

     

    long timeNow = (long) rtcGetTime() + GetLocalOffset();

     

    brokenTime = localtime((

    const  time_t*)&timeNow);

     

    retValue = brokenTime->tm_wday * 86400;

    retValue += brokenTime->tm_hour * 3600;

    retValue += brokenTime->tm_min * 60;

    retValue += brokenTime->tm_sec;

     

     

     

    return  retValue;

    }

    The above function has the problem, all the maths on retValue seems to be done as 16-bit maths. My aasumption of this is based on what I see in CCS when watching the variable so I suppose it could be a problem with CCS.

     

  • I've been looking at this again today.

    It seems the problem is not with it being an auto-variable per se but the fact that its the return value of the function. timeNow is also an unisgned long yet this holds the correct values.

  • Ahh...this seems to be a problem with CCS, the local watch window is only displaying the lower 16-bits of the variable however if I inspect what the value actually returned to the rest of the code then it is correct.

  • All the fields of "struct tm" are int, thus 16 bits.  Integer literals without a suffix like "L" are int, thus 16 bits.

    Therefore, all the arithmetic is done using 16-bit operations.

    In C, the type of the variable being assigned to does not control the type used for arithmetic operations.  Rather, the types of the operands are what matter.  "int * int" will always be computed as int;  if you want it computed as long, you need either or both operand(s) to be long.

    For example, I bet it would work as you wish if you used "86400L" and "3600L" and "60L" as your constants.  Alternatively, cast each of the tm fields to long.