Main Content

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 rtwdemo_float_mul_for_net_slope_correction 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. Create a temporary folder for the build and inspection process.

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

3. Build the 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 15.778s

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

/* Model step function */
void rtwdemo_float_mul_for_net_slope_correction_step(void)
{
  /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates:
   *  Inport: '<Root>/Input'
   */
  if (rtU.Input < 256.0F) {
    if (rtU.Input >= 0.0F) {
      /* Outport: '<Root>/Output' */
      rtY.Output = (uint8_T)rtU.Input;
    } else {
      /* Outport: '<Root>/Output' */
      rtY.Output = 0U;
    }
  } else if (rtU.Input >= 256.0F) {
    /* Outport: '<Root>/Output' */
    rtY.Output = MAX_uint8_T;
  } else {
    /* Outport: '<Root>/Output' */
    rtY.Output = 0U;
  }

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

The code generator applies condition to map NaN to interger 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: 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 12.515s

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

/* Model step function */
void rtwdemo_float_mul_for_net_slope_correction_step(void)
{
  /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates:
   *  Inport: '<Root>/Input'
   */
  if (rtU.Input < 256.0F) {
    if (rtU.Input >= 0.0F) {
      /* Outport: '<Root>/Output' */
      rtY.Output = (uint8_T)rtU.Input;
    } else {
      /* Outport: '<Root>/Output' */
      rtY.Output = 0U;
    }
  } else {
    /* Outport: '<Root>/Output' */
    rtY.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

Related Topics