Search code examples
ccompiler-optimizationcode-composermap-filescode-size

Disable 64-bit division in Code Composer Studio compiler


I'm currently writing a program in C using Code Composer Studio (CCS) V7.4.0.00015. The program has several self-written libraries that perform Byte, unsigned int and float division.

I have reached that stage in the project where I need to reduce code size in order to ensure there is enough space to fit the boot-loader.

Looking at my .map file reveals several a runtime-support objects that CCS is automatically including. Some of these include the following:

  • div64u.obj --> 846 bytes
  • div64s.obj --> 316 bytes

These objects are from the rts430x_lc_sd_eabi.lib

My question is: Why are these 64bit division objects being included (especially when I don't have any 64 bit floats in my program)? And more importantly, can I disable them (or stop CCS from including them)?

I've spent a few days googling around and trawling different sites but I haven't been able to find much documentation on these objects or how to disable them.

Edit: Turns out I do in fact have one function utilising long long ints (typedef'd as SLLONG)

/**
 * @brief Compensate the raw pressure gained from the BME
 * @details Uses the pressure compensation parameters to 
 *      calculate the true pressure from the raw pressure
 *      
 *      Output value of “96386.2” equals 96386.2 Pa = 963.862 hPa
 *
 *      The contents of this function have been taken from the Adafruit Github page
 *      https://github.com/adafruit/Adafruit_BME280_Library
 * 
 * @param rawPressure The raw pressure
 * @param tempFine The temperature in high resoltuion format, 
 *      gained from the BME_compensateTemp() function
 * 
 * @return the pressure read from the device
 */
float BME_compensatePressure(ULONG rawPressure, SLONG tempFine)
{
    SLLONG var1, var2, p;

    if (rawPressure == 0x800000) // value in case pressure measurement was disabled
        return SNaN;
    rawPressure >>= 4;

    var1 = ((SLLONG)tempFine) - 128000;                                         // SLONG cast to SLLONG 
    var2 = var1 * var1 * (SLLONG)compParamsStruct.dig_P6;                       // SLONG^2 x (SWORD cast to SLLONG) 
    var2 = var2 + ((var1*(SLLONG)compParamsStruct.dig_P5)<<17);                 // SLLONG + (SLLONG * SWORD cast to SLLONG)
    var2 = var2 + (((SLLONG)compParamsStruct.dig_P4)<<35);
    var1 = ((var1 * var1 * (SLLONG)compParamsStruct.dig_P3)>>8) +
           ((var1 * (SLLONG)compParamsStruct.dig_P2)<<12);
    var1 = (((((SLLONG)1)<<47)+var1))*((SLLONG)compParamsStruct.dig_P1)>>33;

    if (var1 == 0) {
        return 0; // avoid exception caused by division by zero
    }
    p = 1048576 - rawPressure;
    p = (((p<<31) - var2)*3125) / var1;
    var1 = (((SLLONG)compParamsStruct.dig_P9) * (p>>13) * (p>>13)) >> 25;
    var2 = (((SLLONG)compParamsStruct.dig_P8) * p) >> 19;

    p = ((p + var1 + var2) >> 8) + (((SLLONG)compParamsStruct.dig_P7)<<4);
    return ((float)p)/256;
}

New question:

  • Can anyone figure out a way to rearrange the function so that it does not require the use of the long long integers (without causing any loss of precision?)
  • OR more specifically, can anyone figure out how I can do that long long division differently i.e the line shown below:
p = (((p<<31) - var2)*3125) / var1;

Solution

  • Summary of my solution to the original problem of 64bit float operations:

    The following lines were first inserted into the compiler flags:

    --float_operations_allowed=32
    

    This however produced several errors around the project. The error was the same for each location:

    #1558-D 64-bit floating point operations are not allowed
    

    The code that produced these errors was:

    float lowerFence = med -1.5 * IQR;
    float upperFence = med +1.5 * IQR;
    

    and

    return 0.5*coeffs->c0+tempScaled*coeffs->c1;                
    

    The error was fixed by casting literals to floats and moving multiple float operations to single lines

    float IQR = STATS_Iqr(sorted, numSamples);
    float iqrScaled = 1.5 * IQR;
    float lowerFence = med - iqrScaled;
    float upperFence = med + iqrScaled;
    

    and

    float half = 0.5;
    float c0Scaled = half*coeffs->c0;
    float c1Scaled = tempScaled*coeffs->c1;
    return c0Scaled + c1Scaled;  
    

    After the above errors were resolved, the project was cleaned and rebuilt. Adding this compiler flag had the effect of removing the below objects