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.

Incorrect results in optimization mode

Hello all,

I have made a simple programm to represent the double numbers as
the numbers in unsigned long long int format with the same hex content.
Then I calculated the control sum of the array as a sum of the hex codes
(I used the sum of the unsigned long long int numbers).
This programm works correctly when it was compiled with the next optimizations mode:
Optimization -> "empty"; Advanced Optimization -> "empty",
but I got the incorrect results when the same program was compiled with this optimizations mode:
Optimization -> "2"; Advanced Optimization -> "2".
I have got the same results with the Release and with the Debug configurations.
I did not look the mistake in the sourse code.

Please answer on the following questions:
-why the same code gives the different results in the different optimization modes?
-which some more the codes can give the similar results?

The programm is attached.

I used:
Code Composer Studio 5.2.1.00018
mcsdk_2_01_02_06
MDSEVM6678L/MDXEVMPCI board

Best regards,
Viktor

  • Hi Viktor,
    What is the compiler version used to build the application? Moving this thread over compiler forum for faster response. Thank you for your patience.

    Thank you.
  • Hi Rajasekaran,

    Thank you for your response.

    I use TI v7.4.7

    Regards,

    Viktor.

  • The problem is the source code violates what is called the strict aliasing rule.  Speaking loosely, the strict aliasing rule says the type used to write to memory must be closely compatible with the type used to read from memory.  For all the details, see the C99 ANSI Standard for C, section 6.5, paragraph 7.  (While the TI compiler is documented to adhere to the C89 standard, the C99 standard is substantially the same in language matters such as this, and is much easier to find online).  

    Here is one example ...

    doubleNumbers_Re = Y_dist_Core0_Re[i][j];
    void_Pointer = (void *) &doubleNumbers_Re;
    uLLInt_Pointer = (unsigned long long int *) void_Pointer;
    uLLInt_Number = (unsigned long long int) *uLLInt_Pointer;
    sumOfuLLInt_Numbers_Re = sumOfuLLInt_Numbers_Re + uLLInt_Number;
    

    The memory location doubleNumbers_Re is written with type double.  But on line 4, this same memory location is read with type unsigned long long.  One of the advantages of the strict aliasing rule is it allows the compiler to presume, in this specific case, that void_Pointer and uLLInt_Pointer cannot point to the same memory location.  It is likely the compiler takes advantage of that presumption in scheduling the instructions for the loop which encloses the above lines.  However, that presumption is wrong.  And it could lead to the behavior described for this case: it works when built with no optimization, but fails when built with high levels of optimization.

    The general term for this programming technique is type punning.  An internet search on that term yields some promising links that further explain the details, and offers suggestions for safer ways to implement type punning.

    Thanks and regards,

    -George

  • Many thanks,

    Regards,

    Viktor.