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 rtwdemo_float_mul_for_net_slope_correction.

model = 'rtwdemo_float_mul_for_net_slope_correction';
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('rtwdemo_float_mul_for_net_slope_correction/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. Create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

2. 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');

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

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

Build Summary

Top model targets built:

Model                                       Action                       Rebuild Reason                                    
===========================================================================================================================
rtwdemo_float_mul_for_net_slope_correction  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 16.329s

4. Inspect the generated rtwdemo_float_mul_for_net_slope_correction_step step function in the rtwdemo_float_mul_for_net_slope_correction.c.

file = fullfile('rtwdemo_float_mul_for_net_slope_correction_ert_rtw','rtwdemo_float_mul_for_net_slope_correction.c');
rtwdemodbtype(file,'/* Model step function */','/* Model initialize function',1,1);
/* Model step function */
void rtwdemo_float_mul_for_net_slope_correction_step(void)
{
  real32_T tmp;

  /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates:
   *  Inport: '<Root>/Input'
   */
  tmp = (real32_T)fmod((real_T)(real32_T)floor((real_T)rtU.Input), 256.0);

  /* Outport: '<Root>/Output' incorporates:
   *  DataTypeConversion: '<Root>/Data Type Conversion'
   */
  rtY.Output = (uint8_T)(tmp < 0.0F ? (int32_T)(uint8_T)(int32_T)-(int32_T)
    (int8_T)(uint8_T)-tmp : (int32_T)(uint8_T)tmp);
}

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 rtwdemo_float_mul_for_net_slope_correction_step step function with optimization in the rtwdemo_float_mul_for_net_slope_correction.c

slbuild(model);
file = fullfile('rtwdemo_float_mul_for_net_slope_correction_ert_rtw','rtwdemo_float_mul_for_net_slope_correction.c');
rtwdemodbtype(file,'/* Model step function */','/* Model initialize function',1,1);
### Starting build procedure for: rtwdemo_float_mul_for_net_slope_correction
### Successful completion of build procedure for: rtwdemo_float_mul_for_net_slope_correction

Build Summary

Top model targets built:

Model                                       Action                       Rebuild Reason                   
==========================================================================================================
rtwdemo_float_mul_for_net_slope_correction  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 13.006s

/* Model step function */
void rtwdemo_float_mul_for_net_slope_correction_step(void)
{
  /* Outport: '<Root>/Output' incorporates:
   *  DataTypeConversion: '<Root>/Data Type Conversion'
   *  Inport: '<Root>/Input'
   */
  rtY.Output = (uint8_T)rtU.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);
cd(currentDir);
rtwdemoclean;

See Also

Related Topics