Main Content

Remove Code for Out-of-Range Floating Point to Integer Conversions

This example shows how to generate optimized code by removing code for out-of-range floating point to integer conversions. Standard C does not define the behavior of out-of-range floating point to integer conversions, while these conversions are well-defined during simulation. When you select the configuration parameter Remove code from floating-point to integer conversions that wraps out-of-range values, the code generator removes the wrapping code that handles out-of-range floating point to integer conversion and improves code efficiency. Without this code, there might be a mismatch between simulation and code generation results for out-of range values.

If the input values in your application are in the range of the output type, remove code for out-of-range floating-point to integer conversions. Removing this code reduces the size and increases the speed of the generated code.

Example Model

Open the example model FloatMultiplicationNetSlope.

model = 'FloatMultiplicationNetSlope';
open_system(model);

The model contains a Data Type Conversion block. To configure the model to convert the input signal from a single to a uint8, open the Data Type Conversion dialog box. For the Output data type parameter, select uint8. Alternatively, use this command-line API to set the Output data type:

set_param('FloatMultiplicationNetSlope/Data Type Conversion', 'OutDataTypeStr','uint8');

A uint8 can support values from 0 to 255. If the input signal has a value outside of this range, an out-of-range conversion occurs.

Generate Code Without Optimization

1. Open the Configuration Parameters dialog box. On the Optimization pane, clear the Remove code from floating-point to integer conversions that wraps out-of-range values parameter check box. Alternatively, use the command-line API to disable the optimization:

set_param(model, 'EfficientFloat2IntCast','off');

2. Build the model by using the slbuild function or by pressing Ctrl+B.

slbuild(model)
### Starting build procedure for: FloatMultiplicationNetSlope
### Successful completion of build procedure for: FloatMultiplicationNetSlope

Build Summary

Top model targets built:

Model                        Action                        Rebuild Reason                                    
=============================================================================================================
FloatMultiplicationNetSlope  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 15.853s

3. Inspect the generated FloatMultiplicationNetSlope_step step function in the FloatMultiplicationNetSlope.c.

file = fullfile('FloatMultiplicationNetSlope_grt_rtw','FloatMultiplicationNetSlope.c');
coder.example.extractLines(file,'/* Model step function */','/* Model initialize function',1,1);
/* Model step function */
void FloatMultiplicationNetSlope_step(void)
{
  real32_T tmp;

  /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates:
   *  Inport: '<Root>/Input'
   */
  tmp = (real32_T)floor(FloatMultiplicationNetSlope_U.Input);
  if (rtIsNaNF(tmp) || rtIsInfF(tmp)) {
    tmp = 0.0F;
  } else {
    tmp = (real32_T)fmod(tmp, 256.0);
  }

  if (tmp < 0.0F) {
    /* Outport: '<Root>/Output' */
    FloatMultiplicationNetSlope_Y.Output = (uint8_T)-(int8_T)(uint8_T)-tmp;
  } else {
    /* Outport: '<Root>/Output' */
    FloatMultiplicationNetSlope_Y.Output = (uint8_T)tmp;
  }

  /* End of DataTypeConversion: '<Root>/Data Type Conversion' */
}

The code generator applies the fmod function to handle out-of range-results. In this case, the generated code is larger but the results of the generated code match the simulation results.

Generate Code with Optimization

1. Open the Configuration Parameters dialog box. On the Optimization pane, select the Remove code from floating-point to integer conversions that wraps out-of-range values parameter. Alternatively, use the command-line API to enable the optimization:

set_param(model, 'EfficientFloat2IntCast','on')

2. Build the model. Inspect the generated FloatMultiplicationNetSlope_step step function with optimization in the FloatMultiplicationNetSlope.c

slbuild(model);
file = fullfile('FloatMultiplicationNetSlope_grt_rtw','FloatMultiplicationNetSlope.c');
coder.example.extractLines(file,'/* Model step function */','/* Model initialize function',1,1);
### Starting build procedure for: FloatMultiplicationNetSlope
### Successful completion of build procedure for: FloatMultiplicationNetSlope

Build Summary

Top model targets built:

Model                        Action                        Rebuild Reason                   
============================================================================================
FloatMultiplicationNetSlope  Code generated and compiled.  Generated code was out of date.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 12.926s

/* Model step function */
void FloatMultiplicationNetSlope_step(void)
{
  /* Outport: '<Root>/Output' incorporates:
   *  DataTypeConversion: '<Root>/Data Type Conversion'
   *  Inport: '<Root>/Input'
   */
  FloatMultiplicationNetSlope_Y.Output = (uint8_T)
    FloatMultiplicationNetSlope_U.Input;
}

The generated code is more efficient without the protective code, but it is possible that the execution of the generated code does not produce the same results as simulation for values not in the range of 0 to 255.

Clean Up Example Folders and Files

Close the model and remove temporary folders and files.

bdclose(model);

See Also

Related Topics