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.

CCS Does Not Compile C abs() Function Code

 

It has been observed that when the C absolute function, abs(), is called in C code, such as that below, the CCS compiler will not generate assembly code for that C abs() code when Optimization is set to "3 Interprocedure Optimizations":

   et = Timestamp_get32();

   if (abs(intvar1 - intvar2) > 1)

   {

      intvar2 = intvar1;

   }

   et = Timestamp_get32() - et - cal;

 

The first indication that assembly code for the C abs() function was not generated was when the elapsed time was zero only when the compiler Optimization was set to "3 Interprocedure Optimizations”.

 

Proof that the compiler does not generate assembly code for the C abs()code above is provided in the associated assembly code below. Note that a comment containing the C abs() code is given but no assembly code is generated for it:

;----------------------------------------------------------------------

; 215 | et = Timestamp_get32();                                               

; 216 | if (abs(intvar1 - intvar2) > 1)                                       

; 218 |         intvar2 = intvar1;        

;----------------------------------------------------------------------

        LCR       #_xdc_runtime_Timestamp_get32__E ; [CPU_] |215|

        MOVW      DP,#_et               ; [CPU_U]

        MOVL      @_et,ACC              ; [CPU_] |215|

;----------------------------------------------------------------------

; 220 | et = Timestamp_get32() - et - cal;                                    

;----------------------------------------------------------------------

        LCR       #_xdc_runtime_Timestamp_get32__E ; [CPU_] |220|

 

The associated Debugger Disassembly code follows which also proves that no assembly code was generated for the C abs() code:

10a6bf:   7650FEDC    LCR          xdc_runtime_Timestamp_get32__F

10a6c1:   761F03C0    MOVW         DP, #0x3c0

10a6c3:   1E04        MOVL         @0x4, ACC

10a6c4:   7650FEDC    LCR          xdc_runtime_Timestamp_get32__F

 

QUESTION: Why is C code associated with the abs()function not included when compiled with the Optimization set to "3 Interprocedure Optimizations" in the CCS compiler?

 

Thank you,

Tim Ball

TDB Consulting

  • Please show us the types of the variables involved and the prototype for Timestamp_get32.

  • Most likely, you're going to need to widen your view of the assembly.  The assembly code you've shown us is one call to Timestamp_get32, followed by a store to et, followed by another call to Timestamp_get32.  Although the interlisted C code shows the call to abs() after the set to et, when using high levels of optimization, sometimes the interlisted C code gets a bit off.  In particular notice that the variable et is not involved in the call to abs.  The compiler should have generated an abs machine instruction instead of a function call, and this could very easily have been moved relative to adjacent instructions.

  • The reason why the compiler ignored the C abs() function call and all associated code, when Optimized, has been discovered.

    A few lines above the C abs() code (before setting the elapsed timer), intvar1 and intvar2 were (mistakenly) set to the same value.

    Since intvar1 and intvar2 were always identical at the abs() function call code line, all code related to the abs() function call was not needed. That is, abs(intvar1 - intvar2) would never ever be > 1 because the values were always identical at that point; therefore, that code could be ignored, precisely as the compiler did when the code was Optimized.

    Tim