Remove Code From Floating-Point to Integer Conversions That Wraps Out-of-Range Values

This example shows how to remove code for out-of-range floating-point to integer conversions. Without this code, there might be a mismatch between simulation and code generation results. Standard C does not define the behavior of out-of-range floating-point to integer conversions, while these conversions are well-defined during simulation. In Standard C and during simulation, floating-point to integer conversions are well-defined for input values in the range of the output type.

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

In this model, a Data Type Conversion block converts an input signal from a double to a 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. In this example, the model is named conversion_ex.

  1. Use Inport, Outport, and Data Type Conversion blocks to create the example model.

  2. Open the Inport Block Parameters dialog box and select the Signal Attributes tab. For the Data Type parameter, select double.

  3. Open the Data Type Conversion dialog box. For the Output data type parameter, select uint8.

  4. Open the Simulink Coder app. If you have an Embedded Coder license, open the Embedded Coder app.

  5. Select Code Interface > Individual Element Code Mappings.

  6. On the Code Mappings-C > Inports tab, set the storage class to ImportedExtern.

  7. In the Code section of the Property Inspector, assign a name to the Identifier such as Inport.

  8. On the Code Mappings-C > Outputs tab, set the storage class to ImportedExtern.

  9. In the Code section of the Property Inspector, assign a name to the Identifier such as Outport.

Generate Code Without Optimization

  1. Open the Configuration Parameters dialog box. On the Solver pane, for the Type parameter, select Fixed-step.

  2. On the Code Generation > Report pane, select Create code generation report and Open report automatically.

  3. On the Code Generation pane, select Generate code only, and then, in the model window, press Ctrl+B. When code generation is complete, an HTML code generation report opens.

  4. In the Code Generation report, select the conversion_ex.c file and view the model step function. The code generator applies the fmod function to handle out-of range-results.

void conversion_ex_step(void)
{
  real_T tmp;

  /* DataTypeConversion: '<Root>/Data Type Conversion' incorporates:
   *  Inport: '<Root>/Input'
   */
  tmp = floor(Inport);
  if (rtIsNaN(tmp) || rtIsInf(tmp)) {
    tmp = 0.0;
  } else {
    tmp = fmod(tmp, 256.0);
  }

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

Generate Code with Optimization

  1. Open the Configuration Parameters dialog box. On the Optimization pane, select Remove code from floating-point to integer conversions that wraps out-of-range values. Generate code.

  2. In the code generation report, select the conversion_ex.c file and view the model step function. The generated code does not contain code that protects against out-of-range values.

    void conversion_ex_step(void)
    {
      /* Outport: '<Root>/Out1' incorporates:
       *  DataTypeConversion: '<Root>/Data Type Conversion'
       *  Inport: '<Root>/Input'
       */
      Outport = (uint8_T)Inport;
    }

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

See Also

Related Topics