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.

Questions about expression simplification in TMS320C28x C/C++ compiler



Trying to understand some details about expression simplification from the compiler manual, and I have some questions for clarification:

  • Not sure from the manual what level of optimization includes expression simplification? Or does it happen even if optimizations are off?
  • Does expression simplification occur when typecasts are included? For example, if I use the macro SENSOR_LSB in the example below, will it do the division operation at run time or will the compiler realize that it is a constant value everywhere?

#define SENSOR_MAX_COUNTS 1000U
#define SENSOR_RANGE      5.0
#define SENSOR_LSB        (SENSOR_RANGE/(float)SENSOR_MAX_COUNTS)

Thank you!

  • Expression simplification is a broad term.  There are many forms of it.  Some of them are simple, and others are more complex.

    does it happen even if optimizations are off?

    Simpler forms of it, yes.

    if I use the macro SENSOR_LSB in the example below, will it do the division operation at run time or will the compiler realize that it is a constant value everywhere?

    The compiler knows it is a constant value everywhere.

    Thanks and regards,

    -George

  • Thank you! This may be getting in to the weeds, but I tried to make myself a test case and look at the generated assembly before I made the original post. The relevant bit of code I used was:

        int16_t ii;
        volatile uint32_t testCnts;
        volatile float testFloat;
    
        for(ii = 0; ii < 5; ii++)
        {
            testCnts = ii*10;
            testFloat = testCnts * SENSOR_LSB; //(this is line 100)
        }

    ...which generated the following assembly code:

            UI32TOF64 R0,*-SP[2]            ; [CPU_FPU] |100| 
            NOP       ; [CPU_ALU] 
            MPYF64    R0,R0,#16048          ; [CPU_FPU] |100| 
            NOP       ; [CPU_ALU] 
            NOP       ; [CPU_ALU] 
            F64TOF32  R0H,R0                ; [CPU_FPU] |100| 
            NOP       ; [CPU_ALU] 
            MOV32     *-SP[4],R0H           ; [CPU_FPU] |100| 

    ... I'm not very good with assembly yet, but I assumed that the UI32TOF64 instruction was the typecast from my #define SENSOR_LSB. If so I figured either I needed to turn on optimization to get it to write a constant value to my testFloat variable without any additional instructions, or I am wrong about how expression simplification (specifically the form of it described in section 3.16.5 of the compiler guide) works... Or I suppose, I am just wrong about the why that UI32TOF64 is there?

    Thanks again for your help!

  • Or I suppose, I am just wrong about the why that UI32TOF64 is there?

    The definition of SENSOR_RANGE is:

    #define SENSOR_RANGE      5.0

    Where the un-suffixed floating point literal 5.0 is implicitly type double, causing the compiler to promote the calculation to double precision.

    Try changing to use a single-precision floating pointy literal by adding a suffix f to how much the expression gets simplified:

    #define SENSOR_RANGE      5.0f

  • Okay I tried that and it did indeed shorten it to 5 instructions instead of 8, but still seems to be typecasting:

            UI32TOF32 R0H,*-SP[2]           ; [CPU_FPU] |100| 
            NOP       ; [CPU_ALU] 
            MPYF32    R0H,R0H,#13984        ; [CPU_FPU] |100| 
            NOP       ; [CPU_ALU] 
            MOV32     *-SP[4],R0H           ; [CPU_FPU] |100| 

    After tinkering for a bit though it seems that setting optimization to two or higher does make the UI32TOF32 instruction go away, but since I'm not really ready to turn on optimizations in my project yet this is all just academic... a few extra instructions for the typecast right now are a reasonable trade for being able to swap out sensor configurations easily. 

    Anyway, thanks again for your help!