Remove Code That Maps NaN to Integer Zero
This example shows how to remove code that maps NaN
to integer zero. For floating point to integer conversions involving saturation, Simulink® converts NaN to integer zero during simulation. If your model contains an input value of NaN
, you can specify that the code generator produce code that maps NaN to zero. Without this code, there is a mismatch between simulation and code generation results because in Standard C, every condition involving NaN
evaluates to false.
If input values of NaN
do not exist in your application, you can remove code that maps NaN to integer zero. Removing this code reduces the size and increases the speed of the generated code.
Example Model
Open the example model FloatMultiplicationNetSlope
by entering the model name in the Command Window.
The model contains a Data Type Conversion block. Configure the model to convert the input signal from a single
to a uint8
.
1. Open the Data Type Conversion dialog box. For the Output data type parameter, select uint8
.
2. Select the Saturate on integer overflow parameter check box. Selecting this parameter specifies that an out-of-range signal value equals either the minimum or maximum value that the data type can represent.
3. Open the Configuration Parameters dialog box. Select the Support: non-finite numbers parameter check box. Selecting this parameter enables generating nonfinite data and operations on nonfinite data.
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 with saturation that maps NaN to zero parameter check box.
2. Build the model.
### Starting build procedure for: FloatMultiplicationNetSlope ### Successful completion of build procedure for: FloatMultiplicationNetSlope Build Summary Top model targets: Model Build Reason Status Build Duration ============================================================================================================================== FloatMultiplicationNetSlope Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 9.9337s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.532s
3. Inspect the generated FloatMultiplicationNetSlope_step
step function in the FloatMultiplicationNetSlope.c
.
/* Model step function */ void FloatMultiplicationNetSlope_step(void) { /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates: * Inport: '<Root>/Input' */ if (FloatMultiplicationNetSlope_U.Input < 256.0F) { if (FloatMultiplicationNetSlope_U.Input >= 0.0F) { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = (uint8_T) FloatMultiplicationNetSlope_U.Input; } else { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = 0U; } } else if (FloatMultiplicationNetSlope_U.Input >= 256.0F) { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = MAX_uint8_T; } else { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = 0U; } /* End of DataTypeConversion: '<Root>/Data Type Conversion' */ }
The code generator applies condition to map NaN
to integer zero. In this case, the generated code is larger but the results of the generated code match with simulation results.
Generate Code with Optimization
1. Open the Configuration Parameters dialog box.
2. On the Optimization pane, select the Remove code from floating-point to integer conversions with saturation that maps NaN to zero parameter.
3. Build the model.
### Starting build procedure for: FloatMultiplicationNetSlope ### Successful completion of build procedure for: FloatMultiplicationNetSlope Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================== FloatMultiplicationNetSlope Generated code was out of date. Code generated and compiled. 0h 0m 7.0891s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 8.4854s
4. Inspect the generated FloatMultiplicationNetSlope_step
step function with optimization in the FloatMultiplicationNetSlope.c
.
/* Model step function */ void FloatMultiplicationNetSlope_step(void) { /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates: * Inport: '<Root>/Input' */ if (FloatMultiplicationNetSlope_U.Input < 256.0F) { if (FloatMultiplicationNetSlope_U.Input >= 0.0F) { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = (uint8_T) FloatMultiplicationNetSlope_U.Input; } else { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = 0U; } } else { /* Outport: '<Root>/Output' */ FloatMultiplicationNetSlope_Y.Output = MAX_uint8_T; } /* End of DataTypeConversion: '<Root>/Data Type Conversion' */ }
The generated code maps NaN
to 255 and not integer zero. The generated code is more efficient without the extra code that maps NaN to integer zero. But it is possible that the execution of generated code does not produce the same results as the simulation for NaN
values.
See Also
Remove code from floating-point to integer conversions with saturation that maps NaN to zero